Skip to content

Commit eb734c7

Browse files
committed
Push WIP
1 parent 49be717 commit eb734c7

File tree

6 files changed

+48
-186
lines changed

6 files changed

+48
-186
lines changed

include/http/HTTPClient.h

Lines changed: 0 additions & 103 deletions
This file was deleted.

include/http/HTTPRequestManager.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
22

3-
#include <Arduino.h>
4-
53
#include <cJSON.h>
64

75
#include <functional>
@@ -61,11 +59,11 @@ namespace OpenShock::HTTP {
6159
using GotContentLengthCallback = std::function<bool(int contentLength)>;
6260
using DownloadCallback = std::function<bool(std::size_t offset, const uint8_t* data, std::size_t len)>;
6361

64-
Response<std::size_t> Download(const char* url, const std::map<String, String>& headers, GotContentLengthCallback contentLengthCallback, DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
65-
Response<std::string> GetString(const char* url, const std::map<String, String>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
62+
Response<std::size_t> Download(const char* url, const std::map<std::string, std::string>& headers, GotContentLengthCallback contentLengthCallback, DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
63+
Response<std::string> GetString(const char* url, const std::map<std::string, std::string>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000);
6664

6765
template<typename T>
68-
Response<T> GetJSON(const char* url, const std::map<String, String>& headers, JsonParser<T> jsonParser, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000)
66+
Response<T> GetJSON(const char* url, const std::map<std::string, std::string>& headers, JsonParser<T> jsonParser, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs = 10'000)
6967
{
7068
auto response = GetString(url, headers, acceptedCodes, timeoutMs);
7169
if (response.result != RequestResult::Success) {

include/http/HTTPResponse.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

include/http/JsonAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace OpenShock::HTTP::JsonAPI {
1414
/// @brief Gets the hub info for the given hub token. Valid response codes: 200, 401
1515
/// @param hubToken
1616
/// @return
17-
HTTP::Response<Serialization::JsonAPI::HubInfoResponse> GetHubInfo(std::string_view hubToken);
17+
HTTP::Response<Serialization::JsonAPI::HubInfoResponse> GetHubInfo(std::string hubToken);
1818

1919
/// @brief Requests a Live Control Gateway to connect to. Valid response codes: 200, 401
2020
/// @param hubToken
2121
/// @return
22-
HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> AssignLcg(std::string_view hubToken);
22+
HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> AssignLcg(std::string hubToken);
2323
} // namespace OpenShock::HTTP::JsonAPI

src/http/HTTPRequestManager.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ const char* const TAG = "HTTPRequestManager";
44

55
#include "Common.h"
66
#include "Core.h"
7-
#include "http/HTTPClient.h"
87
#include "Logging.h"
98
#include "RateLimiter.h"
109
#include "SimpleMutex.h"
1110
#include "util/HexUtils.h"
1211
#include "util/StringUtils.h"
1312

14-
#include "WiFiClient.h"
13+
#include <esp_http_client.h>
1514

1615
#include <algorithm>
1716
#include <memory>
@@ -350,29 +349,35 @@ StreamReaderResult _readStreamData(HTTP::HTTPClient& client, WiFiClient* stream,
350349
}
351350

352351
HTTP::Response<std::size_t> _doGetStream(
353-
HTTP::HTTPClient& client,
352+
esp_http_client_handle_t client,
354353
const char* url,
355-
const std::map<String, String>& headers,
354+
const std::map<std::string, std::string>& headers,
356355
tcb::span<const uint16_t> acceptedCodes,
357356
std::shared_ptr<OpenShock::RateLimiter> rateLimiter,
358357
HTTP::GotContentLengthCallback contentLengthCallback,
359358
HTTP::DownloadCallback downloadCallback,
360359
int timeoutMs
361360
)
362361
{
362+
esp_err_t err;
363+
363364
int64_t begin = OpenShock::millis();
364365

365366
for (auto& header : headers) {
366-
client.SetHeader(header.first, header.second);
367+
err = esp_http_client_set_header(client, header.first.c_str(), header.second.c_str());
368+
if (err != ESP_OK) {
369+
// TODO: Handle error
370+
}
371+
return {HTTP::RequestResult::RequestFailed, 0, 0};
367372
}
368373

369-
auto response = client.Get(url);
370-
if (!response.IsValid()) {
371-
esp_err_t err = response.GetError();
374+
err = esp_http_client_open(client, 0);
375+
if (err != ESP_OK) {
372376
OS_LOGE(TAG, "Failed to begin HTTP request");
373377
return {HTTP::RequestResult::RequestFailed, 0, 0};
374378
}
375379

380+
err = esp_http_
376381
auto responseCode = response.ResponseCode();
377382
if (responseCode == HTTP_CODE_REQUEST_TIMEOUT || begin + timeoutMs < OpenShock::millis()) {
378383
OS_LOGW(TAG, "Request timed out");
@@ -383,7 +388,7 @@ HTTP::Response<std::size_t> _doGetStream(
383388
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
384389

385390
// Get "Retry-After" header
386-
String retryAfterStr = client.header("Retry-After");
391+
std::string retryAfterStr = client.header("Retry-After");
387392

388393
// Try to parse it as an integer (delay-seconds)
389394
long retryAfter = 0;
@@ -444,7 +449,7 @@ HTTP::Response<std::size_t> _doGetStream(
444449
return {result.result, responseCode, result.nWritten};
445450
}
446451

447-
HTTP::Response<std::size_t> HTTP::Download(const char* url, const std::map<String, String>& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
452+
HTTP::Response<std::size_t> HTTP::Download(const char* url, const std::map<std::string, std::string>& headers, HTTP::GotContentLengthCallback contentLengthCallback, HTTP::DownloadCallback downloadCallback, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
448453
{
449454
std::shared_ptr<OpenShock::RateLimiter> rateLimiter = _getRateLimiter(url);
450455
if (rateLimiter == nullptr) {
@@ -455,12 +460,29 @@ HTTP::Response<std::size_t> HTTP::Download(const char* url, const std::map<Strin
455460
return {RequestResult::RateLimited, 0, 0};
456461
}
457462

458-
HTTP::HTTPClient client(url);
463+
esp_http_client_config_t cfg = {
464+
.url = url,
465+
.user_agent = OpenShock::Constants::FW_USERAGENT,
466+
.timeout_ms = 10'000,
467+
.disable_auto_redirect = true,
468+
.event_handler = HTTPClient::EventHandler,
469+
.user_data = reinterpret_cast<void*>(this),
470+
.is_async = true,
471+
.use_global_ca_store = true,
472+
};
473+
esp_http_client_handle_t client = esp_http_client_init(&cfg);
474+
475+
auto result = _doGetStream(client, url, headers, acceptedCodes, rateLimiter, contentLengthCallback, downloadCallback, timeoutMs);
476+
477+
esp_err_t err = esp_http_client_cleanup(client);
478+
if (err != ESP_OK) {
479+
// TODO: Handle error
480+
}
459481

460-
return _doGetStream(client, url, headers, acceptedCodes, rateLimiter, contentLengthCallback, downloadCallback, timeoutMs);
482+
return result;
461483
}
462484

463-
HTTP::Response<std::string> HTTP::GetString(const char* url, const std::map<String, String>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
485+
HTTP::Response<std::string> HTTP::GetString(const char* url, const std::map<std::string, std::string>& headers, tcb::span<const uint16_t> acceptedCodes, uint32_t timeoutMs)
464486
{
465487
std::string result;
466488

src/http/JsonAPI.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ HTTP::Response<Serialization::JsonAPI::AccountLinkResponse> HTTP::JsonAPI::LinkA
2626
);
2727
}
2828

29-
HTTP::Response<Serialization::JsonAPI::HubInfoResponse> HTTP::JsonAPI::GetHubInfo(std::string_view hubToken)
29+
HTTP::Response<Serialization::JsonAPI::HubInfoResponse> HTTP::JsonAPI::GetHubInfo(std::string hubToken)
3030
{
3131
std::string domain;
3232
if (!Config::GetBackendDomain(domain)) {
@@ -39,15 +39,15 @@ HTTP::Response<Serialization::JsonAPI::HubInfoResponse> HTTP::JsonAPI::GetHubInf
3939
return HTTP::GetJSON<Serialization::JsonAPI::HubInfoResponse>(
4040
uri,
4141
{
42-
{ "Accept", "application/json"},
43-
{"DeviceToken", OpenShock::StringToArduinoString(hubToken)}
44-
},
42+
{ "Accept", "application/json"},
43+
{"DeviceToken", std::move(hubToken)}
44+
},
4545
Serialization::JsonAPI::ParseHubInfoJsonResponse,
4646
std::array<uint16_t, 2> {200}
4747
);
4848
}
4949

50-
HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> HTTP::JsonAPI::AssignLcg(std::string_view hubToken)
50+
HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> HTTP::JsonAPI::AssignLcg(std::string hubToken)
5151
{
5252
std::string domain;
5353
if (!Config::GetBackendDomain(domain)) {
@@ -60,9 +60,9 @@ HTTP::Response<Serialization::JsonAPI::AssignLcgResponse> HTTP::JsonAPI::AssignL
6060
return HTTP::GetJSON<Serialization::JsonAPI::AssignLcgResponse>(
6161
uri,
6262
{
63-
{ "Accept", "application/json"},
64-
{"DeviceToken", OpenShock::StringToArduinoString(hubToken)}
65-
},
63+
{ "Accept", "application/json"},
64+
{"DeviceToken", std::move(hubToken)}
65+
},
6666
Serialization::JsonAPI::ParseAssignLcgJsonResponse,
6767
std::array<uint16_t, 2> {200}
6868
);

0 commit comments

Comments
 (0)