#include "apiUrl.hpp" #include #include #include #include #include #include #include #include class DownloadMethod { public: virtual nlohmann::json download(const bnet::ApiUrl &iApiUrl, const std::shared_ptr &iToken) = 0; virtual ~DownloadMethod() = default; }; class DefaultDownloadMethod : public DownloadMethod { public: DefaultDownloadMethod() = default; ~DefaultDownloadMethod() = default; nlohmann::json download(const bnet::ApiUrl &iApiUrl, const std::shared_ptr &iToken) override { if (iToken) { return bnet::HearthstoneApi::downloadCards(iApiUrl, *iToken); } return nlohmann::json({}); }; }; class DownloaderContext { public: DownloaderContext() = default; DownloaderContext(std::unique_ptr iDownloader) : mDownloader(std::move(iDownloader)) {}; void setDownloadMethod(std::unique_ptr iDownloader) { mDownloader = std::move(iDownloader); } nlohmann::json download(const bnet::ApiUrl &iApiUrl, const std::shared_ptr &iToken) { if (mDownloader) { return mDownloader->download(iApiUrl, iToken); } else { std::cerr << "No download method available in DownloaderContext\n"; return nlohmann::json({}); } } private: std::unique_ptr 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("Provide following arguments: /path/to/executable " "/path/to/configuration.json /path/to/output_file.json"); } DownloaderContext aDownloaderContext{std::make_unique()}; bnet::HearthstoneTokenBuilder aHearthstoneTokenBuilder(argv[1]); std::ofstream aOutputJsonFile(argv[2]); aOutputJsonFile << aDownloaderContext .download(computeApiUrl( nlohmann::json::parse(std::ifstream(argv[1]))["ApiUrl"]), aHearthstoneTokenBuilder.buildToken().getToken()) .dump(4) << '\n'; return 0; }