Add ifdef for client auth
This commit is contained in:
@ -36,4 +36,10 @@ config LOG_LOCAL_LEVEL_ASYNC_HTTP
|
|||||||
default 4 if LOG_LOCAL_LEVEL_ASYNC_HTTP_DEBUG
|
default 4 if LOG_LOCAL_LEVEL_ASYNC_HTTP_DEBUG
|
||||||
default 5 if LOG_LOCAL_LEVEL_ASYNC_HTTP_VERBOSE
|
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
|
endmenu
|
||||||
|
@ -138,10 +138,16 @@ bool AsyncHttpRequest::taskRunning() const
|
|||||||
return false;
|
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,
|
std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method,
|
||||||
int timeout_ms,
|
int timeout_ms,
|
||||||
std::string_view serverCert,
|
std::string_view serverCert,
|
||||||
const std::optional<cpputils::ClientAuth> &clientAuth)
|
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)
|
if (m_client)
|
||||||
{
|
{
|
||||||
@ -167,6 +173,7 @@ std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view
|
|||||||
config.cert_len = serverCert.size();
|
config.cert_len = serverCert.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
if (clientAuth)
|
if (clientAuth)
|
||||||
{
|
{
|
||||||
config.client_key_pem = clientAuth->clientKey.data();
|
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_pem = clientAuth->clientCert.data();
|
||||||
config.client_cert_len = clientAuth->clientCert.size();
|
config.client_cert_len = clientAuth->clientCert.size();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
m_client = espcpputils::http_client{&config};
|
m_client = espcpputils::http_client{&config};
|
||||||
|
|
||||||
@ -211,12 +219,20 @@ bool AsyncHttpRequest::hasClient() const
|
|||||||
return m_client;
|
return m_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
|
std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
|
||||||
esp_http_client_method_t method,
|
esp_http_client_method_t method,
|
||||||
const std::map<std::string, std::string> &requestHeaders,
|
const std::map<std::string, std::string> &requestHeaders,
|
||||||
std::string &&requestBody, int timeout_ms,
|
std::string &&requestBody, int timeout_ms,
|
||||||
std::string_view serverCert,
|
std::string_view serverCert,
|
||||||
const std::optional<cpputils::ClientAuth> &clientAuth)
|
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)
|
if (!m_taskHandle)
|
||||||
{
|
{
|
||||||
@ -237,8 +253,13 @@ std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
|
|||||||
m_client = {};
|
m_client = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
if (auto result = createClient(url, method, timeout_ms, serverCert, clientAuth); !result)
|
if (auto result = createClient(url, method, timeout_ms, serverCert, clientAuth); !result)
|
||||||
return std::unexpected(std::move(result).error());
|
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);
|
m_requestBody = std::move(requestBody);
|
||||||
if (!m_requestBody.empty())
|
if (!m_requestBody.empty())
|
||||||
|
@ -16,7 +16,9 @@
|
|||||||
#include <wrappers/http_client.h>
|
#include <wrappers/http_client.h>
|
||||||
#include <wrappers/event_group.h>
|
#include <wrappers/event_group.h>
|
||||||
#include <taskutils.h>
|
#include <taskutils.h>
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
#include <clientauth.h>
|
#include <clientauth.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
class AsyncHttpRequest
|
class AsyncHttpRequest
|
||||||
{
|
{
|
||||||
@ -28,20 +30,36 @@ public:
|
|||||||
std::expected<void, std::string> endTask();
|
std::expected<void, std::string> endTask();
|
||||||
bool taskRunning() const;
|
bool taskRunning() const;
|
||||||
|
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
std::expected<void, std::string> createClient(std::string_view url,
|
std::expected<void, std::string> createClient(std::string_view url,
|
||||||
esp_http_client_method_t method = HTTP_METHOD_GET,
|
esp_http_client_method_t method = HTTP_METHOD_GET,
|
||||||
int timeout_ms = 0,
|
int timeout_ms = 0,
|
||||||
std::string_view serverCert = {},
|
std::string_view serverCert = {},
|
||||||
const std::optional<cpputils::ClientAuth> &clientAuth = {});
|
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();
|
std::expected<void, std::string> deleteClient();
|
||||||
bool hasClient() const;
|
bool hasClient() const;
|
||||||
|
|
||||||
|
#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH
|
||||||
std::expected<void, std::string> start(std::string_view url,
|
std::expected<void, std::string> start(std::string_view url,
|
||||||
esp_http_client_method_t method = HTTP_METHOD_GET,
|
esp_http_client_method_t method = HTTP_METHOD_GET,
|
||||||
const std::map<std::string, std::string> &requestHeaders = {},
|
const std::map<std::string, std::string> &requestHeaders = {},
|
||||||
std::string &&requestBody = {}, int timeout_ms = 0,
|
std::string &&requestBody = {}, int timeout_ms = 0,
|
||||||
std::string_view serverCert = {},
|
std::string_view serverCert = {},
|
||||||
const std::optional<cpputils::ClientAuth> &clientAuth = {});
|
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::expected<void, std::string> retry(std::optional<std::string_view> url = std::nullopt,
|
||||||
std::optional<esp_http_client_method_t> method = std::nullopt,
|
std::optional<esp_http_client_method_t> method = std::nullopt,
|
||||||
const std::map<std::string, std::string> &requestHeaders = {},
|
const std::map<std::string, std::string> &requestHeaders = {},
|
||||||
|
Reference in New Issue
Block a user