From 27459497a5ba0eac18a57e0c15b21972ab7e5284 Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Mon, 6 Apr 2026 00:59:04 +0200 Subject: Update main.cpp --- battleNetApi/src/main.cpp | 39 +++++++++++++++------------------------ battleNetApi/src/tokenPool.cpp | 3 ++- 2 files changed, 17 insertions(+), 25 deletions(-) (limited to 'battleNetApi/src') diff --git a/battleNetApi/src/main.cpp b/battleNetApi/src/main.cpp index 7f2450e..4a5594c 100644 --- a/battleNetApi/src/main.cpp +++ b/battleNetApi/src/main.cpp @@ -1,12 +1,12 @@ #include "apiUrl.hpp" +#include #include #include -#include #include class DownloadMethod { public: - virtual void download() = 0; + virtual json download() = 0; virtual ~DownloadMethod() = default; }; @@ -14,20 +14,18 @@ class DefaultDownloadMethod : public DownloadMethod { public: DefaultDownloadMethod() = default; ~DefaultDownloadMethod() = default; - void download() override { + json download() 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::println("Downloading cards..."); - TokenPool::fillTokenSource("tests/resources/credentials.json"); std::optional aToken{TokenPool::unusedToken()}; if (aToken.has_value()) { HearthstoneApi::downloadCards(aApiUrl, aToken.value()); - std::println("Download done."); } + return json({}); }; }; @@ -36,11 +34,12 @@ class DownloaderContext { DownloaderContext() = default; DownloaderContext(std::unique_ptr iDownloader) : mDownloader(std::move(iDownloader)) {}; void setDownloadMethod(std::unique_ptr iDownloader) { mDownloader = std::move(iDownloader); } - void download() { + json download() { if (mDownloader) { - mDownloader->download(); + return mDownloader->download(); } else { std::cerr << "No download method available in DownloaderContext\n"; + return json({}); } } @@ -49,26 +48,18 @@ class DownloaderContext { }; int main(int argc, char *argv[]) { - // if (argc < 3) { - // // Example of parameters: - // // ./build/downloadCards tests/resources/credentials.json build/output.json - // throw std::invalid_argument("/ - // "); - // } - // std::string aCredentialsFilePath{argv[1]}; - // Token aToken(aCredentialsFilePath); - // HearthstoneApi aHearthstoneApi(aToken); - // std::string aOutputPath{argv[2]}; - DownloaderContext aDownloaderContext; + std::string aOutputJsonPath; if (argc < 4) { + TokenPool::fillTokenSource("tests/resources/credentials.json"); aDownloaderContext.setDownloadMethod(std::make_unique()); + aOutputJsonPath = "tests/output/list_of_cards.json"; } else { - // TODO: retrieve config from JSON file + std::string aCredentialsFilePath{argv[1]}; + TokenPool::fillTokenSource(aCredentialsFilePath); + aOutputJsonPath = argv[2]; } - aDownloaderContext.download(); - - // std::ofstream aOutputJson(iOutputJsonPath); - // aOutputJson << aMergedCards.dump(4); + std::ofstream aOutputJsonFile(aOutputJsonPath); + aOutputJsonFile << aDownloaderContext.download().dump(4) << '\n'; return 0; } diff --git a/battleNetApi/src/tokenPool.cpp b/battleNetApi/src/tokenPool.cpp index 20ea6cd..f8a6256 100644 --- a/battleNetApi/src/tokenPool.cpp +++ b/battleNetApi/src/tokenPool.cpp @@ -9,4 +9,5 @@ std::optional TokenPool::unusedToken() { return aOutputToken.text(); } -void TokenPool::fillTokenSource(const std::string &iJsonPath) { mTokenContainer.emplace(Token(iJsonPath)); } +void TokenPool::fillTokenSource(const std::string &iJsonPath) { // mTokenContainer.emplace(Token(iJsonPath)); +} -- cgit v1.2.3