summaryrefslogtreecommitdiff
path: root/battleNetApi/src
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-04-19 18:16:14 +0200
committerAotrix <[email protected]>2026-04-19 18:16:14 +0200
commitcc436cb44c60ef2485321e1e22b3ea0a59ee22af (patch)
treeb04464945278bcdc8915d8b3676b504a95e3f701 /battleNetApi/src
parent8837de4783b25255b6a1799be210c9e580dd2d4f (diff)
TokenPool now works
Diffstat (limited to 'battleNetApi/src')
-rw-r--r--battleNetApi/src/apiUrl.cpp22
-rw-r--r--battleNetApi/src/hearthstoneApi.cpp5
-rw-r--r--battleNetApi/src/main.cpp6
-rw-r--r--battleNetApi/src/tokenPool.cpp17
4 files changed, 30 insertions, 20 deletions
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 <cstdint>
#include <format>
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<json> HearthstoneApi::makeApiRequest(const std::vector<std::string>
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<std::string> 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<Token> aToken{TokenPool::unusedToken()};
+ std::optional<Token> 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<DefaultDownloadMethod>());
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 <tokenPool.hpp>
-std::optional<std::string> TokenPool::unusedToken() {
- if (mTokenContainer.empty()) {
+std::optional<Token> 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<std::string> &iJsonPaths) {
+ for (const std::string &iJsonPath : iJsonPaths) {
+ feedTokenSource(iJsonPath);
+ }
}