From 6ee9100fdc885347e90a168ff3b5f5ce756ad325 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 9 Jul 2024 20:07:06 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=84=20Formats=20HttpEndpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/lib/ESP32-sveltekit/HttpEndpoint.h | 70 ++++++++++-------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/esp32/lib/ESP32-sveltekit/HttpEndpoint.h b/esp32/lib/ESP32-sveltekit/HttpEndpoint.h index ccf66ea..2d923c6 100644 --- a/esp32/lib/ESP32-sveltekit/HttpEndpoint.h +++ b/esp32/lib/ESP32-sveltekit/HttpEndpoint.h @@ -12,7 +12,7 @@ template class HttpEndpoint { - protected: + protected: JsonStateReader _stateReader; JsonStateUpdater _stateUpdater; StatefulService *_statefulService; @@ -22,13 +22,10 @@ class HttpEndpoint { PsychicHttpServer *_server; const char *_servicePath; - public: - HttpEndpoint(JsonStateReader stateReader, - JsonStateUpdater stateUpdater, - StatefulService *statefulService, PsychicHttpServer *server, - const char *servicePath, SecurityManager *securityManager, - AuthenticationPredicate authenticationPredicate = - AuthenticationPredicates::IS_ADMIN) + public: + HttpEndpoint(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService *statefulService, + PsychicHttpServer *server, const char *servicePath, SecurityManager *securityManager, + AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN) : _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService), @@ -41,19 +38,16 @@ class HttpEndpoint { void begin() { // OPTIONS (for CORS preflight) #ifdef ENABLE_CORS - _server->on( - _servicePath, HTTP_OPTIONS, - _securityManager->wrapRequest( - [this](PsychicRequest *request) { return request->reply(200); }, - AuthenticationPredicates::IS_AUTHENTICATED)); + _server->on(_servicePath, HTTP_OPTIONS, + _securityManager->wrapRequest([this](PsychicRequest *request) { return request->reply(200); }, + AuthenticationPredicates::IS_AUTHENTICATED)); #endif // GET _server->on(_servicePath, HTTP_GET, _securityManager->wrapRequest( [this](PsychicRequest *request) { - PsychicJsonResponse response = - PsychicJsonResponse(request, false); + PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject jsonObject = response.getRoot(); _statefulService->read(jsonObject, _stateReader); return response.send(); @@ -62,36 +56,32 @@ class HttpEndpoint { ESP_LOGV("HttpEndpoint", "Registered GET endpoint: %s", _servicePath); // POST - _server->on( - _servicePath, HTTP_POST, - _securityManager->wrapCallback( - [this](PsychicRequest *request, JsonVariant &json) { - if (!json.is()) { - return request->reply(400); - } + _server->on(_servicePath, HTTP_POST, + _securityManager->wrapCallback( + [this](PsychicRequest *request, JsonVariant &json) { + if (!json.is()) { + return request->reply(400); + } - JsonObject jsonObject = json.as(); - StateUpdateResult outcome = - _statefulService->updateWithoutPropagation( - jsonObject, _stateUpdater); + JsonObject jsonObject = json.as(); + StateUpdateResult outcome = + _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater); - if (outcome == StateUpdateResult::ERROR) { - return request->reply(400); - } else if ((outcome == StateUpdateResult::CHANGED)) { - // persist the changes to the FS - _statefulService->callUpdateHandlers( - HTTP_ENDPOINT_ORIGIN_ID); - } + if (outcome == StateUpdateResult::ERROR) { + return request->reply(400); + } else if ((outcome == StateUpdateResult::CHANGED)) { + // persist the changes to the FS + _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); + } - PsychicJsonResponse response = - PsychicJsonResponse(request, false); - jsonObject = response.getRoot(); + PsychicJsonResponse response = PsychicJsonResponse(request, false); + jsonObject = response.getRoot(); - _statefulService->read(jsonObject, _stateReader); + _statefulService->read(jsonObject, _stateReader); - return response.send(); - }, - _authenticationPredicate)); + return response.send(); + }, + _authenticationPredicate)); ESP_LOGV("HttpEndpoint", "Registered POST endpoint: %s", _servicePath); }