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)
83 TEMPLATABLE_VALUE(
const char *, useragent)
84 TEMPLATABLE_VALUE(uint16_t, timeout)
86 void add_header(const
char *key,
TemplatableValue<const
char *, Ts...>
value) { this->headers_.insert({key, value}); }
90 void set_json(std::function<
void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
94 void play(Ts... x)
override {
95 this->parent_->set_url(this->url_.value(x...));
96 this->parent_->set_method(this->method_.value(x...));
97 if (this->body_.has_value()) {
98 this->parent_->set_body(this->body_.value(x...));
100 if (!this->json_.empty()) {
104 if (this->json_func_ !=
nullptr) {
108 if (this->useragent_.has_value()) {
109 this->parent_->set_useragent(this->useragent_.value(x...));
111 if (this->timeout_.has_value()) {
112 this->parent_->set_timeout(this->timeout_.value(x...));
114 if (!this->headers_.empty()) {
115 std::list<Header> headers;
116 for (
const auto &item : this->headers_) {
117 auto val = item.second;
119 header.
name = item.first;
121 headers.push_back(header);
123 this->parent_->set_headers(headers);
125 this->parent_->send(this->response_triggers_);
126 this->parent_->close();
131 for (
const auto &item : this->json_) {
132 auto val = item.second;
133 root[item.first] =
val.value(x...);
140 std::function<void(Ts..., JsonObject)> json_func_{
nullptr};
147 #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)
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_