mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 06:58:11 +01:00
Improve 'body' handling in http_request on_response triggers (#6968)
This commit is contained in:
parent
d8a5c1ea0c
commit
0179358f9c
4 changed files with 17 additions and 6 deletions
|
@ -60,6 +60,7 @@ from esphome.cpp_types import ( # noqa
|
||||||
std_ns,
|
std_ns,
|
||||||
std_shared_ptr,
|
std_shared_ptr,
|
||||||
std_string,
|
std_string,
|
||||||
|
std_string_ref,
|
||||||
std_vector,
|
std_vector,
|
||||||
uint8,
|
uint8,
|
||||||
uint16,
|
uint16,
|
||||||
|
|
|
@ -257,7 +257,7 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
|
||||||
trigger,
|
trigger,
|
||||||
[
|
[
|
||||||
(cg.std_shared_ptr.template(HttpContainer), "response"),
|
(cg.std_shared_ptr.template(HttpContainer), "response"),
|
||||||
(cg.std_string, "body"),
|
(cg.std_string_ref, "body"),
|
||||||
],
|
],
|
||||||
conf,
|
conf,
|
||||||
)
|
)
|
||||||
|
|
|
@ -43,10 +43,10 @@ class HttpContainer : public Parented<HttpRequestComponent> {
|
||||||
bool secure_{false};
|
bool secure_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
class HttpRequestResponseTrigger : public Trigger<std::shared_ptr<HttpContainer>, std::string> {
|
class HttpRequestResponseTrigger : public Trigger<std::shared_ptr<HttpContainer>, std::string &> {
|
||||||
public:
|
public:
|
||||||
void process(std::shared_ptr<HttpContainer> container, std::string response_body) {
|
void process(std::shared_ptr<HttpContainer> container, std::string &response_body) {
|
||||||
this->trigger(std::move(container), std::move(response_body));
|
this->trigger(std::move(container), response_body);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -153,8 +153,17 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto *trigger : this->response_triggers_) {
|
if (this->response_triggers_.size() == 1) {
|
||||||
trigger->process(container, response_body);
|
// if there is only one trigger, no need to copy the response body
|
||||||
|
this->response_triggers_[0]->process(container, response_body);
|
||||||
|
} else {
|
||||||
|
for (auto *trigger : this->response_triggers_) {
|
||||||
|
// with multiple triggers, pass a copy of the response body to each
|
||||||
|
// one so that modifications made in one trigger are not visible to
|
||||||
|
// the others
|
||||||
|
auto response_body_copy = std::string(response_body);
|
||||||
|
trigger->process(container, response_body_copy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
container->end();
|
container->end();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ int_ = global_ns.namespace("int")
|
||||||
std_ns = global_ns.namespace("std")
|
std_ns = global_ns.namespace("std")
|
||||||
std_shared_ptr = std_ns.class_("shared_ptr")
|
std_shared_ptr = std_ns.class_("shared_ptr")
|
||||||
std_string = std_ns.class_("string")
|
std_string = std_ns.class_("string")
|
||||||
|
std_string_ref = std_ns.namespace("string &")
|
||||||
std_vector = std_ns.class_("vector")
|
std_vector = std_ns.class_("vector")
|
||||||
uint8 = global_ns.namespace("uint8_t")
|
uint8 = global_ns.namespace("uint8_t")
|
||||||
uint16 = global_ns.namespace("uint16_t")
|
uint16 = global_ns.namespace("uint16_t")
|
||||||
|
|
Loading…
Reference in a new issue