From cc436cb44c60ef2485321e1e22b3ea0a59ee22af Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Sun, 19 Apr 2026 18:16:14 +0200 Subject: TokenPool now works --- battleNetApi/src/apiUrl.cpp | 22 ++++++++++++---------- battleNetApi/src/hearthstoneApi.cpp | 5 ++++- battleNetApi/src/main.cpp | 6 +++--- battleNetApi/src/tokenPool.cpp | 17 +++++++++++------ 4 files changed, 30 insertions(+), 20 deletions(-) (limited to 'battleNetApi/src') diff --git a/battleNetApi/src/apiUrl.cpp b/battleNetApi/src/apiUrl.cpp index ac8171b..1f11add 100644 --- a/battleNetApi/src/apiUrl.cpp +++ b/battleNetApi/src/apiUrl.cpp @@ -1,4 +1,5 @@ #include "apiUrl.hpp" +#include #include ApiUrl::ApiUrl(const std::string &iContentType, const std::string &iRegion) @@ -12,35 +13,36 @@ void ApiUrl::setLocale(const std::string &iLocale) { if (not iLocale.empty()) { //!< The locale to reflect in localized data, e.g. "fr_FR" or "ko_KR" //!< If you do not supply a value, all translations are returned. - const std::string kKey{"locale"}; - setValue(kKey, iLocale); + setValue(kLOCALE_KEY, iLocale); } } void ApiUrl::setGameMode(const std::string &iGameMode) { if (not iGameMode.empty()) { //!< A recognized game mode, e.g. "constructed" //!< @see https://community.developer.battle.net/documentation/hearthstone/guides/game-modes - const std::string kKey{"gameMode"}; - setValue(kKey, iGameMode); + setValue(kGAME_MODE_KEY, iGameMode); } } void ApiUrl::setPage(const std::string &iPage) { if (not iPage.empty()) { //!< The page to retrieve, e.g. "9" - const std::string kKey{"page"}; - setValue(kKey, iPage); + setValue(kPAGE_KEY, iPage); } } +void ApiUrl::setPage(uint64_t iPage) { setPage(std::to_string(iPage)); } void ApiUrl::setPageSize(const std::string &iPageSize) { if (not iPageSize.empty()) { //!< The number of results to choose per page, e.g. "40" - const std::string kKey{"pageSize"}; - setValue(kKey, iPageSize); + setValue(kPAGE_SIZE_KEY, iPageSize); } } - +void ApiUrl::setPageSize(uint64_t iPageSize) { return setPageSize(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::hasValue(const std::string &iKey) const { return aParameters.count(iKey) > 0; } std::string ApiUrl::text() const { std::string aFormattedParameters; for (const auto &[key, value] : aParameters) { diff --git a/battleNetApi/src/hearthstoneApi.cpp b/battleNetApi/src/hearthstoneApi.cpp index d6becc1..133f38d 100644 --- a/battleNetApi/src/hearthstoneApi.cpp +++ b/battleNetApi/src/hearthstoneApi.cpp @@ -104,7 +104,10 @@ std::vector HearthstoneApi::makeApiRequest(const std::vector return aJsonOutputs; } -json HearthstoneApi::downloadCards(ApiUrl &iApiUrl, Token &iToken) { +json HearthstoneApi::downloadCards(ApiUrl iApiUrl, const Token &iToken) { + if (iApiUrl.hasPage()) { + return makeApiRequest(iApiUrl, iToken.text()); + } iApiUrl.setPage("1"); std::vector aRequests; diff --git a/battleNetApi/src/main.cpp b/battleNetApi/src/main.cpp index 4a5594c..2b6596a 100644 --- a/battleNetApi/src/main.cpp +++ b/battleNetApi/src/main.cpp @@ -21,7 +21,7 @@ class DefaultDownloadMethod : public DownloadMethod { aApiUrl.setLocale("fr_FR"); aApiUrl.setGameMode("constructed"); aApiUrl.setPageSize("100"); - std::optional aToken{TokenPool::unusedToken()}; + std::optional aToken{TokenPool::availableToken()}; if (aToken.has_value()) { HearthstoneApi::downloadCards(aApiUrl, aToken.value()); } @@ -51,12 +51,12 @@ int main(int argc, char *argv[]) { DownloaderContext aDownloaderContext; std::string aOutputJsonPath; if (argc < 4) { - TokenPool::fillTokenSource("tests/resources/credentials.json"); + TokenPool::feedTokenSource("tests/resources/credentials.json"); aDownloaderContext.setDownloadMethod(std::make_unique()); aOutputJsonPath = "tests/output/list_of_cards.json"; } else { std::string aCredentialsFilePath{argv[1]}; - TokenPool::fillTokenSource(aCredentialsFilePath); + TokenPool::feedTokenSource(aCredentialsFilePath); aOutputJsonPath = argv[2]; } std::ofstream aOutputJsonFile(aOutputJsonPath); diff --git a/battleNetApi/src/tokenPool.cpp b/battleNetApi/src/tokenPool.cpp index f8a6256..08770e8 100644 --- a/battleNetApi/src/tokenPool.cpp +++ b/battleNetApi/src/tokenPool.cpp @@ -1,13 +1,18 @@ #include -std::optional TokenPool::unusedToken() { - if (mTokenContainer.empty()) { +std::optional TokenPool::availableToken() { + if (aTokenContainer.empty()) { return std::nullopt; } - Token aOutputToken{mTokenContainer.front()}; - mTokenContainer.pop(); - return aOutputToken.text(); + Token aOutputToken{aTokenContainer.front()}; + aTokenContainer.pop(); + return aOutputToken; } -void TokenPool::fillTokenSource(const std::string &iJsonPath) { // mTokenContainer.emplace(Token(iJsonPath)); +void TokenPool::feedTokenSource(const std::string &iJsonPath) { aTokenContainer.emplace(iJsonPath); } + +void TokenPool::feedTokenSource(const std::span &iJsonPaths) { + for (const std::string &iJsonPath : iJsonPaths) { + feedTokenSource(iJsonPath); + } } -- cgit v1.2.3