Skip to content

Commit

Permalink
adopt WebApiClass::parseRequestData() method
Browse files Browse the repository at this point in the history
saves redundant code, reducing flash usage.
  • Loading branch information
schlimmchen committed Apr 29, 2024
1 parent fdc5054 commit 84e83f2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 186 deletions.
63 changes: 7 additions & 56 deletions src/WebApi_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>();
Expand Down Expand Up @@ -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")) ||
Expand Down
31 changes: 3 additions & 28 deletions src/WebApi_battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 3 additions & 24 deletions src/WebApi_powerlimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 6 additions & 50 deletions src/WebApi_powermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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")) {
Expand Down
31 changes: 3 additions & 28 deletions src/WebApi_vedirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") ) {
Expand Down

0 comments on commit 84e83f2

Please sign in to comment.