🪄 Formats HttpEndpoint
This commit is contained in:
@@ -23,12 +23,9 @@ class HttpEndpoint {
|
|||||||
const char *_servicePath;
|
const char *_servicePath;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HttpEndpoint(JsonStateReader<T> stateReader,
|
HttpEndpoint(JsonStateReader<T> stateReader, JsonStateUpdater<T> stateUpdater, StatefulService<T> *statefulService,
|
||||||
JsonStateUpdater<T> stateUpdater,
|
PsychicHttpServer *server, const char *servicePath, SecurityManager *securityManager,
|
||||||
StatefulService<T> *statefulService, PsychicHttpServer *server,
|
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN)
|
||||||
const char *servicePath, SecurityManager *securityManager,
|
|
||||||
AuthenticationPredicate authenticationPredicate =
|
|
||||||
AuthenticationPredicates::IS_ADMIN)
|
|
||||||
: _stateReader(stateReader),
|
: _stateReader(stateReader),
|
||||||
_stateUpdater(stateUpdater),
|
_stateUpdater(stateUpdater),
|
||||||
_statefulService(statefulService),
|
_statefulService(statefulService),
|
||||||
@@ -41,10 +38,8 @@ class HttpEndpoint {
|
|||||||
void begin() {
|
void begin() {
|
||||||
// OPTIONS (for CORS preflight)
|
// OPTIONS (for CORS preflight)
|
||||||
#ifdef ENABLE_CORS
|
#ifdef ENABLE_CORS
|
||||||
_server->on(
|
_server->on(_servicePath, HTTP_OPTIONS,
|
||||||
_servicePath, HTTP_OPTIONS,
|
_securityManager->wrapRequest([this](PsychicRequest *request) { return request->reply(200); },
|
||||||
_securityManager->wrapRequest(
|
|
||||||
[this](PsychicRequest *request) { return request->reply(200); },
|
|
||||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
AuthenticationPredicates::IS_AUTHENTICATED));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -52,8 +47,7 @@ class HttpEndpoint {
|
|||||||
_server->on(_servicePath, HTTP_GET,
|
_server->on(_servicePath, HTTP_GET,
|
||||||
_securityManager->wrapRequest(
|
_securityManager->wrapRequest(
|
||||||
[this](PsychicRequest *request) {
|
[this](PsychicRequest *request) {
|
||||||
PsychicJsonResponse response =
|
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||||
PsychicJsonResponse(request, false);
|
|
||||||
JsonObject jsonObject = response.getRoot();
|
JsonObject jsonObject = response.getRoot();
|
||||||
_statefulService->read(jsonObject, _stateReader);
|
_statefulService->read(jsonObject, _stateReader);
|
||||||
return response.send();
|
return response.send();
|
||||||
@@ -62,8 +56,7 @@ class HttpEndpoint {
|
|||||||
ESP_LOGV("HttpEndpoint", "Registered GET endpoint: %s", _servicePath);
|
ESP_LOGV("HttpEndpoint", "Registered GET endpoint: %s", _servicePath);
|
||||||
|
|
||||||
// POST
|
// POST
|
||||||
_server->on(
|
_server->on(_servicePath, HTTP_POST,
|
||||||
_servicePath, HTTP_POST,
|
|
||||||
_securityManager->wrapCallback(
|
_securityManager->wrapCallback(
|
||||||
[this](PsychicRequest *request, JsonVariant &json) {
|
[this](PsychicRequest *request, JsonVariant &json) {
|
||||||
if (!json.is<JsonObject>()) {
|
if (!json.is<JsonObject>()) {
|
||||||
@@ -72,19 +65,16 @@ class HttpEndpoint {
|
|||||||
|
|
||||||
JsonObject jsonObject = json.as<JsonObject>();
|
JsonObject jsonObject = json.as<JsonObject>();
|
||||||
StateUpdateResult outcome =
|
StateUpdateResult outcome =
|
||||||
_statefulService->updateWithoutPropagation(
|
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||||
jsonObject, _stateUpdater);
|
|
||||||
|
|
||||||
if (outcome == StateUpdateResult::ERROR) {
|
if (outcome == StateUpdateResult::ERROR) {
|
||||||
return request->reply(400);
|
return request->reply(400);
|
||||||
} else if ((outcome == StateUpdateResult::CHANGED)) {
|
} else if ((outcome == StateUpdateResult::CHANGED)) {
|
||||||
// persist the changes to the FS
|
// persist the changes to the FS
|
||||||
_statefulService->callUpdateHandlers(
|
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID);
|
||||||
HTTP_ENDPOINT_ORIGIN_ID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicJsonResponse response =
|
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||||
PsychicJsonResponse(request, false);
|
|
||||||
jsonObject = response.getRoot();
|
jsonObject = response.getRoot();
|
||||||
|
|
||||||
_statefulService->read(jsonObject, _stateReader);
|
_statefulService->read(jsonObject, _stateReader);
|
||||||
|
|||||||
Reference in New Issue
Block a user