12 const std::shared_ptr<ConfigItemBase>& item) {
13 JsonObject obj = doc.to<JsonObject>();
16 serializeJson(doc, str);
18 obj[
"path"] = item->get_config_path();
19 obj[
"title"] = item->get_title();
20 obj[
"description"] = item->get_description();
21 obj[
"requires_restart"] = item->requires_restart();
22 obj[
"schema"] = serialized(item->get_config_schema());
26 JsonObject config = obj[
"config"].to<JsonObject>();
27 bool result = item->to_json(config);
29 serializeJson(obj, str);
31 if (doc.overflowed()) {
32 ESP_LOGE(
"ConfigHandler",
"JSON document overflowed");
40 ESP_LOGI(
"ConfigHandler",
"GET request to URL %s", req->uri);
41 String url = String(req->uri);
43 if (url.indexOf(
'?') != -1) {
44 query = url.substring(url.indexOf(
'?') + 1);
47 bool cards_only =
false;
50 if (query ==
"cards") {
52 }
else if (query !=
"") {
53 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
"Invalid query");
57 JsonDocument json_doc;
58 JsonArray arr = json_doc.to<JsonArray>();
62 for (
auto it = config_items->begin(); it != config_items->end(); ++it) {
64 ((*it)->get_config_schema() ==
"null" ||
65 (*it)->get_config_schema() ==
"{}" || (*it)->get_title() ==
"")) {
68 const String& path = (*it)->get_config_path();
72 auto obj = arr.add(path);
76 serializeJson(json_doc, response);
77 httpd_resp_set_type(req,
"application/json");
78 httpd_resp_sendstr(req, response.c_str());
89 auto handler = std::make_shared<HTTPRequestHandler>(
90 1 << HTTP_GET,
"/api/config/*", [](httpd_req_t* req) {
91 ESP_LOGD(
"ConfigHandler",
"GET request to URL %s", req->uri);
92 String url_tail = String(req->uri).substring(11);
95 if (url_tail.indexOf(
'?') != -1) {
96 path = url_tail.substring(0, url_tail.indexOf(
'?'));
97 query = url_tail.substring(url_tail.indexOf(
'?') + 1);
101 char path_cstr[path.length() + 1];
103 url_tail = String(path_cstr);
105 if (path.length() == 0) {
112 if (config_item ==
nullptr) {
113 httpd_resp_send_err(req, HTTPD_404_NOT_FOUND,
114 "No ConfigItem found with that path");
123 serializeJson(doc, response);
124 httpd_resp_set_type(req,
"application/json");
125 httpd_resp_sendstr(req, response.c_str());
128 server->add_handler(handler);
132 auto handler = std::make_shared<HTTPRequestHandler>(
133 1 << HTTP_PUT,
"/api/config/*",
134 [](httpd_req_t* req) {
135 ESP_LOGI(__FILENAME__,
"PUT request to URL %s", req->uri);
137 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
138 "application/json content type expected");
143 String url_tail = String(req->uri).substring(11);
144 if (url_tail.length() == 0) {
145 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
146 "No configuration path specified");
151 char url_tail_cstr[url_tail.length() + 1];
153 url_tail = String(url_tail_cstr);
157 if (config_item ==
nullptr) {
158 httpd_resp_send_err(req, HTTPD_404_NOT_FOUND,
159 "No Configurable found with that path");
164 size_t payload_len = req->content_len;
165 char* payload =
new char[payload_len + 1];
166 int ret = httpd_req_recv(req, payload, payload_len);
168 httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
169 "Error receiving payload");
172 payload[payload_len] =
'\0';
174 ESP_LOGV(
"ConfigHandler",
"Received payload: %s", payload);
178 DeserializationError error = deserializeJson(doc, payload);
181 ESP_LOGE(
"ConfigHandler",
"Error parsing JSON payload: %s",
183 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
184 "Error parsing JSON payload");
189 bool result = config_item->from_json(doc.as<JsonObject>());
191 ESP_LOGE(
"ConfigHandler",
"Error applying JSON payload");
192 httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
193 "Invalid JSON payload");
197 response =
"{\"status\":\"ok\"}";
199 httpd_resp_set_type(req,
"application/json");
200 httpd_resp_sendstr(req, response.c_str());
204 server->add_handler(handler);