#include "hearthstoneApi.hpp" #include #include 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(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(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; }