From 13f8fa1fd1878dd4f0ad6f22dc65da777c6fc377 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 6 Oct 2024 17:46:43 +0200 Subject: [PATCH] Add ifdef for client auth --- Kconfig.projbuild | 6 ++++++ src/asynchttprequest.cpp | 21 +++++++++++++++++++++ src/asynchttprequest.h | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/Kconfig.projbuild b/Kconfig.projbuild index c3b6d18..a6fd1a6 100644 --- a/Kconfig.projbuild +++ b/Kconfig.projbuild @@ -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 diff --git a/src/asynchttprequest.cpp b/src/asynchttprequest.cpp index e421357..9f62fe0 100644 --- a/src/asynchttprequest.cpp +++ b/src/asynchttprequest.cpp @@ -138,10 +138,16 @@ bool AsyncHttpRequest::taskRunning() const return false; } +#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH std::expected AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method, int timeout_ms, std::string_view serverCert, const std::optional &clientAuth) +#else +std::expected 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 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 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 AsyncHttpRequest::start(std::string_view url, esp_http_client_method_t method, const std::map &requestHeaders, std::string &&requestBody, int timeout_ms, std::string_view serverCert, const std::optional &clientAuth) +#else +std::expected AsyncHttpRequest::start(std::string_view url, + esp_http_client_method_t method, + const std::map &requestHeaders, + std::string &&requestBody, int timeout_ms, + std::string_view serverCert) +#endif { if (!m_taskHandle) { @@ -237,8 +253,13 @@ std::expected 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()) diff --git a/src/asynchttprequest.h b/src/asynchttprequest.h index c1c6a30..f57c3d2 100644 --- a/src/asynchttprequest.h +++ b/src/asynchttprequest.h @@ -16,7 +16,9 @@ #include #include #include +#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH #include +#endif class AsyncHttpRequest { @@ -28,20 +30,36 @@ public: std::expected endTask(); bool taskRunning() const; +#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH std::expected 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 &clientAuth = {}); +#else + std::expected 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 deleteClient(); bool hasClient() const; +#ifdef ASYNC_HTTP_REQ_INCLUDE_CLIENT_AUTH std::expected start(std::string_view url, esp_http_client_method_t method = HTTP_METHOD_GET, const std::map &requestHeaders = {}, std::string &&requestBody = {}, int timeout_ms = 0, std::string_view serverCert = {}, const std::optional &clientAuth = {}); +#else + std::expected start(std::string_view url, + esp_http_client_method_t method = HTTP_METHOD_GET, + const std::map &requestHeaders = {}, + std::string &&requestBody = {}, int timeout_ms = 0, + std::string_view serverCert = {}); +#endif std::expected retry(std::optional url = std::nullopt, std::optional method = std::nullopt, const std::map &requestHeaders = {},