summaryrefslogtreecommitdiff
path: root/battleNetApi
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-04-28 03:49:41 +0200
committerAotrix <[email protected]>2026-04-28 03:49:41 +0200
commita0632d299b85dc4ef7c0e03898f38e6deca0edc2 (patch)
treecc83712ca644e8b5d1b9e1c0c49bdbdd8475d0a9 /battleNetApi
parent5582e994d39b16a6d61f05b13e78c30f4bac5d84 (diff)
add HearthstoneTokenBuilder class
Diffstat (limited to 'battleNetApi')
-rw-r--r--battleNetApi/CMakeLists.txt4
-rw-r--r--battleNetApi/include/hearthstoneTokenBuilder.hpp43
-rw-r--r--battleNetApi/include/token.hpp25
-rw-r--r--battleNetApi/include/tokenBuilder.hpp14
-rw-r--r--battleNetApi/include/tokenPool.hpp20
-rw-r--r--battleNetApi/src/hearthstoneTokenBuilder.cpp140
-rw-r--r--battleNetApi/src/main.cpp32
-rw-r--r--battleNetApi/src/token.cpp121
-rw-r--r--battleNetApi/src/tokenBuilder.cpp13
-rw-r--r--battleNetApi/src/tokenPool.cpp20
-rw-r--r--battleNetApi/tests/tests.cpp16
11 files changed, 225 insertions, 223 deletions
diff --git a/battleNetApi/CMakeLists.txt b/battleNetApi/CMakeLists.txt
index 43f9f17..e662d13 100644
--- a/battleNetApi/CMakeLists.txt
+++ b/battleNetApi/CMakeLists.txt
@@ -18,7 +18,7 @@ set(SRC
${PROJECT_SOURCE_DIR}/src/hearthstoneApi.cpp
${PROJECT_SOURCE_DIR}/src/token.cpp
${PROJECT_SOURCE_DIR}/src/tokenBuilder.cpp
- ${PROJECT_SOURCE_DIR}/src/tokenPool.cpp
+ ${PROJECT_SOURCE_DIR}/src/hearthstoneTokenBuilder.cpp
${PROJECT_SOURCE_DIR}/src/apiUrl.cpp
${PROJECT_SOURCE_DIR}/src/main.cpp
)
@@ -26,7 +26,7 @@ set(SRC_TESTS
${PROJECT_SOURCE_DIR}/src/hearthstoneApi.cpp
${PROJECT_SOURCE_DIR}/src/token.cpp
${PROJECT_SOURCE_DIR}/src/tokenBuilder.cpp
- ${PROJECT_SOURCE_DIR}/src/tokenPool.cpp
+ ${PROJECT_SOURCE_DIR}/src/hearthstoneTokenBuilder.cpp
${PROJECT_SOURCE_DIR}/src/apiUrl.cpp
${PROJECT_SOURCE_DIR}/tests/tests.cpp
)
diff --git a/battleNetApi/include/hearthstoneTokenBuilder.hpp b/battleNetApi/include/hearthstoneTokenBuilder.hpp
new file mode 100644
index 0000000..488aff4
--- /dev/null
+++ b/battleNetApi/include/hearthstoneTokenBuilder.hpp
@@ -0,0 +1,43 @@
+#include <chrono>
+#include <cstdint>
+#include <nlohmann/json.hpp>
+#include <string>
+#include <token.hpp>
+#include <tokenBuilder.hpp>
+#include <vector>
+
+using nlohmann::json;
+
+static constexpr std::uint64_t kTOKEN_CONTENT_MIN_LENGTH{10};
+
+class HearthstoneTokenBuilder : TokenBuilder {
+ private:
+ std::shared_ptr<Token> mCurrentToken;
+ bool mUpdateSourceFile{false};
+ std::vector<std::string> mJsonPaths;
+ bool isUpdateSourceFileAllowed() const;
+ const bool hasExpired(json iToken) const;
+ static size_t curlCallbackStringOutput(void *iPtr, size_t iSize,
+ size_t iNmemb,
+ std::string *iUserData);
+ std::chrono::seconds getCurrentTime() const;
+ std::pair<std::string, std::uint64_t> getTokenWithApi() const;
+ std::string getTokenFromLocalFile() const;
+ void writeTokenToLocalFile(
+ const std::pair<std::string, std::uint64_t> &iToken) const;
+
+ public:
+ HearthstoneTokenBuilder(const std::span<std::string> &iJsonPaths);
+ HearthstoneTokenBuilder(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &addJsonPath(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &addJsonPaths(const std::string &iJsonPath);
+ HearthstoneTokenBuilder &allowUpdateSourceFile();
+ /**
+ * @return Create an access token to perform API requests.
+ * First, the cache files are checked.
+ * A new token is computed if the token lifetimes expired,
+ * or if there is no token at all.
+ */
+ HearthstoneTokenBuilder &buildToken() override;
+ std::shared_ptr<Token> getToken() override;
+};
diff --git a/battleNetApi/include/token.hpp b/battleNetApi/include/token.hpp
index b913b2b..cc909a8 100644
--- a/battleNetApi/include/token.hpp
+++ b/battleNetApi/include/token.hpp
@@ -1,32 +1,13 @@
#pragma once
-#include <chrono>
#include <string>
class Token {
public:
- explicit Token(const std::string &iTokenFilePath);
+ explicit Token(const std::string &iTokenText);
std::string text() const;
- const bool hasExpired() const;
+ void setText(const std::string &iTokenText);
private:
- const std::string mFilePath; //!< Path to the token file
- const std::string mUsername; //!< Username for Battle.net API
- const std::string mPassword; //!< Password for Battle.net API
- std::string mToken; //!< Token value
-
- /**
- * @return An access token that a user can use to perform API requests.
- * First, the cache file is checked.
- * A new token is computed if the current token lifetime expired,
- * or if there is no token at all.
- */
- std::string generateToken();
- void writeTokenToLocalFile(const std::pair<std::string, std::uint64_t> &iToken) const;
- std::string getTokenFromLocalFile() const;
- std::pair<std::string, std::uint64_t> getTokenWithApi() const;
- std::chrono::seconds getCurrentTime() const;
- static size_t curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData);
-
- static const std::uint16_t kTokenContentMinLength{10};
+ std::string mTokenText; //!< Token value
};
diff --git a/battleNetApi/include/tokenBuilder.hpp b/battleNetApi/include/tokenBuilder.hpp
index 71286cd..2893728 100644
--- a/battleNetApi/include/tokenBuilder.hpp
+++ b/battleNetApi/include/tokenBuilder.hpp
@@ -1,12 +1,12 @@
+#pragma once
+
+#include <memory>
#include <token.hpp>
class TokenBuilder {
public:
- TokenBuilder();
- [[nodiscard]]
- Token build() const;
- TokenBuilder &allowSourceFileUpdate();
-
- private:
- bool mAllowSourceFileUpdate{false};
+ TokenBuilder() = default;
+ virtual ~TokenBuilder() = default;
+ virtual TokenBuilder &buildToken() = 0;
+ virtual std::shared_ptr<Token> getToken() = 0;
};
diff --git a/battleNetApi/include/tokenPool.hpp b/battleNetApi/include/tokenPool.hpp
deleted file mode 100644
index 0b1e4cc..0000000
--- a/battleNetApi/include/tokenPool.hpp
+++ /dev/null
@@ -1,20 +0,0 @@
-#pragma once
-
-#include <queue>
-#include <token.hpp>
-
-static std::queue<Token> aTokenContainer;
-
-class TokenPool {
- private:
- TokenPool() = delete;
- TokenPool(const TokenPool &) = delete;
- TokenPool(TokenPool &&) = delete;
- TokenPool &operator=(const TokenPool &) = delete;
- TokenPool &operator=(TokenPool &&) = delete;
-
- public:
- static void feedTokenSource(const std::span<std::string> &iJsonPaths);
- static void feedTokenSource(const std::string &iJsonPath);
- static std::optional<Token> availableToken();
-};
diff --git a/battleNetApi/src/hearthstoneTokenBuilder.cpp b/battleNetApi/src/hearthstoneTokenBuilder.cpp
new file mode 100644
index 0000000..8403f46
--- /dev/null
+++ b/battleNetApi/src/hearthstoneTokenBuilder.cpp
@@ -0,0 +1,140 @@
+#include <curl/curl.h>
+#include <hearthstoneTokenBuilder.hpp>
+#include <iostream>
+#include <memory>
+
+HearthstoneTokenBuilder::HearthstoneTokenBuilder(const std::string &iJsonPath) {
+ mJsonPaths.push_back(iJsonPath);
+}
+HearthstoneTokenBuilder::HearthstoneTokenBuilder(
+ const std::span<std::string> &iJsonPaths) {
+ mJsonPaths.append_range(iJsonPaths);
+}
+HearthstoneTokenBuilder &
+HearthstoneTokenBuilder::addJsonPath(const std::string &iJsonPath) {
+ mJsonPaths.push_back(iJsonPath);
+ return *this;
+}
+HearthstoneTokenBuilder &
+HearthstoneTokenBuilder::addJsonPaths(const std::string &iJsonPath) {
+ mJsonPaths.push_back(iJsonPath);
+ return *this;
+}
+HearthstoneTokenBuilder &HearthstoneTokenBuilder::buildToken() {
+ if (hasExpired()) {
+ const std::pair<std::string, std::uint64_t> kTokenContent{
+ getTokenWithApi()};
+ writeTokenToLocalFile(kTokenContent);
+ }
+ // return getTokenFromLocalFile();
+ if (aTokenContainer.empty()) {
+ return std::nullopt;
+ }
+ Token aOutputToken{aTokenContainer.front()};
+ aTokenContainer.pop();
+ // mCurrentToken = std::make_unique<Token>(...);
+ return *this;
+}
+std::shared_ptr<Token> HearthstoneTokenBuilder::getToken() {
+ return mCurrentToken;
+}
+HearthstoneTokenBuilder &HearthstoneTokenBuilder::allowUpdateSourceFile() {
+ mUpdateSourceFile = true;
+ return *this;
+}
+bool HearthstoneTokenBuilder::isUpdateSourceFileAllowed() const {
+ return mUpdateSourceFile;
+}
+
+const bool HearthstoneTokenBuilder::hasExpired(json iCredentials) const {
+ if (not iCredentials.contains("Token") or
+ not iCredentials["Token"].contains("ExpirationTime") or
+ not iCredentials["Token"].contains("Content")) {
+ return true;
+ }
+ if (iCredentials["Token"]["Content"].dump().length() <
+ kTOKEN_CONTENT_MIN_LENGTH) {
+ return true;
+ }
+ const std::uint64_t kExpirationTime{
+ iCredentials["Token"]["ExpirationTime"]};
+ const std::uint64_t kCurrentTime{static_cast<std::uint64_t>(
+ std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::high_resolution_clock::now().time_since_epoch())
+ .count())};
+ return kCurrentTime >= kExpirationTime;
+}
+
+size_t HearthstoneTokenBuilder::curlCallbackStringOutput(
+ void *iPtr, size_t iSize, size_t iNmemb, std::string *iUserData) {
+ size_t newLength{iSize * iNmemb};
+ try {
+ iUserData->append(static_cast<char *>(iPtr), newLength);
+ } catch (std::bad_alloc &e) {
+ // handle memory problem
+ return 0;
+ }
+ return newLength;
+}
+
+std::chrono::seconds HearthstoneTokenBuilder::getCurrentTime() const {
+ return std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::utc_clock::now().time_since_epoch());
+}
+
+std::pair<std::string, std::uint64_t>
+HearthstoneTokenBuilder::getTokenWithApi() const {
+ std::ifstream aCredentialsFile{mFilePath};
+ json aCredentials = json::parse(aCredentialsFile);
+ if (not aCredentials.contains("Agent") or
+ not aCredentials["Agent"].contains("Username") or
+ not aCredentials["Agent"].contains("Password")) {
+ std::cerr << "Please define Agent/Username and Agent/Password in the "
+ "JSON credentials file\n";
+ std::exit(EXIT_FAILURE);
+ }
+ const std::string kAgentUsername{aCredentials["Agent"]["Username"]};
+ const std::string kAgentPassword{aCredentials["Agent"]["Password"]};
+ CURL *aCurl{curl_easy_init()};
+ std::string aTokenInfo;
+ if (aCurl and not kAgentUsername.empty() and not kAgentPassword.empty()) {
+ curl_easy_setopt(aCurl, CURLOPT_URL, "https://oauth.battle.net/token");
+ curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYPEER, 0L);
+ curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYHOST, 0L);
+ curl_easy_setopt(aCurl, CURLOPT_WRITEFUNCTION,
+ curlCallbackStringOutput);
+ curl_easy_setopt(aCurl, CURLOPT_WRITEDATA, &aTokenInfo);
+ curl_easy_setopt(
+ aCurl, CURLOPT_USERPWD,
+ std::format("{}:{}", kAgentUsername, kAgentPassword).data());
+ curl_easy_setopt(aCurl, CURLOPT_POSTFIELDS,
+ "grant_type=client_credentials");
+ CURLcode result_code{curl_easy_perform(aCurl)};
+ curl_easy_cleanup(aCurl);
+ if (result_code != CURLE_OK) {
+ std::cerr << "ERROR: token request failed" << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ }
+ json aTokenJson = json::parse(aTokenInfo);
+ return std::make_pair(aTokenJson["access_token"], aTokenJson["expires_in"]);
+}
+
+std::string HearthstoneTokenBuilder::getTokenFromLocalFile() const {
+ std::ifstream aTokenFile{mFilePath};
+ return json::parse(aTokenFile)["Token"]["Content"];
+}
+
+void HearthstoneTokenBuilder::writeTokenToLocalFile(
+ const std::pair<std::string, std::uint64_t> &iToken) const {
+ std::ifstream aTokenFileInput{mFilePath};
+ json aCredentialsFile = json::parse(aTokenFileInput);
+ aCredentialsFile["Token"]["Content"] = iToken.first;
+ aCredentialsFile["Token"]["ExpirationTime"] =
+ std::chrono::duration_cast<std::chrono::seconds>(
+ getCurrentTime() + std::chrono::seconds(iToken.second))
+ .count();
+
+ std::ofstream aTokenFileOutput{mFilePath};
+ aTokenFileOutput << std::setw(4) << aCredentialsFile;
+}
diff --git a/battleNetApi/src/main.cpp b/battleNetApi/src/main.cpp
index ccab84c..dd93daa 100644
--- a/battleNetApi/src/main.cpp
+++ b/battleNetApi/src/main.cpp
@@ -1,12 +1,14 @@
#include "apiUrl.hpp"
#include <fstream>
#include <hearthstoneApi.hpp>
+#include <hearthstoneTokenBuilder.hpp>
#include <iostream>
-#include <tokenPool.hpp>
+#include <memory>
+#include <tokenBuilder.hpp>
class DownloadMethod {
public:
- virtual json download() = 0;
+ virtual json download(const std::shared_ptr<Token> &iToken) = 0;
virtual ~DownloadMethod() = default;
};
@@ -14,16 +16,15 @@ class DefaultDownloadMethod : public DownloadMethod {
public:
DefaultDownloadMethod() = default;
~DefaultDownloadMethod() = default;
- json download() override {
+ json download(const std::shared_ptr<Token> &iToken) override {
const std::string kContentType{"cards"};
const std::string kRegion{"eu"};
ApiUrl aApiUrl(kContentType, kRegion);
aApiUrl.setLocale("fr_FR");
aApiUrl.setGameMode("constructed");
- aApiUrl.setPageSize("100");
- std::optional<Token> aToken{TokenPool::availableToken()};
- if (aToken.has_value()) {
- return HearthstoneApi::downloadCards(aApiUrl, aToken.value());
+ aApiUrl.setPageSize(100);
+ if (iToken) {
+ return HearthstoneApi::downloadCards(aApiUrl, *iToken);
}
return json({});
};
@@ -37,9 +38,9 @@ class DownloaderContext {
void setDownloadMethod(std::unique_ptr<DownloadMethod> iDownloader) {
mDownloader = std::move(iDownloader);
}
- json download() {
+ json download(const std::shared_ptr<Token> &iToken) {
if (mDownloader) {
- return mDownloader->download();
+ return mDownloader->download(iToken);
} else {
std::cerr << "No download method available in DownloaderContext\n";
return json({});
@@ -52,20 +53,25 @@ class DownloaderContext {
int main(int argc, char *argv[]) {
DownloaderContext aDownloaderContext;
+ std::string aCredentialsFilePath;
std::string aOutputJsonPath;
if (argc < 3) {
- TokenPool::feedTokenSource("tests/resources/credentials.json");
+ aCredentialsFilePath = "tests/resources/credentials.json";
aDownloaderContext.setDownloadMethod(
std::make_unique<DefaultDownloadMethod>());
aOutputJsonPath = "tests/output/list_of_cards.json";
} else {
- std::string aCredentialsFilePath{argv[1]};
- TokenPool::feedTokenSource(aCredentialsFilePath);
+ aCredentialsFilePath = argv[1];
aDownloaderContext.setDownloadMethod(
std::make_unique<DefaultDownloadMethod>());
aOutputJsonPath = argv[2];
}
+ HearthstoneTokenBuilder aHearthstoneTokenBuilder(aCredentialsFilePath);
std::ofstream aOutputJsonFile(aOutputJsonPath);
- aOutputJsonFile << aDownloaderContext.download().dump(4) << '\n';
+ aOutputJsonFile << aDownloaderContext
+ .download(
+ aHearthstoneTokenBuilder.buildToken().getToken())
+ .dump(4)
+ << '\n';
return 0;
}
diff --git a/battleNetApi/src/token.cpp b/battleNetApi/src/token.cpp
index 1f952ff..b312cc1 100644
--- a/battleNetApi/src/token.cpp
+++ b/battleNetApi/src/token.cpp
@@ -1,122 +1,7 @@
#include "token.hpp"
-#include <chrono>
-#include <cstdint>
-#include <curl/curl.h>
-#include <format>
-#include <fstream>
-#include <iostream>
-#include <nlohmann/json.hpp>
-#include <utility>
-using json = nlohmann::json;
+Token::Token(const std::string &iTokenText) : mTokenText{iTokenText} {}
-Token::Token(const std::string &iTokenFilePath)
- : mFilePath{iTokenFilePath}, mToken{generateToken()} {}
+std::string Token::text() const { return mTokenText; }
-std::string Token::text() const { return mToken; }
-
-const bool Token::hasExpired() const {
- std::ifstream aTokenFile{mFilePath};
- json aCredentialsFile = json::parse(aTokenFile);
- if (not aCredentialsFile.contains("Token") or
- not aCredentialsFile["Token"].contains("ExpirationTime") or
- not aCredentialsFile["Token"].contains("Content")) {
- return true;
- }
- if (aCredentialsFile["Token"]["Content"].dump().length() <
- kTokenContentMinLength) {
- return true;
- }
- const std::uint64_t kExpirationTime{
- aCredentialsFile["Token"]["ExpirationTime"]};
- const std::uint64_t kCurrentTime{static_cast<std::uint64_t>(
- std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::high_resolution_clock::now().time_since_epoch())
- .count())};
- if (kCurrentTime >= kExpirationTime) {
- return true;
- }
- return false;
-}
-
-std::string Token::getTokenFromLocalFile() const {
- std::ifstream aTokenFile{mFilePath};
- return json::parse(aTokenFile)["Token"]["Content"];
-}
-
-std::string Token::generateToken() {
- if (hasExpired()) {
- const std::pair<std::string, std::uint64_t> kTokenContent{
- getTokenWithApi()};
- writeTokenToLocalFile(kTokenContent);
- }
- return getTokenFromLocalFile();
-}
-
-void Token::writeTokenToLocalFile(
- const std::pair<std::string, std::uint64_t> &iToken) const {
- std::ifstream aTokenFileInput{mFilePath};
- json aCredentialsFile = json::parse(aTokenFileInput);
- aCredentialsFile["Token"]["Content"] = iToken.first;
- aCredentialsFile["Token"]["ExpirationTime"] =
- std::chrono::duration_cast<std::chrono::seconds>(
- getCurrentTime() + std::chrono::seconds(iToken.second))
- .count();
-
- std::ofstream aTokenFileOutput{mFilePath};
- aTokenFileOutput << std::setw(4) << aCredentialsFile;
-}
-
-std::pair<std::string, std::uint64_t> Token::getTokenWithApi() const {
- std::ifstream aCredentialsFile{mFilePath};
- json aCredentials = json::parse(aCredentialsFile);
- if (not aCredentials.contains("Agent") or
- not aCredentials["Agent"].contains("Username") or
- not aCredentials["Agent"].contains("Password")) {
- std::cerr << "Please define Agent/Username and Agent/Password in the "
- "JSON credentials file\n";
- std::exit(EXIT_FAILURE);
- }
- const std::string kAgentUsername{aCredentials["Agent"]["Username"]};
- const std::string kAgentPassword{aCredentials["Agent"]["Password"]};
- CURL *aCurl{curl_easy_init()};
- std::string aTokenInfo;
- if (aCurl and not kAgentUsername.empty() and not kAgentPassword.empty()) {
- curl_easy_setopt(aCurl, CURLOPT_URL, "https://oauth.battle.net/token");
- curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(aCurl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_easy_setopt(aCurl, CURLOPT_WRITEFUNCTION,
- curlCallbackStringOutput);
- curl_easy_setopt(aCurl, CURLOPT_WRITEDATA, &aTokenInfo);
- curl_easy_setopt(
- aCurl, CURLOPT_USERPWD,
- std::format("{}:{}", kAgentUsername, kAgentPassword).data());
- curl_easy_setopt(aCurl, CURLOPT_POSTFIELDS,
- "grant_type=client_credentials");
- CURLcode result_code{curl_easy_perform(aCurl)};
- curl_easy_cleanup(aCurl);
- if (result_code != CURLE_OK) {
- std::cerr << "ERROR: token request failed" << std::endl;
- std::exit(EXIT_FAILURE);
- }
- }
- json aTokenJson = json::parse(aTokenInfo);
- return std::make_pair(aTokenJson["access_token"], aTokenJson["expires_in"]);
-}
-
-std::chrono::seconds Token::getCurrentTime() const {
- return std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::utc_clock::now().time_since_epoch());
-}
-
-size_t Token::curlCallbackStringOutput(void *iPtr, size_t iSize, size_t iNmemb,
- std::string *iUserData) {
- size_t newLength{iSize * iNmemb};
- try {
- iUserData->append(static_cast<char *>(iPtr), newLength);
- } catch (std::bad_alloc &e) {
- // handle memory problem
- return 0;
- }
- return newLength;
-}
+void Token::setText(const std::string &iTokenText) { mTokenText = iTokenText; }
diff --git a/battleNetApi/src/tokenBuilder.cpp b/battleNetApi/src/tokenBuilder.cpp
index 733f5c1..9e2a2f0 100644
--- a/battleNetApi/src/tokenBuilder.cpp
+++ b/battleNetApi/src/tokenBuilder.cpp
@@ -1,14 +1 @@
#include <tokenBuilder.hpp>
-
-TokenBuilder::TokenBuilder() {
- static_assert(
- false, "'TokenBuilder::TokenBuilder()' implementation is not complete");
-}
-Token TokenBuilder::build() const {
- static_assert(false,
- "'TokenBuilder::build()' implementation is not complete");
-}
-TokenBuilder &TokenBuilder::allowSourceFileUpdate() {
- mAllowSourceFileUpdate = true;
- return *this;
-}
diff --git a/battleNetApi/src/tokenPool.cpp b/battleNetApi/src/tokenPool.cpp
deleted file mode 100644
index b74aae5..0000000
--- a/battleNetApi/src/tokenPool.cpp
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <tokenPool.hpp>
-
-std::optional<Token> TokenPool::availableToken() {
- if (aTokenContainer.empty()) {
- return std::nullopt;
- }
- Token aOutputToken{aTokenContainer.front()};
- aTokenContainer.pop();
- return aOutputToken;
-}
-
-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);
- }
-}
diff --git a/battleNetApi/tests/tests.cpp b/battleNetApi/tests/tests.cpp
index b79a37b..10c6f23 100644
--- a/battleNetApi/tests/tests.cpp
+++ b/battleNetApi/tests/tests.cpp
@@ -12,7 +12,7 @@
#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
-#include <tokenPool.hpp>
+#include <tokenBuilder.hpp>
TEST_CASE("Retrieve API Token", "[generateApiToken]") {
const std::string kCredentialsFile{"tests/resources/credentials.json"};
@@ -33,7 +33,7 @@ TEST_CASE("Retrieve number of cards", "[countCards]") {
aApiUrl.setPageSize("1");
TokenPool::feedTokenSource("tests/resources/credentials.json");
const json kResponse = HearthstoneApi::makeApiRequest(
- aApiUrl, TokenPool::availableToken().value().text());
+ aApiUrl, TokenPool::build().value().text());
REQUIRE(kResponse.size() > 0);
REQUIRE(kResponse.contains("pageCount"));
std::uint64_t kNumberOfCards{kResponse["pageCount"]};
@@ -52,7 +52,7 @@ TEST_CASE("Retrieve page once", "[retrievePageOnce]") {
aApiUrl.setPageSize("16");
TokenPool::feedTokenSource("tests/resources/credentials.json");
const json kApiResponse = HearthstoneApi::makeApiRequest(
- aApiUrl, TokenPool::availableToken().value().text());
+ aApiUrl, TokenPool::build().value().text());
REQUIRE(kApiResponse["cards"].size() == 16);
}
@@ -68,7 +68,7 @@ TEST_CASE("Retrieve pages 10 times without parallelization",
aApiUrl.setPageSize("16");
aApiUrl.setPage("1");
TokenPool::feedTokenSource("tests/resources/credentials.json");
- std::string kTokenText{TokenPool::availableToken().value().text()};
+ std::string kTokenText{TokenPool::build().value().text()};
json kApiResponse = HearthstoneApi::makeApiRequest(aApiUrl, kTokenText);
// Retrieve number of pages
@@ -97,7 +97,7 @@ TEST_CASE("Retrieve pages 10 times with parallelization",
aApiUrl.setPage("1");
std::vector<std::string> aRequests;
TokenPool::feedTokenSource("tests/resources/credentials.json");
- std::string kTokenText{TokenPool::availableToken().value().text()};
+ std::string kTokenText{TokenPool::build().value().text()};
json kFirstPageJson = HearthstoneApi::makeApiRequest(aApiUrl, kTokenText);
int kNumberOfPages{kFirstPageJson["pageCount"]};
@@ -139,11 +139,11 @@ TEST_CASE("Retrieve a single page with HearthstoneApi::downloadCards",
aApiUrl.setPage(3);
TokenPool::feedTokenSource("tests/resources/credentials.json");
json kApiResponse = HearthstoneApi::makeApiRequest(
- aApiUrl, TokenPool::availableToken().value().text());
+ aApiUrl, TokenPool::build().value().text());
const json::size_type kExpectedNumberOfCards{kApiResponse["cards"].size()};
TokenPool::feedTokenSource("tests/resources/credentials.json");
- kApiResponse = HearthstoneApi::downloadCards(
- aApiUrl, TokenPool::availableToken().value());
+ kApiResponse =
+ HearthstoneApi::downloadCards(aApiUrl, TokenPool::build().value());
const json::size_type kCurrentNumberOfCards{kApiResponse["cards"].size()};
REQUIRE(kCurrentNumberOfCards == kExpectedNumberOfCards);
}