Upgrades ArduinoJson from version 6 to 7

This commit is contained in:
Rune Harlyk
2024-05-07 10:36:16 +02:00
committed by Rune Harlyk
parent 2b4d196e7c
commit a150caad9d
39 changed files with 241 additions and 561 deletions
+51 -60
View File
@@ -1,22 +1,18 @@
#ifndef HttpEndpoint_h
#define HttpEndpoint_h
#include <functional>
#include <PsychicHttp.h>
#include <SecurityManager.h>
#include <StatefulService.h>
#include <functional>
#define HTTP_ENDPOINT_ORIGIN_ID "http"
#define HTTPS_ENDPOINT_ORIGIN_ID "https"
using namespace std::placeholders; // for `_1` etc
template <class T>
class HttpEndpoint
{
protected:
class HttpEndpoint {
protected:
JsonStateReader<T> _stateReader;
JsonStateUpdater<T> _stateUpdater;
StatefulService<T> *_statefulService;
@@ -26,42 +22,38 @@ protected:
PsychicHttpServer *_server;
const char *_servicePath;
public:
public:
HttpEndpoint(JsonStateReader<T> stateReader,
JsonStateUpdater<T> stateUpdater,
StatefulService<T> *statefulService,
PsychicHttpServer *server,
const char *servicePath,
SecurityManager *securityManager,
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
size_t bufferSize = DEFAULT_BUFFER_SIZE)
: _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService), _server(server), _servicePath(servicePath), _securityManager(securityManager), _authenticationPredicate(authenticationPredicate), _bufferSize(bufferSize)
{
}
StatefulService<T> *statefulService, PsychicHttpServer *server,
const char *servicePath, SecurityManager *securityManager,
AuthenticationPredicate authenticationPredicate =
AuthenticationPredicates::IS_ADMIN)
: _stateReader(stateReader),
_stateUpdater(stateUpdater),
_statefulService(statefulService),
_server(server),
_servicePath(servicePath),
_securityManager(securityManager),
_authenticationPredicate(authenticationPredicate) {}
// register the web server on() endpoints
void begin()
{
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,
_server->on(_servicePath, HTTP_GET,
_securityManager->wrapRequest(
[this](PsychicRequest *request)
{
PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize);
[this](PsychicRequest *request) {
PsychicJsonResponse response =
PsychicJsonResponse(request, false);
JsonObject jsonObject = response.getRoot();
_statefulService->read(jsonObject, _stateReader);
return response.send();
@@ -70,37 +62,36 @@ public:
ESP_LOGV("HttpEndpoint", "Registered GET endpoint: %s", _servicePath);
// POST
_server->on(_servicePath,
HTTP_POST,
_securityManager->wrapCallback(
[this](PsychicRequest *request, JsonVariant &json)
{
if (!json.is<JsonObject>())
{
return request->reply(400);
}
_server->on(
_servicePath, HTTP_POST,
_securityManager->wrapCallback(
[this](PsychicRequest *request, JsonVariant &json) {
if (!json.is<JsonObject>()) {
return request->reply(400);
}
JsonObject jsonObject = json.as<JsonObject>();
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
JsonObject jsonObject = json.as<JsonObject>();
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, _bufferSize);
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);
}