Skip to content

Commit

Permalink
use GET for software/latest
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Aug 5, 2024
1 parent 512eb22 commit 9a7977b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
102 changes: 68 additions & 34 deletions noisemeter-device/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::optional<JsonDocument> API::sendAuthorizedRequest(const API::Request& req)
if (https.begin(client, req.url)) {
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
https.addHeader("Authorization", String("Token ") + token);
return sendHttpRequest(https, req.params.substring(1));
return sendHttpPOST(https, req.params.substring(1));
#ifdef API_VERBOSE
} else {
SERIAL.println("[api] Failed to https.begin()");
Expand All @@ -78,7 +78,7 @@ std::optional<JsonDocument> API::sendNonauthorizedRequest(const API::Request& re
HTTPClient https;
if (https.begin(client, req.url)) {
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
return sendHttpRequest(https, req.params.substring(1));
return sendHttpPOST(https, req.params.substring(1));
#ifdef API_VERBOSE
} else {
SERIAL.println("[api] Failed to https.begin()");
Expand All @@ -88,41 +88,57 @@ std::optional<JsonDocument> API::sendNonauthorizedRequest(const API::Request& re
return {};
}

std::optional<JsonDocument> API::sendHttpRequest(HTTPClient& https, const String& payload)
std::optional<JsonDocument> API::sendHttpGET(HTTPClient& https)
{
const auto code = https.GET();
if (code == HTTP_CODE_OK || code == HTTP_CODE_MOVED_PERMANENTLY) {
return handleHttpResponse(https);
#ifdef API_VERBOSE
} else {
SERIAL.print("[api] HTTP error: ");
SERIAL.println(code);
return {};
#endif
}
}

std::optional<JsonDocument> API::sendHttpPOST(HTTPClient& https, const String& payload)
{
#ifdef API_VERBOSE
SERIAL.print("[api] payload: ");
SERIAL.println(payload);
#endif
if (const auto code = https.POST(payload); code > 0) {
const auto response = https.getString();
const auto json = responseToJson(response);
https.end();

if (json) {
SERIAL.print("[api] ");
SERIAL.print((String)(*json)["result"]);
SERIAL.print(": ");
SERIAL.println((String)(*json)["message"]);

if (code == HTTP_CODE_OK || code == HTTP_CODE_MOVED_PERMANENTLY) {
return *json;

const auto code = https.POST(payload);
if (code == HTTP_CODE_OK || code == HTTP_CODE_MOVED_PERMANENTLY) {
return handleHttpResponse(https);
#ifdef API_VERBOSE
} else {
SERIAL.print("[api] HTTP error: ");
SERIAL.println(code);
} else {
SERIAL.print("[api] HTTP error: ");
SERIAL.println(code);
return {};
#endif
}
}
}

std::optional<JsonDocument> API::handleHttpResponse(HTTPClient& https)
{
const auto response = https.getString();
const auto json = responseToJson(response);
https.end();

if (json) {
#ifdef API_VERBOSE
} else {
SERIAL.print("[api] Invalid JSON! HTTP error: ");
SERIAL.println(code);
SERIAL.print("[api] ");
SERIAL.print((String)(*json)["result"]);
SERIAL.print(": ");
SERIAL.println((String)(*json)["message"]);
#endif
}

return *json;
#ifdef API_VERBOSE
} else {
SERIAL.print("[api] HTTP error: ");
SERIAL.println(code);
SERIAL.print("[api] Invalid JSON!");
#endif
}

Expand Down Expand Up @@ -190,17 +206,35 @@ std::optional<API::LatestSoftware> API::getLatestSoftware()
{
const auto request = Request("software/latest");

const auto resp = sendNonauthorizedRequest(request);
if (resp && (*resp)["result"] == "ok") {
LatestSoftware ls = {
(*resp)["version"],
(*resp)["url"]
};
WiFiClientSecure client;
client.setCACert(rootCertificate());

String endpoint = request.url + '?' + request.params.substring(1);

#ifdef API_VERBOSE
SERIAL.print("[api] Non-authorized request: ");
SERIAL.println(endpoint);
#endif

return ls;
HTTPClient https;
if (https.begin(client, endpoint)) {
const auto resp = sendHttpGET(https);

if (resp && (*resp)["result"] == "ok") {
LatestSoftware ls = {
(*resp)["version"],
(*resp)["url"]
};

return ls;
}
#ifdef API_VERBOSE
} else {
return {};
SERIAL.println("[api] Failed to https.begin()");
#endif
}

return {};
}

const char *API::rootCertificate()
Expand Down
4 changes: 3 additions & 1 deletion noisemeter-device/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ class API
/** Attempts the given request and returns the JSON response on success. */
std::optional<JsonDocument> sendNonauthorizedRequest(const Request& req);

std::optional<JsonDocument> sendHttpRequest(HTTPClient& https, const String& payload);
std::optional<JsonDocument> sendHttpGET(HTTPClient& https);
std::optional<JsonDocument> sendHttpPOST(HTTPClient& https, const String& payload);
std::optional<JsonDocument> handleHttpResponse(HTTPClient& https);
};

#endif // API_H
Expand Down

0 comments on commit 9a7977b

Please sign in to comment.