summaryrefslogtreecommitdiff
path: root/battleNetApi/src
diff options
context:
space:
mode:
authorAotrix <[email protected]>2026-07-31 02:03:10 +0200
committerAotrix <[email protected]>2026-07-31 02:03:10 +0200
commitd27b6aeb73679737ca921b5fc25a7b9c1f3c832c (patch)
tree6741a355c79302c29f839d546b1b6c6ad96b1566 /battleNetApi/src
parent71b50eaf1e10028b2a0119bc935671be2f7b405e (diff)
Remove GameMode enum class
Diffstat (limited to 'battleNetApi/src')
-rw-r--r--battleNetApi/src/apiUrl.cpp34
-rw-r--r--battleNetApi/src/main.cpp50
2 files changed, 46 insertions, 38 deletions
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;
}