diff options
Diffstat (limited to 'subprojects')
| -rw-r--r-- | subprojects/battleNetApi/include/hearthstoneApi.hpp | 9 | ||||
| -rw-r--r-- | subprojects/battleNetApi/src/hearthstoneApi.cpp | 53 | ||||
| -rw-r--r-- | subprojects/battleNetApi/tests/tests.cpp | 7 |
3 files changed, 36 insertions, 33 deletions
diff --git a/subprojects/battleNetApi/include/hearthstoneApi.hpp b/subprojects/battleNetApi/include/hearthstoneApi.hpp index c2298f7..6b3bbe5 100644 --- a/subprojects/battleNetApi/include/hearthstoneApi.hpp +++ b/subprojects/battleNetApi/include/hearthstoneApi.hpp @@ -5,18 +5,19 @@ class HearthstoneApi { public: HearthstoneApi(Token &&iToken); + std::uint64_t numberOfCards(); std::string getCards(const std::string &t_card_parameters); private: - std::string generateRequestHeader(); + std::string authorizationHeader(); const std::string getUrl(const std::string &t_parameters); const std::string getSetOfCardsUrl(const std::string &t_card_parameters); - std::string makeApiRequest(const char *t_url, const char *t_header); - std::string makeApiRequest(const std::string &t_url, const std::string &t_header); + std::string makeApiRequest(const char *t_url); + std::string makeApiRequest(const std::string &t_url); static size_t curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s); Token mToken; - std::string mHeader; + std::string mAuthorizationHeader; //<! Bearer authorization string for OAuth 2.0 header friend class HearthstoneApiMock; }; diff --git a/subprojects/battleNetApi/src/hearthstoneApi.cpp b/subprojects/battleNetApi/src/hearthstoneApi.cpp index d531247..903e987 100644 --- a/subprojects/battleNetApi/src/hearthstoneApi.cpp +++ b/subprojects/battleNetApi/src/hearthstoneApi.cpp @@ -2,42 +2,30 @@ #include <curl/curl.h> #include <format> -HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mHeader(generateRequestHeader()) {} +HearthstoneApi::HearthstoneApi(Token &&iToken) : mToken(iToken), mAuthorizationHeader(authorizationHeader()) {} -std::string HearthstoneApi::generateRequestHeader() { - return std::format("Authorization: Bearer {}", mToken.toString()); +std::uint64_t HearthstoneApi::numberOfCards() { + // TODO: implement logic + return 123456; } -const std::string HearthstoneApi::getUrl(const std::string &t_parameters) { - return std::format("https://eu.api.blizzard.com/hearthstone/{}", t_parameters); -} - -const std::string HearthstoneApi::getSetOfCardsUrl(const std::string &t_card_parameters) { - return getUrl(std::format("cards{}", t_card_parameters)); -} +std::string HearthstoneApi::authorizationHeader() { return std::format("Authorization: Bearer {}", mToken.toString()); } -size_t HearthstoneApi::curlCallbackStringOutput(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) { - size_t newLength = t_size * t_nmemb; - try { - t_s->append(static_cast<char *>(t_contents), newLength); - } catch (std::bad_alloc &e) { - // handle memory problem - return 0; - } - return newLength; +const std::string HearthstoneApi::getUrl(const std::string &iParameters) { + return std::format("https://eu.api.blizzard.com/hearthstone/{}", iParameters); } -std::string HearthstoneApi::makeApiRequest(const char *t_url, const char *t_header) { +std::string HearthstoneApi::makeApiRequest(const char *iUrl) { CURL *curl = curl_easy_init(); std::string request_output; if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, t_url); + curl_easy_setopt(curl, CURLOPT_URL, iUrl); 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, t_header); + chunk = curl_slist_append(chunk, mAuthorizationHeader.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); curl_easy_perform(curl); curl_easy_cleanup(curl); @@ -45,10 +33,23 @@ std::string HearthstoneApi::makeApiRequest(const char *t_url, const char *t_head return request_output; } -std::string HearthstoneApi::makeApiRequest(const std::string &t_url, const std::string &t_header) { - return makeApiRequest(t_url.c_str(), t_header.c_str()); +const std::string HearthstoneApi::getSetOfCardsUrl(const std::string &iCardParameters) { + return getUrl(std::format("cards{}", iCardParameters)); } -std::string HearthstoneApi::getCards(const std::string &t_card_parameters) { - return makeApiRequest(getSetOfCardsUrl(t_card_parameters), mHeader); +size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, std::string *iS) { + size_t newLength = iSize * iNmemb; + try { + iS->append(static_cast<char *>(iContent), newLength); + } catch (std::bad_alloc &e) { + // handle memory problem + return 0; + } + return newLength; +} + +std::string HearthstoneApi::makeApiRequest(const std::string &iUrl) { return makeApiRequest(iUrl.c_str()); } + +std::string HearthstoneApi::getCards(const std::string &iCardParameters) { + return makeApiRequest(getSetOfCardsUrl(iCardParameters)); } diff --git a/subprojects/battleNetApi/tests/tests.cpp b/subprojects/battleNetApi/tests/tests.cpp index 3cb28ff..d225de6 100644 --- a/subprojects/battleNetApi/tests/tests.cpp +++ b/subprojects/battleNetApi/tests/tests.cpp @@ -38,7 +38,8 @@ TEST_CASE("Retrieve API Token", "[getApiToken]") { TEST_CASE("Retrieve number of pages", "[getPagesCount]") { HearthstoneApiMock aHearthstoneApi; - // aHearthstoneApi.getNumberOfPages(); - // input parameters - // aHearthstoneApi.getCards + // std::cout << aHearthstoneApi.numberOfCards() << '\n'; + // aHearthstoneApi.getNumberOfPages(); + // input parameters + // aHearthstoneApi.getCards } |
