15 #include <HTTPClient.h> 18 #include <ESP8266HTTPClient.h> 19 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS 20 #include <WiFiClientSecure.h> 25 namespace http_request {
36 void dump_config()
override;
39 void set_url(std::string url);
40 void set_method(
const char *method) { this->method_ = method; }
41 void set_useragent(
const char *useragent) { this->useragent_ = useragent; }
42 void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
45 void set_body(
const std::string &body) { this->body_ = body; }
46 void set_headers(std::list<Header> headers) { this->headers_ = std::move(headers); }
47 void send(
const std::vector<HttpRequestResponseTrigger *> &response_triggers);
49 const char *get_string();
56 const char *useragent_{
nullptr};
60 uint16_t timeout_{5000};
65 #ifdef USE_HTTP_REQUEST_ESP8266_HTTPS 68 std::shared_ptr<WiFiClient> get_wifi_client_();
75 TEMPLATABLE_VALUE(std::string, url)
76 TEMPLATABLE_VALUE(
const char *, method)
77 TEMPLATABLE_VALUE(std::string, body)
78 TEMPLATABLE_VALUE(
const char *, useragent)
79 TEMPLATABLE_VALUE(uint16_t, timeout)
81 void add_header(const
char *key,
TemplatableValue<const
char *, Ts...>
value) { this->headers_.insert({key, value}); }
85 void set_json(std::function<
void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
89 void play(Ts... x)
override {
90 this->parent_->set_url(this->url_.value(x...));
91 this->parent_->set_method(this->method_.value(x...));
92 if (this->body_.has_value()) {
93 this->parent_->set_body(this->body_.value(x...));
95 if (!this->json_.empty()) {
99 if (this->json_func_ !=
nullptr) {
103 if (this->useragent_.has_value()) {
104 this->parent_->set_useragent(this->useragent_.value(x...));
106 if (this->timeout_.has_value()) {
107 this->parent_->set_timeout(this->timeout_.value(x...));
109 if (!this->headers_.empty()) {
110 std::list<Header> headers;
111 for (
const auto &item : this->headers_) {
112 auto val = item.second;
114 header.
name = item.first;
116 headers.push_back(header);
118 this->parent_->set_headers(headers);
120 this->parent_->send(this->response_triggers_);
121 this->parent_->close();
126 for (
const auto &item : this->json_) {
127 auto val = item.second;
128 root[item.first] =
val.value(x...);
135 std::function<void(Ts..., JsonObject)> json_func_{
nullptr};
141 void process(
int status_code) { this->trigger(status_code); }
147 #endif // USE_ARDUINO
void set_json(std::function< void(Ts..., JsonObject)> json_func)
void set_method(const char *method)
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_
void process(int status_code)
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
HttpRequestComponent * parent_
HttpRequestSendAction(HttpRequestComponent *parent)
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_