diff options
| author | Aotrix <[email protected]> | 2025-11-17 13:30:23 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2025-11-17 13:30:23 +0100 |
| commit | 9dab564d3aaabda88efda5e3289392d0be716084 (patch) | |
| tree | 4d2d0188119ba33f29c0dc6c362c26a47a9e637d /submodules/battleNetApi/src/hearthstoneApi.cpp | |
| parent | f78e3e1428f59fb96cb02cf6fbaad692a582f9d5 (diff) | |
changed subprojects to submodules
Diffstat (limited to 'submodules/battleNetApi/src/hearthstoneApi.cpp')
| -rw-r--r-- | submodules/battleNetApi/src/hearthstoneApi.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/submodules/battleNetApi/src/hearthstoneApi.cpp b/submodules/battleNetApi/src/hearthstoneApi.cpp new file mode 100644 index 0000000..c34e6ad --- /dev/null +++ b/submodules/battleNetApi/src/hearthstoneApi.cpp @@ -0,0 +1,40 @@ +#include "hearthstoneApi.hpp" +#include <curl/curl.h> +#include <format> + +HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mAuthorizationHeader(authorizationHeader()) {} + +std::uint64_t HearthstoneApi::numberOfCards(const std::string &iApiResponse) const { + const std::string aCardCountRegex{"\"cardCount\":(\\d+)"}; + return valueInJson<std::uint64_t>(iApiResponse, aCardCountRegex); +} +std::string HearthstoneApi::authorizationHeader() { return std::format("Authorization: Bearer {}", mToken.toString()); } + +size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, std::string *iS) { + const size_t kNewLength = iSize * iNmemb; + try { + iS->append(static_cast<char *>(iContent), kNewLength); + } catch (std::bad_alloc &e) { + // handle memory problem + return 0; + } + return kNewLength; +} + +std::string HearthstoneApi::makeApiRequest(const std::string &iUrl) const { + CURL *curl = curl_easy_init(); + std::string request_output; + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, iUrl.c_str()); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallbackStringOutput); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request_output); + struct curl_slist *chunk = nullptr; + chunk = curl_slist_append(chunk, mAuthorizationHeader.c_str()); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); + curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + return request_output; +} |
