Add ifdef for client auth

This commit is contained in:
CommanderRedYT
2024-10-06 17:46:43 +02:00
parent 4a319bdcd2
commit 13f8fa1fd1
3 changed files with 45 additions and 0 deletions

View File

@ -36,4 +36,10 @@ config LOG_LOCAL_LEVEL_ASYNC_HTTP
default 4 if LOG_LOCAL_LEVEL_ASYNC_HTTP_DEBUG
default 5 if LOG_LOCAL_LEVEL_ASYNC_HTTP_VERBOSE
config ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
bool "Include client authentication"
default n
help
Include client authentication in the request.
endmenu

View File

@ -138,10 +138,16 @@ bool AsyncHttpRequest::taskRunning() const
return false;
}
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method,
int timeout_ms,
std::string_view serverCert,
const std::optional<cpputils::ClientAuth> &clientAuth)
#else
std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method,
int timeout_ms,
std::string_view serverCert)
#endif
{
if (m_client)
{
@ -167,6 +173,7 @@ std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view
config.cert_len = serverCert.size();
}
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
if (clientAuth)
{
config.client_key_pem = clientAuth->clientKey.data();
@ -174,6 +181,7 @@ std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view
config.client_cert_pem = clientAuth->clientCert.data();
config.client_cert_len = clientAuth->clientCert.size();
}
#endif
m_client = espcpputils::http_client{&config};
@ -211,12 +219,20 @@ bool AsyncHttpRequest::hasClient() const
return m_client;
}
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
esp_http_client_method_t method,
const std::map<std::string, std::string> &requestHeaders,
std::string &&requestBody, int timeout_ms,
std::string_view serverCert,
const std::optional<cpputils::ClientAuth> &clientAuth)
#else
std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
esp_http_client_method_t method,
const std::map<std::string, std::string> &requestHeaders,
std::string &&requestBody, int timeout_ms,
std::string_view serverCert)
#endif
{
if (!m_taskHandle)
{
@ -237,8 +253,13 @@ std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
m_client = {};
}
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
if (auto result = createClient(url, method, timeout_ms, serverCert, clientAuth); !result)
return std::unexpected(std::move(result).error());
#else
if (auto result = createClient(url, method, timeout_ms, serverCert); !result)
return std::unexpected(std::move(result).error());
#endif
m_requestBody = std::move(requestBody);
if (!m_requestBody.empty())

View File

@ -16,7 +16,9 @@
#include <wrappers/http_client.h>
#include <wrappers/event_group.h>
#include <taskutils.h>
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
#include <clientauth.h>
#endif
class AsyncHttpRequest
{
@ -28,20 +30,36 @@ public:
std::expected<void, std::string> endTask();
bool taskRunning() const;
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
std::expected<void, std::string> createClient(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET,
int timeout_ms = 0,
std::string_view serverCert = {},
const std::optional<cpputils::ClientAuth> &clientAuth = {});
#else
std::expected<void, std::string> createClient(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET,
int timeout_ms = 0,
std::string_view serverCert = {});
#endif
std::expected<void, std::string> deleteClient();
bool hasClient() const;
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
std::expected<void, std::string> start(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET,
const std::map<std::string, std::string> &requestHeaders = {},
std::string &&requestBody = {}, int timeout_ms = 0,
std::string_view serverCert = {},
const std::optional<cpputils::ClientAuth> &clientAuth = {});
#else
std::expected<void, std::string> start(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET,
const std::map<std::string, std::string> &requestHeaders = {},
std::string &&requestBody = {}, int timeout_ms = 0,
std::string_view serverCert = {});
#endif
std::expected<void, std::string> retry(std::optional<std::string_view> url = std::nullopt,
std::optional<esp_http_client_method_t> method = std::nullopt,
const std::map<std::string, std::string> &requestHeaders = {},