refactoring

This commit is contained in:
Proddy
2024-02-12 14:21:30 +01:00
parent ed5f0bc6d5
commit 80a3007f8b
56 changed files with 555 additions and 516 deletions

View File

@@ -2,15 +2,13 @@
#define HttpEndpoint_h
#include <functional>
#include <ESPAsyncWebServer.h>
#include <SecurityManager.h>
#include <StatefulService.h>
#include "SecurityManager.h"
#include "StatefulService.h"
#define HTTP_ENDPOINT_ORIGIN_ID "http"
using namespace std::placeholders; // for `_1` etc
#define HTTPS_ENDPOINT_ORIGIN_ID "https"
template <class T>
class HttpEndpoint {
@@ -19,8 +17,7 @@ class HttpEndpoint {
JsonStateUpdater<T> _stateUpdater;
StatefulService<T> * _statefulService;
AsyncCallbackWebHandler * GEThandler;
AsyncCallbackJsonWebHandler * POSThandler;
AsyncCallbackJsonWebHandler * handler;
public:
HttpEndpoint(JsonStateReader<T> stateReader,
@@ -33,12 +30,12 @@ class HttpEndpoint {
: _stateReader(stateReader)
, _stateUpdater(stateUpdater)
, _statefulService(statefulService) {
// Create the GET and POST endpoints
POSThandler = new AsyncCallbackJsonWebHandler(servicePath,
securityManager->wrapCallback([this](AsyncWebServerRequest * request,
JsonVariant json) { handleRequest(request, json); },
authenticationPredicate));
server->addHandler(POSThandler);
// Create hander for both GET and POST endpoints
handler = new AsyncCallbackJsonWebHandler(servicePath,
securityManager->wrapCallback([this](AsyncWebServerRequest * request,
JsonVariant json) { handleRequest(request, json); },
authenticationPredicate));
server->addHandler(handler);
}
protected:
@@ -66,8 +63,8 @@ class HttpEndpoint {
}
}
AsyncJsonResponse * response = new AsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot().to<JsonObject>();
auto * response = new AsyncJsonResponse(false);
JsonObject jsonObject = response->getRoot().to<JsonObject>();
_statefulService->read(jsonObject, _stateReader);
response->setLength();
request->send(response);