19namespace http_request {
83class HttpRequestComponent;
92 virtual int read(uint8_t *buf,
size_t max_len) = 0;
93 virtual void end() = 0;
116 void process(std::shared_ptr<HttpContainer> container, std::string &response_body) {
117 this->
trigger(std::move(container), response_body);
133 std::shared_ptr<HttpContainer>
get(
const std::string &url) {
return this->
start(url,
"GET",
"", {}); }
134 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::list<Header> &request_headers) {
135 return this->
start(url,
"GET",
"", request_headers);
137 std::shared_ptr<HttpContainer>
get(
const std::string &url,
const std::list<Header> &request_headers,
138 const std::set<std::string> &collect_headers) {
139 return this->
start(url,
"GET",
"", request_headers, collect_headers);
141 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body) {
142 return this->
start(url,
"POST", body, {});
144 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
145 const std::list<Header> &request_headers) {
146 return this->
start(url,
"POST", body, request_headers);
148 std::shared_ptr<HttpContainer>
post(
const std::string &url,
const std::string &body,
149 const std::list<Header> &request_headers,
150 const std::set<std::string> &collect_headers) {
151 return this->
start(url,
"POST", body, request_headers, collect_headers);
154 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
155 const std::list<Header> &request_headers) {
156 return this->
start(url, method, body, request_headers, {});
159 std::shared_ptr<HttpContainer>
start(
const std::string &url,
const std::string &method,
const std::string &body,
160 const std::list<Header> &request_headers,
161 const std::set<std::string> &collect_headers) {
162 std::set<std::string> lower_case_collect_headers;
163 for (
const std::string &collect_header : collect_headers) {
164 lower_case_collect_headers.insert(
str_lower_case(collect_header));
166 return this->
perform(url, method, body, request_headers, lower_case_collect_headers);
170 virtual std::shared_ptr<HttpContainer>
perform(std::string url, std::string method, std::string body,
171 std::list<Header> request_headers,
172 std::set<std::string> collect_headers) = 0;
208 if (this->body_.has_value()) {
209 body = this->body_.value(
x...);
211 if (!this->
json_.empty()) {
219 std::list<Header> request_headers;
221 auto val = item.second;
223 header.
name = item.first;
225 request_headers.push_back(header);
228 auto container = this->
parent_->
start(this->url_.value(
x...), this->method_.value(
x...), body, request_headers,
229 this->collect_headers_);
231 if (container ==
nullptr) {
237 size_t content_length = container->content_length;
240 std::string response_body;
241 if (this->capture_response_.value(
x...)) {
243 uint8_t *buf = allocator.
allocate(max_length);
244 if (buf !=
nullptr) {
245 size_t read_index = 0;
246 while (container->get_bytes_read() < max_length) {
247 int read = container->read(buf + read_index, std::min<size_t>(max_length - read_index, 512));
252 response_body.reserve(read_index);
253 response_body.assign((
char *) buf, read_index);
266 auto response_body_copy = std::string(response_body);
267 trigger->process(container, response_body_copy);
275 for (
const auto &item : this->
json_) {
276 auto val = item.second;
277 root[item.first] =
val.value(
x...);
void feed_wdt(uint32_t time=0)
Helper class to easily give an object a parent of type T.
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
std::string get_response_header(const std::string &header_name)
std::map< std::string, std::list< std::string > > get_response_headers()
Get response headers.
virtual int read(uint8_t *buf, size_t max_len)=0
virtual ~HttpContainer()=default
std::map< std::string, std::list< std::string > > response_headers_
size_t get_bytes_read() const
void set_secure(bool secure)
virtual std::shared_ptr< HttpContainer > perform(std::string url, std::string method, std::string body, std::list< Header > request_headers, std::set< std::string > collect_headers)=0
float get_setup_priority() const override
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
uint32_t get_watchdog_timeout() const
void set_timeout(uint16_t timeout)
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body)
std::shared_ptr< HttpContainer > post(const std::string &url, const std::string &body, const std::list< Header > &request_headers)
void set_useragent(const char *useragent)
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
std::shared_ptr< HttpContainer > get(const std::string &url, const std::list< Header > &request_headers)
uint32_t watchdog_timeout_
void set_follow_redirects(bool follow_redirects)
std::shared_ptr< HttpContainer > start(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers)
void set_redirect_limit(uint16_t limit)
void set_watchdog_timeout(uint32_t watchdog_timeout)
std::shared_ptr< HttpContainer > get(const std::string &url, const std::list< Header > &request_headers, const std::set< std::string > &collect_headers)
std::shared_ptr< HttpContainer > get(const std::string &url)
void dump_config() override
void process(std::shared_ptr< HttpContainer > container, std::string &response_body)
void play(Ts... x) override
void set_max_response_buffer_size(size_t max_response_buffer_size)
void register_error_trigger(Trigger<> *trigger)
std::set< std::string > collect_headers_
HttpRequestComponent * parent_
std::map< const char *, TemplatableValue< std::string, Ts... > > json_
method capture_response void add_request_header(const char *key, TemplatableValue< const char *, Ts... > value)
TEMPLATABLE_VALUE(std::string, url) TEMPLATABLE_VALUE(const char *
void add_collect_header(const char *value)
std::vector< HttpRequestResponseTrigger * > response_triggers_
void register_response_trigger(HttpRequestResponseTrigger *trigger)
std::function< void(Ts..., JsonObject)> json_func_
void encode_json_func_(Ts... x, JsonObject root)
std::vector< Trigger<> * > error_triggers_
void set_json(std::function< void(Ts..., JsonObject)> json_func)
void add_json(const char *key, TemplatableValue< std::string, Ts... > value)
void encode_json_(Ts... x, JsonObject root)
size_t max_response_buffer_size_
std::map< const char *, TemplatableValue< const char *, Ts... > > request_headers_
HttpRequestSendAction(HttpRequestComponent *parent)
@ HTTP_STATUS_PARTIAL_CONTENT
@ HTTP_STATUS_LENGTH_REQUIRED
@ HTTP_STATUS_PERMANENT_REDIRECT
@ HTTP_STATUS_METHOD_NOT_ALLOWED
@ HTTP_STATUS_NOT_MODIFIED
@ HTTP_STATUS_INTERNAL_ERROR
@ HTTP_STATUS_UNAUTHORIZED
@ HTTP_STATUS_MULTIPLE_CHOICES
@ HTTP_STATUS_NOT_ACCEPTABLE
@ HTTP_STATUS_TEMPORARY_REDIRECT
@ HTTP_STATUS_BAD_REQUEST
@ HTTP_STATUS_MOVED_PERMANENTLY
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
bool is_redirect(int const status)
Returns true if the HTTP status code is a redirect.
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Providing packet encoding functions for exchanging data with a remote host.
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
void IRAM_ATTR HOT yield()
Application App
Global storage of Application pointer - only one Application can exist.