From c777502a1c22b240f66069831e6f0cb1e7e41b64 Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Sat, 10 Oct 2026 16:09:48 +0200 Subject: Use string_view and delete gameView --- battleNetApi/include/apiUrl.hpp | 31 +++---- battleNetApi/src/apiUrl.cpp | 94 +++++++++++++++------ battleNetApi/src/hearthstoneApi.cpp | 112 ++++++++++++++++--------- battleNetApi/tests/src/hearthstoneApiTests.cpp | 43 ++++++---- 4 files changed, 185 insertions(+), 95 deletions(-) (limited to 'battleNetApi') diff --git a/battleNetApi/include/apiUrl.hpp b/battleNetApi/include/apiUrl.hpp index 1360102..bc62d42 100644 --- a/battleNetApi/include/apiUrl.hpp +++ b/battleNetApi/include/apiUrl.hpp @@ -3,16 +3,18 @@ #include #include -namespace bnet { +namespace bnet +{ -class ApiUrl { +class ApiUrl +{ public: - ApiUrl(const std::string &iContentType, const std::string &iRegion); + ApiUrl(const std::string& iContentType, + const std::string& iRegion); - void setRegion(const std::string &iRegion); - void setLocale(const std::string &iLocale); - void setGameMode(const std::string &iGameMode); - void setPage(const std::string &iPage); + void setRegion(const std::string& iRegion); + void setLocale(const std::string& iLocale); + void setGameMode(const std::string& iGameMode); void setPage(uint64_t iPage); void setPageSize(uint64_t iPageSize); @@ -25,15 +27,16 @@ class ApiUrl { std::string text() const; private: - static constexpr std::string kLOCALE_KEY{"locale"}; - static constexpr std::string kGAME_MODE_KEY{"gameMode"}; - static constexpr std::string kPAGE_KEY{"page"}; - static constexpr std::string kPAGE_SIZE_KEY{"pageSize"}; + std::string_view kLOCALE_KEY{"locale"}; + std::string_view kGAME_MODE_KEY{"gameMode"}; + std::string_view kPAGE_KEY{"page"}; + std::string_view kPAGE_SIZE_KEY{"pageSize"}; std::string mContentType; std::string mRegion; - std::unordered_map aParameters; + std::unordered_map aParameters; - void setValue(const std::string &iKey, const std::string &iValue); - bool hasValue(const std::string &iKey) const; + void setValue(std::string_view iKey, + const std::string& iValue); + bool hasValue(std::string_view iKey) const; }; }; // namespace bnet diff --git a/battleNetApi/src/apiUrl.cpp b/battleNetApi/src/apiUrl.cpp index 8775943..bc54330 100644 --- a/battleNetApi/src/apiUrl.cpp +++ b/battleNetApi/src/apiUrl.cpp @@ -2,54 +2,96 @@ #include #include -namespace bnet { +namespace bnet +{ -ApiUrl::ApiUrl(const std::string &iContentType, const std::string &iRegion) - : mContentType{iContentType}, mRegion{iRegion} {} -void ApiUrl::setRegion(const std::string &iRegion) { - if (!iRegion.empty()) { +ApiUrl::ApiUrl(const std::string& iContentType, + const std::string& iRegion) + : mContentType{iContentType}, + mRegion{iRegion} +{ +} + +void ApiUrl::setRegion(const std::string& iRegion) +{ + if (!iRegion.empty()) + { mRegion = iRegion; //!< The region of the data to receive, e.g. "eu" } } -void ApiUrl::setLocale(const std::string &iLocale) { - if (!iLocale.empty()) { + +void ApiUrl::setLocale(const std::string& iLocale) +{ + if (!iLocale.empty()) + { //!< The locale to reflect in localized data, e.g. "fr_FR" or "ko_KR" //!< If you do !supply a value, all translations are returned. setValue(kLOCALE_KEY, iLocale); } } -void ApiUrl::setGameMode(const std::string &iGameMode) { - if (!iGameMode.empty()) { + +void ApiUrl::setGameMode(const std::string& iGameMode) +{ + if (!iGameMode.empty()) + { //!< A recognized game mode, e.g. "constructed" //!< @see //!< https://community.developer.battle.net/documentation/hearthstone/guides/game-modes setValue(kGAME_MODE_KEY, iGameMode); } } -void ApiUrl::setPage(const std::string &iPage) { - if (!iPage.empty()) { - //!< The page to retrieve, e.g. "9" - setValue(kPAGE_KEY, iPage); - } + +void ApiUrl::setPage(uint64_t iPage) +{ + //!< The page to retrieve, e.g. "9" + setValue(kPAGE_KEY, std::to_string(iPage)); } -void ApiUrl::setPage(uint64_t iPage) { setPage(std::to_string(iPage)); } -void ApiUrl::setPageSize(uint64_t iPageSize) { + +void ApiUrl::setPageSize(uint64_t iPageSize) +{ setValue(kPAGE_SIZE_KEY, std::to_string(iPageSize)); } -bool ApiUrl::hasLocale() const { return hasValue(kLOCALE_KEY); } -bool ApiUrl::hasGameMode() const { return hasValue(kGAME_MODE_KEY); } -bool ApiUrl::hasPage() const { return hasValue(kPAGE_KEY); } -bool ApiUrl::hasPageSize() const { return hasValue(kPAGE_SIZE_KEY); } -void ApiUrl::setValue(const std::string &iKey, const std::string &iValue) { - aParameters[iKey] = iValue; + +bool ApiUrl::hasLocale() const +{ + return hasValue(kLOCALE_KEY); +} + +bool ApiUrl::hasGameMode() const +{ + return hasValue(kGAME_MODE_KEY); } -bool ApiUrl::hasValue(const std::string &iKey) const { return aParameters.count(iKey) > 0; } -std::string ApiUrl::text() const { + +bool ApiUrl::hasPage() const +{ + return hasValue(kPAGE_KEY); +} + +bool ApiUrl::hasPageSize() const +{ + return hasValue(kPAGE_SIZE_KEY); +} + +void ApiUrl::setValue(std::string_view iKey, + const std::string& iValue) +{ + aParameters[iKey] = std::move(iValue); +} + +bool ApiUrl::hasValue(std::string_view iKey) const +{ + return aParameters.count(iKey) > 0; +} + +std::string ApiUrl::text() const +{ std::string aFormattedParameters; - for (const auto &[key, value] : aParameters) { + for (const auto& [key, value] : aParameters) + { aFormattedParameters += std::format("&{}={}", key, value); } - if (!aFormattedParameters.empty()) { + if (!aFormattedParameters.empty()) + { aFormattedParameters[0] = '?'; } aFormattedParameters = std::format("https://{}.api.blizzard.com/hearthstone/{}/{}", mRegion, diff --git a/battleNetApi/src/hearthstoneApi.cpp b/battleNetApi/src/hearthstoneApi.cpp index 4dd8fac..c78c4b9 100644 --- a/battleNetApi/src/hearthstoneApi.cpp +++ b/battleNetApi/src/hearthstoneApi.cpp @@ -7,33 +7,44 @@ #include #include -namespace bnet { -std::string HearthstoneApi::authorizationHeader(const std::string &iTokenText) { +namespace bnet +{ +std::string HearthstoneApi::authorizationHeader(const std::string& iTokenText) +{ return std::format("Authorization: Bearer {}", iTokenText); } -size_t HearthstoneApi::curlCallbackStringOutput(void *iContent, size_t iSize, size_t iNmemb, - std::string *iS) { +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) { + try + { + iS->append(static_cast(iContent), kNewLength); + } + catch (std::bad_alloc& e) + { // handle memory problem return 0; } return kNewLength; } -json HearthstoneApi::makeApiRequest(const ApiUrl &iApiUrl, const std::string &iTokenText) { - CURL *curl = curl_easy_init(); +json HearthstoneApi::makeApiRequest(const ApiUrl& iApiUrl, + const std::string& iTokenText) +{ + CURL* curl = curl_easy_init(); std::string aRequestOutput; - if (curl) { + if (curl) + { curl_easy_setopt(curl, CURLOPT_URL, iApiUrl.text().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, &aRequestOutput); - struct curl_slist *chunk = nullptr; + struct curl_slist* chunk = nullptr; chunk = curl_slist_append(chunk, authorizationHeader(iTokenText).c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); curl_easy_perform(curl); @@ -43,27 +54,31 @@ json HearthstoneApi::makeApiRequest(const ApiUrl &iApiUrl, const std::string &iT return aOutputJson; } -std::vector HearthstoneApi::makeApiRequest(const std::vector &iApiUrls, - const std::string &iTokenText) { +std::vector HearthstoneApi::makeApiRequest(const std::vector& iApiUrls, + const std::string& iTokenText) +{ std::vector aApiUrls(iApiUrls.size()); std::ranges::transform(iApiUrls, aApiUrls.begin(), - [](const ApiUrl &iApiUrl) { return iApiUrl.text(); }); + [](const ApiUrl& iApiUrl) { return iApiUrl.text(); }); return makeApiRequest(aApiUrls, iTokenText); } -std::vector HearthstoneApi::makeApiRequest(const std::vector &iUrls, - const std::string &iTokenText) { +std::vector HearthstoneApi::makeApiRequest(const std::vector& iUrls, + const std::string& iTokenText) +{ std::vector aResponses(iUrls.size()); - CURLM *curlm = curl_multi_init(); - if (curlm) { - for (std::vector::size_type i{0}; i < aResponses.size(); ++i) { - CURL *curl = curl_easy_init(); + CURLM* curlm = curl_multi_init(); + if (curlm) + { + for (std::vector::size_type i{0}; i < aResponses.size(); ++i) + { + CURL* curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, iUrls[i].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, &(aResponses[i])); - struct curl_slist *chunk = nullptr; + struct curl_slist* chunk = nullptr; chunk = curl_slist_append(chunk, authorizationHeader(iTokenText).c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); curl_multi_add_handle(curlm, curl); @@ -71,32 +86,39 @@ std::vector HearthstoneApi::makeApiRequest(const std::vector const int kMaxWaitMs{5000}; int aStillRunning{0}; curl_multi_perform(curlm, &aStillRunning); - do { + do + { int numfds = 0; - int res = curl_multi_wait(curlm, NULL, 0, kMaxWaitMs, &numfds); - if (res != CURLM_OK) { + int res = curl_multi_wait(curlm, NULL, 0, kMaxWaitMs, &numfds); + if (res != CURLM_OK) + { std::cerr << std::format("error: curl_multi_wait() returned {}\n", res); } curl_multi_perform(curlm, &aStillRunning); } while (aStillRunning); // clean-up - CURLMsg *aMessage{nullptr}; - CURL *aHandle{nullptr}; + CURLMsg* aMessage{nullptr}; + CURL* aHandle{nullptr}; int aMessagesLeft{0}; CURLcode aReturnCode; - while ((aMessage = curl_multi_info_read(curlm, &aMessagesLeft))) { - if (aMessage->msg == CURLMSG_DONE) { - aHandle = aMessage->easy_handle; + while ((aMessage = curl_multi_info_read(curlm, &aMessagesLeft))) + { + if (aMessage->msg == CURLMSG_DONE) + { + aHandle = aMessage->easy_handle; aReturnCode = aMessage->data.result; - if (aReturnCode != CURLE_OK) { + if (aReturnCode != CURLE_OK) + { fprintf(stderr, "CURL error code: %d\n", aMessage->data.result); continue; } curl_multi_remove_handle(curlm, aHandle); curl_easy_cleanup(aHandle); aHandle = nullptr; - } else { + } + else + { std::cerr << std::format("error: after curl_multi_info_read(), CURLMsg={}\n", static_cast(aMessage->msg)); } @@ -104,32 +126,40 @@ std::vector HearthstoneApi::makeApiRequest(const std::vector curl_multi_cleanup(curlm); } std::vector aJsonOutputs; - for (std::string aResponse : aResponses) { + for (std::string aResponse : aResponses) + { aJsonOutputs.push_back(json::parse(aResponse)); } return aJsonOutputs; } -json HearthstoneApi::downloadCards(ApiUrl iApiUrl, const Token &iToken) { - if (!iToken.text().has_value() || iToken.text().value().empty()) { +json HearthstoneApi::downloadCards(ApiUrl iApiUrl, + const Token& iToken) +{ + if (!iToken.text().has_value() || iToken.text().value().empty()) + { throw std::invalid_argument("Token text does not exist or is empty"); } - if (iApiUrl.hasPage()) { + if (iApiUrl.hasPage()) + { return makeApiRequest(iApiUrl, *iToken.text()); } - iApiUrl.setPage("1"); + iApiUrl.setPage(1); std::vector aRequests; json aFirstPageJson = makeApiRequest(iApiUrl, *iToken.text()); int kNumberOfPages{aFirstPageJson["pageCount"]}; - for (int i{1}; i < kNumberOfPages; ++i) { - iApiUrl.setPage(std::to_string(i + 1)); + for (int i{1}; i < kNumberOfPages; ++i) + { + iApiUrl.setPage(i + 1); aRequests.push_back(iApiUrl.text()); } - json::reference &aOutputJson = aFirstPageJson["cards"]; + json::reference& aOutputJson = aFirstPageJson["cards"]; std::vector kApiResponses = makeApiRequest(aRequests, *iToken.text()); - for (json &aPage : kApiResponses) { - for (json::reference &aCard : aPage["cards"]) { + for (json& aPage : kApiResponses) + { + for (json::reference& aCard : aPage["cards"]) + { aOutputJson.push_back(std::move(aCard)); } } diff --git a/battleNetApi/tests/src/hearthstoneApiTests.cpp b/battleNetApi/tests/src/hearthstoneApiTests.cpp index 192f38c..c3dfc59 100644 --- a/battleNetApi/tests/src/hearthstoneApiTests.cpp +++ b/battleNetApi/tests/src/hearthstoneApiTests.cpp @@ -15,7 +15,9 @@ #include #include -TEST_CASE("Retrieve number of cards", "[countCards]") { +TEST_CASE("Retrieve number of cards", + "[countCards]") +{ const std::string aContentType{"cards"}; const std::string aRegion{"eu"}; bnet::ApiUrl aApiUrl(aContentType, aRegion); @@ -37,7 +39,9 @@ TEST_CASE("Retrieve number of cards", "[countCards]") { REQUIRE(aNumberOfCards >= aExpectedNumberOfCards); } -TEST_CASE("Retrieve page once", "[retrievePageOnce]") { +TEST_CASE("Retrieve page once", + "[retrievePageOnce]") +{ const std::string aContentType{"cards"}; const std::string aRegion{"eu"}; bnet::ApiUrl aApiUrl(aContentType, aRegion); @@ -55,14 +59,16 @@ TEST_CASE("Retrieve page once", "[retrievePageOnce]") { REQUIRE(aApiResponse["cards"].size() == 16); } -TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoParallel]") { +TEST_CASE("Retrieve pages 10 times without parallelization", + "[retrievePagesNoParallel]") +{ const std::string aContentType{"cards"}; const std::string aRegion{"eu"}; bnet::ApiUrl aApiUrl(aContentType, aRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize(16); - aApiUrl.setPage("1"); + aApiUrl.setPage(1); bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() .allowUpdateSourceFile() @@ -76,23 +82,26 @@ TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoPa const int aNumberOfRetrievals{10}; const int aNumberOfPages{aApiResponse["pageCount"]}; REQUIRE(aNumberOfPages >= aNumberOfRetrievals); - for (int i{1}; i < aNumberOfRetrievals; ++i) { + for (int i{1}; i < aNumberOfRetrievals; ++i) + { REQUIRE(aApiResponse["cards"].size() == 16); - aApiUrl.setPage(std::to_string(i + 1)); + aApiUrl.setPage(i + 1); aApiResponse = bnet::HearthstoneApi::makeApiRequest(aApiUrl, aTokenText); } REQUIRE(aApiResponse["cards"].size() >= 1); REQUIRE(aApiResponse["cards"].size() <= 16); } -TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParallel]") { +TEST_CASE("Retrieve pages 10 times with parallelization", + "[retrievePagesParallel]") +{ const std::string aContentType{"cards"}; const std::string aRegion{"eu"}; bnet::ApiUrl aApiUrl(aContentType, aRegion); aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize(16); - aApiUrl.setPage("1"); + aApiUrl.setPage(1); std::vector aRequests; bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json"); const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall() @@ -106,27 +115,32 @@ TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParalle const int aNumberOfPages{aFirstPageJson["pageCount"]}; const int aNumberOfRetrievals{10}; REQUIRE(aNumberOfPages >= aNumberOfRetrievals); - for (int i{1}; i < aNumberOfRetrievals; ++i) { - aApiUrl.setPage(std::to_string(i + 1)); + for (int i{1}; i < aNumberOfRetrievals; ++i) + { + aApiUrl.setPage(i + 1); aRequests.emplace_back(aApiUrl.text()); } std::vector aApiResponses = bnet::HearthstoneApi::makeApiRequest(aRequests, aTokenText); aApiResponses.emplace(aApiResponses.begin(), aFirstPageJson); REQUIRE(aApiResponses.size() == aNumberOfRetrievals); - for (const nlohmann::json &aApiResponse : aApiResponses) { + for (const nlohmann::json& aApiResponse : aApiResponses) + { REQUIRE(aApiResponse["cards"].size() == 16); } } -TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") { +TEST_CASE("Merge two card lists from two page requests", + "[Merge pages]") +{ std::ifstream aFirstPageFile{"resources/page_merge_00.json"}; nlohmann::json aFirstPageJson = nlohmann::json::parse(aFirstPageFile); std::ifstream aSecondPageFile{"resources/page_merge_01.json"}; const nlohmann::json aSecondPageJson = nlohmann::json::parse(aSecondPageFile); nlohmann::json::reference aMergedCards = aFirstPageJson["cards"]; - for (auto &card : aSecondPageJson["cards"]) { + for (auto& card : aSecondPageJson["cards"]) + { aMergedCards.emplace_back(std::move(card)); } REQUIRE(aMergedCards.size() == 32); @@ -135,7 +149,8 @@ TEST_CASE("Merge two card lists from two page requests", "[Merge pages]") { } TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards", - "[retrievePageOnceHearthstoneApi]") { + "[retrievePageOnceHearthstoneApi]") +{ const std::string aContentType{"cards"}; const std::string aRegion{"eu"}; bnet::ApiUrl aApiUrl(aContentType, aRegion); -- cgit v1.2.3