17 #include <HTTPClient.h> 20 #include <ESP8266HTTPClient.h> 21 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS 22 #include <WiFiClientSecure.h> 27 namespace http_request {
36 void process(int32_t status_code, uint32_t duration_ms) { this->trigger(status_code, duration_ms); }
41 void dump_config()
override;
44 void set_url(std::string url);
45 void set_method(
const char *method) { this->method_ = method; }
46 void set_useragent(
const char *useragent) { this->useragent_ = useragent; }
47 void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
50 void set_body(
const std::string &body) { this->body_ = body; }
51 void set_headers(std::list<Header> headers) { this->headers_ = std::move(headers); }
52 void send(
const std::vector<HttpRequestResponseTrigger *> &response_triggers);
54 const char *get_string();
61 const char *useragent_{
nullptr};
65 uint16_t timeout_{5000};
70 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS 73 std::shared_ptr<WiFiClient> get_wifi_client_();
80 TEMPLATABLE_VALUE(std::string, url)
81 TEMPLATABLE_VALUE(
const char *, method)
82 TEMPLATABLE_VALUE(std::string, body)
84 void add_header(const
char *key,
TemplatableValue<const
char *, Ts...>
value) { this->headers_.insert({key, value}); }
88 void set_json(std::function<
void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
93 this->parent_->set_url(this->url_.value(
x...));
94 this->parent_->set_method(this->method_.value(
x...));
95 if (this->body_.has_value()) {
96 this->parent_->set_body(this->body_.value(
x...));
98 if (!this->json_.empty()) {
102 if (this->json_func_ !=
nullptr) {
106 std::list<Header> headers;
107 for (
const auto &item : this->headers_) {
108 auto val = item.second;
110 header.
name = item.first;
112 headers.push_back(header);
114 this->parent_->set_headers(headers);
115 this->parent_->send(this->response_triggers_);
116 this->parent_->close();
117 this->parent_->set_body(
"");
122 for (
const auto &item : this->json_) {
123 auto val = item.second;
124 root[item.first] =
val.value(
x...);
131 std::function<void(Ts..., JsonObject)> json_func_{
nullptr};
138 #endif // USE_ARDUINO
void set_json(std::function< void(Ts..., JsonObject)> json_func)
void set_method(const char *method)
void process(int32_t status_code, uint32_t duration_ms)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
void set_follow_redirects(bool follow_redirects)
void set_useragent(const char *useragent)
void add_json(const char *key, TemplatableValue< std::string, Ts... > value)
void set_redirect_limit(uint16_t limit)
std::shared_ptr< BearSSL::WiFiClientSecure > wifi_client_secure_
void set_headers(std::list< Header > headers)
void set_body(const std::string &body)
void set_timeout(uint16_t timeout)
void encode_json_func_(Ts... x, JsonObject root)
std::vector< HttpRequestResponseTrigger * > response_triggers_
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
HttpRequestComponent * parent_
HttpRequestSendAction(HttpRequestComponent *parent)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
void play(Ts... x) override
float get_setup_priority() const override
void register_response_trigger(HttpRequestResponseTrigger *trigger)
std::list< Header > headers_
void encode_json_(Ts... x, JsonObject root)
std::shared_ptr< WiFiClient > wifi_client_