18 namespace http_request {
34 virtual int read(uint8_t *buf,
size_t max_len) = 0;
35 virtual void end() = 0;
42 size_t bytes_read_{0};
48 void process(std::shared_ptr<HttpContainer> container, std::string &response_body) {
49 this->trigger(std::move(container), response_body);
55 void dump_config()
override;
58 void set_useragent(
const char *useragent) { this->useragent_ = useragent; }
59 void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
65 std::shared_ptr<HttpContainer>
get(std::string url) {
return this->start(std::move(url),
"GET",
"", {}); }
66 std::shared_ptr<HttpContainer>
get(std::string url, std::list<Header> headers) {
67 return this->start(std::move(url),
"GET",
"", std::move(headers));
69 std::shared_ptr<HttpContainer>
post(std::string url, std::string body) {
70 return this->start(std::move(url),
"POST", std::move(body), {});
72 std::shared_ptr<HttpContainer>
post(std::string url, std::string body, std::list<Header> headers) {
73 return this->start(std::move(url),
"POST", std::move(body), std::move(headers));
76 virtual std::shared_ptr<HttpContainer> start(std::string url, std::string method, std::string body,
77 std::list<Header> headers) = 0;
80 const char *useragent_{
nullptr};
83 uint16_t timeout_{4500};
84 uint32_t watchdog_timeout_{0};
90 TEMPLATABLE_VALUE(std::string, url)
91 TEMPLATABLE_VALUE(
const char *, method)
92 TEMPLATABLE_VALUE(std::string, body)
93 TEMPLATABLE_VALUE(
bool, capture_response)
95 void add_header(const
char *key,
TemplatableValue<const
char *, Ts...>
value) { this->headers_.insert({key, value}); }
99 void set_json(std::function<
void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
104 this->max_response_buffer_size_ = max_response_buffer_size;
109 if (this->body_.has_value()) {
110 body = this->body_.value(
x...);
112 if (!this->json_.empty()) {
116 if (this->json_func_ !=
nullptr) {
120 std::list<Header> headers;
121 for (
const auto &item : this->headers_) {
122 auto val = item.second;
124 header.
name = item.first;
126 headers.push_back(header);
129 auto container = this->parent_->start(this->url_.value(
x...), this->method_.value(
x...), body, headers);
131 if (container ==
nullptr) {
135 size_t content_length = container->content_length;
136 size_t max_length = std::min(content_length, this->max_response_buffer_size_);
138 std::string response_body;
139 if (this->capture_response_.value(
x...)) {
141 uint8_t *buf = allocator.
allocate(max_length);
142 if (buf !=
nullptr) {
143 size_t read_index = 0;
144 while (container->get_bytes_read() < max_length) {
145 int read = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
150 response_body.reserve(read_index);
151 response_body.assign((
char *) buf, read_index);
156 if (this->response_triggers_.size() == 1) {
158 this->response_triggers_[0]->process(container, response_body);
160 for (
auto *trigger : this->response_triggers_) {
164 auto response_body_copy = std::string(response_body);
165 trigger->process(container, response_body_copy);
173 for (
const auto &item : this->json_) {
174 auto val = item.second;
175 root[item.first] =
val.value(
x...);
182 std::function<void(Ts..., JsonObject)> json_func_{
nullptr};
185 size_t max_response_buffer_size_{SIZE_MAX};
void set_json(std::function< void(Ts..., JsonObject)> json_func)
std::shared_ptr< HttpContainer > post(std::string url, std::string body)
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)
uint32_t get_watchdog_timeout() const
void deallocate(T *p, size_t n)
void add_json(const char *key, TemplatableValue< std::string, Ts... > value)
void set_max_response_buffer_size(size_t max_response_buffer_size)
void set_redirect_limit(uint16_t limit)
size_t get_bytes_read() const
void set_timeout(uint16_t timeout)
std::shared_ptr< HttpContainer > post(std::string url, std::string body, std::list< Header > headers)
void encode_json_func_(Ts... x, JsonObject root)
Application App
Global storage of Application pointer - only one Application can exist.
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_
void set_secure(bool secure)
void set_watchdog_timeout(uint32_t watchdog_timeout)
void IRAM_ATTR HOT yield()
void process(std::shared_ptr< HttpContainer > container, std::string &response_body)
HttpRequestSendAction(HttpRequestComponent *parent)
Implementation of SPI Controller mode.
void play(Ts... x) override
float get_setup_priority() const override
void register_response_trigger(HttpRequestResponseTrigger *trigger)
void encode_json_(Ts... x, JsonObject root)
Helper class to easily give an object a parent of type T.