summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--battleNetApi/include/apiUrl.hpp3
-rw-r--r--battleNetApi/src/apiUrl.cpp34
-rw-r--r--battleNetApi/src/main.cpp50
-rw-r--r--battleNetApi/tests/src/hearthstoneApiTests.cpp8
4 files changed, 51 insertions, 44 deletions
diff --git a/battleNetApi/include/apiUrl.hpp b/battleNetApi/include/apiUrl.hpp
index ece4d17..1360102 100644
--- a/battleNetApi/include/apiUrl.hpp
+++ b/battleNetApi/include/apiUrl.hpp
@@ -4,7 +4,6 @@
#include <unordered_map>
namespace bnet {
-enum class GameMode { constructed, battlegrounds, mercenaries };
class ApiUrl {
public:
@@ -12,7 +11,7 @@ class ApiUrl {
void setRegion(const std::string &iRegion);
void setLocale(const std::string &iLocale);
- void setGameMode(const GameMode &iGameMode);
+ void setGameMode(const std::string &iGameMode);
void setPage(const std::string &iPage);
void setPage(uint64_t iPage);
void setPageSize(uint64_t iPageSize);
diff --git a/battleNetApi/src/apiUrl.cpp b/battleNetApi/src/apiUrl.cpp
index 5ef109c..8775943 100644
--- a/battleNetApi/src/apiUrl.cpp
+++ b/battleNetApi/src/apiUrl.cpp
@@ -3,41 +3,31 @@
#include <format>
namespace bnet {
-namespace {
-std::string gamemodeToString(const GameMode &iGameMode) {
- switch (iGameMode) {
- case GameMode::constructed:
- return "constructed";
- case GameMode::battlegrounds:
- return "battlegrounds";
- case GameMode::mercenaries:
- return "mercenaries";
- }
-}
-}; // namespace
ApiUrl::ApiUrl(const std::string &iContentType, const std::string &iRegion)
: mContentType{iContentType}, mRegion{iRegion} {}
void ApiUrl::setRegion(const std::string &iRegion) {
- if (not iRegion.empty()) {
+ if (!iRegion.empty()) {
mRegion = iRegion; //!< The region of the data to receive, e.g. "eu"
}
}
void ApiUrl::setLocale(const std::string &iLocale) {
- if (not iLocale.empty()) {
+ if (!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.
+ //!< If you do !supply a value, all translations are returned.
setValue(kLOCALE_KEY, iLocale);
}
}
-void ApiUrl::setGameMode(const GameMode &iGameMode) {
- //!< A recognized game mode, e.g. "constructed"
- //!< @see
- //!< https://community.developer.battle.net/documentation/hearthstone/guides/game-modes
- setValue(kGAME_MODE_KEY, gamemodeToString(iGameMode));
+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 (not iPage.empty()) {
+ if (!iPage.empty()) {
//!< The page to retrieve, e.g. "9"
setValue(kPAGE_KEY, iPage);
}
@@ -59,7 +49,7 @@ std::string ApiUrl::text() const {
for (const auto &[key, value] : aParameters) {
aFormattedParameters += std::format("&{}={}", key, value);
}
- if (not aFormattedParameters.empty()) {
+ if (!aFormattedParameters.empty()) {
aFormattedParameters[0] = '?';
}
aFormattedParameters = std::format("https://{}.api.blizzard.com/hearthstone/{}/{}", mRegion,
diff --git a/battleNetApi/src/main.cpp b/battleNetApi/src/main.cpp
index 64fa935..9d2ed86 100644
--- a/battleNetApi/src/main.cpp
+++ b/battleNetApi/src/main.cpp
@@ -4,12 +4,14 @@
#include <hearthstoneTokenBuilder.hpp>
#include <iostream>
#include <memory>
+#include <nlohmann/json.hpp>
#include <stdexcept>
#include <tokenBuilder.hpp>
class DownloadMethod {
public:
- virtual nlohmann::json download(const std::shared_ptr<bnet::Token> &iToken) = 0;
+ virtual nlohmann::json download(const bnet::ApiUrl &iApiUrl,
+ const std::shared_ptr<bnet::Token> &iToken) = 0;
virtual ~DownloadMethod() = default;
};
@@ -17,15 +19,10 @@ class DefaultDownloadMethod : public DownloadMethod {
public:
DefaultDownloadMethod() = default;
~DefaultDownloadMethod() = default;
- nlohmann::json download(const std::shared_ptr<bnet::Token> &iToken) override {
- const std::string kContentType{"cards"};
- const std::string kRegion{"eu"};
- bnet::ApiUrl aApiUrl(kContentType, kRegion);
- aApiUrl.setLocale("fr_FR");
- aApiUrl.setGameMode(bnet::GameMode::constructed);
- aApiUrl.setPageSize(100);
+ nlohmann::json download(const bnet::ApiUrl &iApiUrl,
+ const std::shared_ptr<bnet::Token> &iToken) override {
if (iToken) {
- return bnet::HearthstoneApi::downloadCards(aApiUrl, *iToken);
+ return bnet::HearthstoneApi::downloadCards(iApiUrl, *iToken);
}
return nlohmann::json({});
};
@@ -39,9 +36,10 @@ class DownloaderContext {
void setDownloadMethod(std::unique_ptr<DownloadMethod> iDownloader) {
mDownloader = std::move(iDownloader);
}
- nlohmann::json download(const std::shared_ptr<bnet::Token> &iToken) {
+ nlohmann::json download(const bnet::ApiUrl &iApiUrl,
+ const std::shared_ptr<bnet::Token> &iToken) {
if (mDownloader) {
- return mDownloader->download(iToken);
+ return mDownloader->download(iApiUrl, iToken);
} else {
std::cerr << "No download method available in DownloaderContext\n";
return nlohmann::json({});
@@ -52,16 +50,36 @@ class DownloaderContext {
std::unique_ptr<DownloadMethod> mDownloader;
};
+bnet::ApiUrl computeApiUrl(nlohmann::json &iApiUrl) {
+ if (!iApiUrl.contains("ContentType") || !iApiUrl.contains("Region")) {
+ throw std::invalid_argument("computeApiUrl: missing ContentType and Region parameters");
+ }
+ bnet::ApiUrl aApiUrl(iApiUrl["ContentType"], iApiUrl["Region"]);
+ if (iApiUrl.contains("Locale")) {
+ aApiUrl.setLocale(iApiUrl["Locale"]);
+ }
+ if (iApiUrl.contains("GameMode")) {
+ aApiUrl.setGameMode(iApiUrl["GameMode"]);
+ }
+ if (iApiUrl.contains("PageSize")) {
+ aApiUrl.setPageSize(iApiUrl["PageSize"]);
+ }
+ return aApiUrl;
+}
+
int main(int argc, char *argv[]) {
if (argc != 3) {
- throw std::invalid_argument(
- "/path/to/executable /path/to/credentials.json /path/to/output_file.json");
+ throw std::invalid_argument("Provide following arguments: /path/to/executable "
+ "/path/to/configuration.json /path/to/output_file.json");
}
DownloaderContext aDownloaderContext{std::make_unique<DefaultDownloadMethod>()};
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder(argv[1]);
std::ofstream aOutputJsonFile(argv[2]);
- aOutputJsonFile
- << aDownloaderContext.download(aHearthstoneTokenBuilder.buildToken().getToken()).dump(4)
- << '\n';
+ aOutputJsonFile << aDownloaderContext
+ .download(computeApiUrl(
+ nlohmann::json::parse(std::ifstream(argv[1]))["ApiUrl"]),
+ aHearthstoneTokenBuilder.buildToken().getToken())
+ .dump(4)
+ << '\n';
return 0;
}
diff --git a/battleNetApi/tests/src/hearthstoneApiTests.cpp b/battleNetApi/tests/src/hearthstoneApiTests.cpp
index 2c39d0c..192f38c 100644
--- a/battleNetApi/tests/src/hearthstoneApiTests.cpp
+++ b/battleNetApi/tests/src/hearthstoneApiTests.cpp
@@ -20,7 +20,7 @@ TEST_CASE("Retrieve number of cards", "[countCards]") {
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
- aApiUrl.setGameMode(bnet::GameMode::constructed);
+ aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(1);
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json");
const nlohmann::json aResponse = bnet::HearthstoneApi::makeApiRequest(
@@ -42,7 +42,7 @@ TEST_CASE("Retrieve page once", "[retrievePageOnce]") {
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
- aApiUrl.setGameMode(bnet::GameMode::constructed);
+ aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(16);
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json");
const std::string aTokenText{aHearthstoneTokenBuilder.allowBattleNetApiCall()
@@ -60,7 +60,7 @@ TEST_CASE("Retrieve pages 10 times without parallelization", "[retrievePagesNoPa
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
- aApiUrl.setGameMode(bnet::GameMode::constructed);
+ aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(16);
aApiUrl.setPage("1");
bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder("resources/credentials.json");
@@ -90,7 +90,7 @@ TEST_CASE("Retrieve pages 10 times with parallelization", "[retrievePagesParalle
const std::string aRegion{"eu"};
bnet::ApiUrl aApiUrl(aContentType, aRegion);
aApiUrl.setLocale("fr_FR");
- aApiUrl.setGameMode(bnet::GameMode::constructed);
+ aApiUrl.setGameMode("constructed");
aApiUrl.setPageSize(16);
aApiUrl.setPage("1");
std::vector<std::string> aRequests;