From 84e83f2dbb2945e10f7a7ad40e65b53462c8fb47 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Mon, 29 Apr 2024 14:21:28 +0200 Subject: [PATCH] adopt WebApiClass::parseRequestData() method saves redundant code, reducing flash usage. --- src/WebApi_Huawei.cpp | 63 +++++-------------------------------- src/WebApi_battery.cpp | 31 ++---------------- src/WebApi_powerlimiter.cpp | 27 ++-------------- src/WebApi_powermeter.cpp | 56 ++++----------------------------- src/WebApi_vedirect.cpp | 31 ++---------------- 5 files changed, 22 insertions(+), 186 deletions(-) diff --git a/src/WebApi_Huawei.cpp b/src/WebApi_Huawei.cpp index e1e32a5e3..8bef19ff4 100644 --- a/src/WebApi_Huawei.cpp +++ b/src/WebApi_Huawei.cpp @@ -72,40 +72,16 @@ void WebApiHuaweiClass::onPost(AsyncWebServerRequest* request) } AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - retMsg["code"] = WebApiError::GenericNoValueFound; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 1024) { - retMsg["message"] = "Data too large!"; - retMsg["code"] = WebApiError::GenericDataTooLarge; - response->setLength(); - request->send(response); + JsonDocument root; + if (!WebApi.parseRequestData(request, response, root)) { return; } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); float value; uint8_t online = true; float minimal_voltage; - if (error) { - retMsg["message"] = "Failed to parse data!"; - retMsg["code"] = WebApiError::GenericParseError; - response->setLength(); - request->send(response); - return; - } + auto& retMsg = response->getRoot(); if (root.containsKey("online")) { online = root["online"].as(); @@ -203,40 +179,15 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request) if (!WebApi.checkCredentials(request)) { return; } - - AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - retMsg["code"] = WebApiError::GenericNoValueFound; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 1024) { - retMsg["message"] = "Data too large!"; - retMsg["code"] = WebApiError::GenericDataTooLarge; - response->setLength(); - request->send(response); - return; - } + AsyncJsonResponse* response = new AsyncJsonResponse(); JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - retMsg["code"] = WebApiError::GenericParseError; - response->setLength(); - request->send(response); + if (!WebApi.parseRequestData(request, response, root)) { return; } + auto& retMsg = response->getRoot(); + if (!(root.containsKey("enabled")) || !(root.containsKey("can_controller_frequency")) || !(root.containsKey("auto_power_enabled")) || diff --git a/src/WebApi_battery.cpp b/src/WebApi_battery.cpp index 1076ec229..798957d3b 100644 --- a/src/WebApi_battery.cpp +++ b/src/WebApi_battery.cpp @@ -59,38 +59,13 @@ void WebApiBatteryClass::onAdminPost(AsyncWebServerRequest* request) } AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - retMsg["code"] = WebApiError::GenericNoValueFound; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 1024) { - retMsg["message"] = "Data too large!"; - retMsg["code"] = WebApiError::GenericDataTooLarge; - response->setLength(); - request->send(response); - return; - } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - retMsg["code"] = WebApiError::GenericParseError; - response->setLength(); - request->send(response); + if (!WebApi.parseRequestData(request, response, root)) { return; } + auto& retMsg = response->getRoot(); + if (!root.containsKey("enabled") || !root.containsKey("provider")) { retMsg["message"] = "Values are missing!"; retMsg["code"] = WebApiError::GenericValueMissing; diff --git a/src/WebApi_powerlimiter.cpp b/src/WebApi_powerlimiter.cpp index 1b72d48dc..114ced773 100644 --- a/src/WebApi_powerlimiter.cpp +++ b/src/WebApi_powerlimiter.cpp @@ -122,34 +122,13 @@ void WebApiPowerLimiterClass::onAdminPost(AsyncWebServerRequest* request) } AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 1024) { - retMsg["message"] = "Data too large!"; - response->setLength(); - request->send(response); - return; - } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__); + if (!WebApi.parseRequestData(request, response, root)) { return; } + auto& retMsg = response->getRoot(); + // we were not actually checking for all the keys we (unconditionally) // access below for a long time, and it is technically not needed if users // use the web application to submit settings. the web app will always diff --git a/src/WebApi_powermeter.cpp b/src/WebApi_powermeter.cpp index c10b0274d..8ca492b01 100644 --- a/src/WebApi_powermeter.cpp +++ b/src/WebApi_powermeter.cpp @@ -98,35 +98,13 @@ void WebApiPowerMeterClass::onAdminPost(AsyncWebServerRequest* request) } AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 4096) { - retMsg["message"] = "Data too large!"; - response->setLength(); - request->send(response); - return; - } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - response->setLength(); - request->send(response); + if (!WebApi.parseRequestData(request, response, root)) { return; } + auto& retMsg = response->getRoot(); + if (!(root.containsKey("enabled") && root.containsKey("source"))) { retMsg["message"] = "Values are missing!"; response->setLength(); @@ -217,35 +195,13 @@ void WebApiPowerMeterClass::onTestHttpRequest(AsyncWebServerRequest* request) } AsyncJsonResponse* asyncJsonResponse = new AsyncJsonResponse(); - auto& retMsg = asyncJsonResponse->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - asyncJsonResponse->setLength(); - request->send(asyncJsonResponse); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 2048) { - retMsg["message"] = "Data too large!"; - asyncJsonResponse->setLength(); - request->send(asyncJsonResponse); - return; - } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - asyncJsonResponse->setLength(); - request->send(asyncJsonResponse); + if (!WebApi.parseRequestData(request, asyncJsonResponse, root)) { return; } + auto& retMsg = asyncJsonResponse->getRoot(); + if (!root.containsKey("url") || !root.containsKey("auth_type") || !root.containsKey("username") || !root.containsKey("password") || !root.containsKey("header_key") || !root.containsKey("header_value") || !root.containsKey("timeout") || !root.containsKey("json_path")) { diff --git a/src/WebApi_vedirect.cpp b/src/WebApi_vedirect.cpp index c1e0c8349..2499ebed3 100644 --- a/src/WebApi_vedirect.cpp +++ b/src/WebApi_vedirect.cpp @@ -66,38 +66,13 @@ void WebApiVedirectClass::onVedirectAdminPost(AsyncWebServerRequest* request) } AsyncJsonResponse* response = new AsyncJsonResponse(); - auto& retMsg = response->getRoot(); - retMsg["type"] = "warning"; - - if (!request->hasParam("data", true)) { - retMsg["message"] = "No values found!"; - retMsg["code"] = WebApiError::GenericNoValueFound; - response->setLength(); - request->send(response); - return; - } - - String json = request->getParam("data", true)->value(); - - if (json.length() > 1024) { - retMsg["message"] = "Data too large!"; - retMsg["code"] = WebApiError::GenericDataTooLarge; - response->setLength(); - request->send(response); - return; - } - JsonDocument root; - DeserializationError error = deserializeJson(root, json); - - if (error) { - retMsg["message"] = "Failed to parse data!"; - retMsg["code"] = WebApiError::GenericParseError; - response->setLength(); - request->send(response); + if (!WebApi.parseRequestData(request, response, root)) { return; } + auto& retMsg = response->getRoot(); + if (!root.containsKey("vedirect_enabled") || !root.containsKey("verbose_logging") || !root.containsKey("vedirect_updatesonly") ) {