online_image: nicer code style for http_request_headers

This commit is contained in:
Kjell Braden 2024-11-14 11:03:33 +01:00
parent 01c1894849
commit dad61299b8
3 changed files with 8 additions and 7 deletions

View File

@ -160,7 +160,9 @@ async def to_code(config):
for key in config.get(CONF_HTTP_REQUEST_HEADERS, []): for key in config.get(CONF_HTTP_REQUEST_HEADERS, []):
template_ = await cg.templatable( template_ = await cg.templatable(
config[CONF_HTTP_REQUEST_HEADERS][key], [], cg.optional.template(cg.const_char_ptr) config[CONF_HTTP_REQUEST_HEADERS][key],
[],
cg.optional.template(cg.const_char_ptr),
) )
cg.add(var.add_header(key, template_)) cg.add(var.add_header(key, template_))

View File

@ -103,12 +103,11 @@ void OnlineImage::update() {
ESP_LOGI(TAG, "Updating image"); ESP_LOGI(TAG, "Updating image");
} }
std::list<http_request::Header> headers; std::list<http_request::Header> headers;
for (const auto &item : this->headers_) { for (const auto &[key, value_template] : this->headers_) {
auto val = item.second(); auto val = value_template();
if (val.has_value()) { if (val.has_value()) {
headers.push_back(http_request::Header{item.first, *val}); headers.push_back(http_request::Header{key, *val});
} }
} }

View File

@ -62,7 +62,7 @@ class OnlineImage : public PollingComponent,
} }
} }
void add_header(const char *key, std::function<optional<const char*>()> &&f) { this->headers_.insert({key, f}); } void add_header(const char *key, std::function<optional<const char *>()> &&f) { this->headers_.insert({key, f}); }
/** /**
* @brief Set the image that needs to be shown as long as the downloaded image * @brief Set the image that needs to be shown as long as the downloaded image
@ -124,7 +124,7 @@ class OnlineImage : public PollingComponent,
image::Image *placeholder_{nullptr}; image::Image *placeholder_{nullptr};
std::string url_{""}; std::string url_{""};
std::map<const char *, std::function<optional<const char*>()>> headers_{}; std::map<const char *, std::function<optional<const char *>()>> headers_{};
/** width requested on configuration, or 0 if non specified. */ /** width requested on configuration, or 0 if non specified. */
const int fixed_width_; const int fixed_width_;