18 namespace http_request {
91 virtual int read(uint8_t *buf,
size_t max_len) = 0;
92 virtual void end() = 0;
99 size_t bytes_read_{0};
105 void process(std::shared_ptr<HttpContainer> container, std::string &response_body) {
106 this->trigger(std::move(container), response_body);
112 void dump_config()
override;
122 std::shared_ptr<HttpContainer>
get(std::string url) {
return this->start(std::move(url),
"GET",
"", {}); }
123 std::shared_ptr<HttpContainer>
get(std::string url, std::list<Header> headers) {
124 return this->start(std::move(url),
"GET",
"", std::move(headers));
126 std::shared_ptr<HttpContainer>
post(std::string url, std::string body) {
127 return this->start(std::move(url),
"POST", std::move(body), {});
129 std::shared_ptr<HttpContainer>
post(std::string url, std::string body, std::list<Header> headers) {
130 return this->start(std::move(url),
"POST", std::move(body), std::move(headers));
133 virtual std::shared_ptr<HttpContainer> start(std::string url, std::string method, std::string body,
134 std::list<Header> headers) = 0;
137 const char *useragent_{
nullptr};
138 bool follow_redirects_{};
139 uint16_t redirect_limit_{};
140 uint16_t timeout_{4500};
141 uint32_t watchdog_timeout_{0};
147 TEMPLATABLE_VALUE(std::string, url)
148 TEMPLATABLE_VALUE(
const char *, method)
149 TEMPLATABLE_VALUE(std::string, body)
150 TEMPLATABLE_VALUE(
bool, capture_response)
152 void add_header(const
char *key,
TemplatableValue<const
char *, Ts...>
value) { this->headers_.insert({key, value}); }
156 void set_json(std::function<
void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
163 this->max_response_buffer_size_ = max_response_buffer_size;
168 if (this->body_.has_value()) {
169 body = this->body_.value(
x...);
171 if (!this->json_.empty()) {
175 if (this->json_func_ !=
nullptr) {
179 std::list<Header> headers;
180 for (
const auto &item : this->headers_) {
181 auto val = item.second;
183 header.
name = item.first;
185 headers.push_back(header);
188 auto container = this->parent_->start(this->url_.value(
x...), this->method_.value(
x...), body, headers);
190 if (container ==
nullptr) {
191 for (
auto *trigger : this->error_triggers_)
192 trigger->trigger(
x...);
196 size_t content_length = container->content_length;
197 size_t max_length = std::min(content_length, this->max_response_buffer_size_);
199 std::string response_body;
200 if (this->capture_response_.value(
x...)) {
202 uint8_t *buf = allocator.
allocate(max_length);
203 if (buf !=
nullptr) {
204 size_t read_index = 0;
205 while (container->get_bytes_read() < max_length) {
206 int read = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
211 response_body.reserve(read_index);
212 response_body.assign((
char *) buf, read_index);
217 if (this->response_triggers_.size() == 1) {
219 this->response_triggers_[0]->process(container, response_body);
221 for (
auto *trigger : this->response_triggers_) {
225 auto response_body_copy = std::string(response_body);
226 trigger->process(container, response_body_copy);
234 for (
const auto &item : this->json_) {
235 auto val = item.second;
236 root[item.first] =
val.value(
x...);
243 std::function<void(Ts..., JsonObject)> json_func_{
nullptr};
244 std::vector<HttpRequestResponseTrigger *> response_triggers_{};
245 std::vector<Trigger<> *> error_triggers_{};
247 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 register_error_trigger(Trigger<> *trigger)
bool is_redirect(int const status)
Returns true if the HTTP status code is a redirect.
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
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
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::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
void deallocate(T *p, size_t n)
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)
An STL allocator that uses SPI or internal RAM.
Helper class to easily give an object a parent of type T.