2021-04-07 20:03:49 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
// system includes
|
|
|
|
|
#include <string>
|
2021-08-04 22:10:25 +02:00
|
|
|
#include <string_view>
|
2021-11-16 16:49:59 +01:00
|
|
|
#include <map>
|
2022-06-13 20:35:02 +02:00
|
|
|
#include <optional>
|
2023-02-27 17:26:55 +01:00
|
|
|
#include <expected>
|
2021-04-07 20:03:49 +02:00
|
|
|
|
|
|
|
|
// esp-idf includes
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
#include <freertos/task.h>
|
|
|
|
|
#include <esp_err.h>
|
|
|
|
|
|
2024-01-29 13:21:53 +01:00
|
|
|
// 3rdparty lib includes
|
|
|
|
|
#include <wrappers/http_client.h>
|
|
|
|
|
#include <wrappers/event_group.h>
|
|
|
|
|
#include <taskutils.h>
|
|
|
|
|
#include <clientauth.h>
|
2021-04-07 20:03:49 +02:00
|
|
|
|
|
|
|
|
class AsyncHttpRequest
|
|
|
|
|
{
|
|
|
|
|
public:
|
2024-02-20 20:38:59 +01:00
|
|
|
AsyncHttpRequest(const char *taskName="httpRequestTask", espcpputils::CoreAffinity coreAffinity=espcpputils::CoreAffinity::Core1, uint32_t taskSize = 3096);
|
2021-04-07 20:03:49 +02:00
|
|
|
~AsyncHttpRequest();
|
|
|
|
|
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> startTask();
|
|
|
|
|
std::expected<void, std::string> endTask();
|
2021-12-06 16:26:24 +01:00
|
|
|
bool taskRunning() const;
|
2021-04-15 21:20:08 +02:00
|
|
|
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> createClient(std::string_view url,
|
2024-01-29 13:21:53 +01:00
|
|
|
esp_http_client_method_t method = HTTP_METHOD_GET,
|
|
|
|
|
int timeout_ms = 0,
|
2024-05-27 19:34:51 +02:00
|
|
|
std::string_view serverCert = {},
|
2024-01-29 13:21:53 +01:00
|
|
|
const std::optional<cpputils::ClientAuth> &clientAuth = {});
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> deleteClient();
|
2021-12-06 16:26:24 +01:00
|
|
|
bool hasClient() const;
|
2021-04-15 21:20:08 +02:00
|
|
|
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> start(std::string_view url,
|
2024-01-29 13:21:53 +01:00
|
|
|
esp_http_client_method_t method = HTTP_METHOD_GET,
|
|
|
|
|
const std::map<std::string, std::string> &requestHeaders = {},
|
2024-02-21 16:27:12 +01:00
|
|
|
std::string &&requestBody = {}, int timeout_ms = 0,
|
2024-05-27 19:34:51 +02:00
|
|
|
std::string_view serverCert = {},
|
2024-01-29 13:21:53 +01:00
|
|
|
const std::optional<cpputils::ClientAuth> &clientAuth = {});
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> retry(std::optional<std::string_view> url = std::nullopt,
|
2024-01-29 13:21:53 +01:00
|
|
|
std::optional<esp_http_client_method_t> method = std::nullopt,
|
|
|
|
|
const std::map<std::string, std::string> &requestHeaders = {},
|
2024-02-21 16:27:12 +01:00
|
|
|
std::optional<std::string> &&requestBody = {}, std::optional<int> timeout_ms = {});
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> abort();
|
2021-04-07 20:03:49 +02:00
|
|
|
|
2021-04-15 21:20:08 +02:00
|
|
|
bool inProgress() const;
|
|
|
|
|
|
2021-04-07 20:03:49 +02:00
|
|
|
bool finished() const;
|
2023-02-27 17:26:55 +01:00
|
|
|
std::expected<void, std::string> result() const;
|
2021-04-07 20:03:49 +02:00
|
|
|
|
2021-10-28 05:05:41 +02:00
|
|
|
int statusCode() const { return m_statusCode; }
|
|
|
|
|
|
2021-04-15 21:20:08 +02:00
|
|
|
void clearFinished();
|
|
|
|
|
|
2021-08-04 22:10:25 +02:00
|
|
|
const std::string &buffer() const { return m_buf; }
|
|
|
|
|
std::string &&takeBuffer() { return std::move(m_buf); }
|
2021-04-07 20:03:49 +02:00
|
|
|
|
2021-11-15 11:44:17 +01:00
|
|
|
std::size_t sizeLimit() const { return m_sizeLimit; }
|
|
|
|
|
void setSizeLimit(std::size_t sizeLimit) { m_sizeLimit = sizeLimit; }
|
|
|
|
|
|
2021-11-16 16:49:59 +01:00
|
|
|
bool collectResponseHeaders() const { return m_collectResponseHeaders; }
|
|
|
|
|
void setCollectResponseHeaders(bool collectResponseHeaders) { m_collectResponseHeaders = collectResponseHeaders; }
|
|
|
|
|
|
|
|
|
|
const std::map<std::string, std::string> &responseHeaders() const { return m_responseHeaders; }
|
|
|
|
|
std::map<std::string, std::string> &&takeResponseHeaders() { return std::move(m_responseHeaders); }
|
|
|
|
|
|
2021-04-07 20:03:49 +02:00
|
|
|
private:
|
2021-11-15 11:44:17 +01:00
|
|
|
esp_err_t httpEventHandler(esp_http_client_event_t *evt);
|
|
|
|
|
static esp_err_t staticHttpEventHandler(esp_http_client_event_t *evt);
|
2021-04-07 20:03:49 +02:00
|
|
|
static void requestTask(void *ptr);
|
2021-08-04 22:10:25 +02:00
|
|
|
void requestTask();
|
2021-04-07 20:03:49 +02:00
|
|
|
|
2021-08-04 22:10:25 +02:00
|
|
|
espcpputils::http_client m_client;
|
|
|
|
|
std::string m_buf;
|
|
|
|
|
TaskHandle_t m_taskHandle{NULL};
|
|
|
|
|
espcpputils::event_group m_eventGroup;
|
|
|
|
|
esp_err_t m_result{};
|
|
|
|
|
int m_statusCode{};
|
2021-11-15 11:44:17 +01:00
|
|
|
std::size_t m_sizeLimit{4096};
|
2021-11-16 16:49:59 +01:00
|
|
|
bool m_collectResponseHeaders{};
|
|
|
|
|
std::map<std::string, std::string> m_responseHeaders;
|
2024-02-21 16:27:12 +01:00
|
|
|
std::string m_requestBody;
|
2021-04-07 20:03:49 +02:00
|
|
|
|
2021-04-07 20:17:40 +02:00
|
|
|
const char * const m_taskName;
|
2024-02-20 20:38:59 +01:00
|
|
|
const uint32_t m_taskSize;
|
2021-04-07 20:03:49 +02:00
|
|
|
const espcpputils::CoreAffinity m_coreAffinity;
|
|
|
|
|
};
|