blob: 9d2ed868ac9d29259e719da05e45b8aae8017bf9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#include "apiUrl.hpp"
#include <fstream>
#include <hearthstoneApi.hpp>
#include <hearthstoneTokenBuilder.hpp>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <tokenBuilder.hpp>
class DownloadMethod {
public:
virtual nlohmann::json download(const bnet::ApiUrl &iApiUrl,
const std::shared_ptr<bnet::Token> &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<bnet::Token> &iToken) override {
if (iToken) {
return bnet::HearthstoneApi::downloadCards(iApiUrl, *iToken);
}
return nlohmann::json({});
};
};
class DownloaderContext {
public:
DownloaderContext() = default;
DownloaderContext(std::unique_ptr<DownloadMethod> iDownloader)
: mDownloader(std::move(iDownloader)) {};
void setDownloadMethod(std::unique_ptr<DownloadMethod> iDownloader) {
mDownloader = std::move(iDownloader);
}
nlohmann::json download(const bnet::ApiUrl &iApiUrl,
const std::shared_ptr<bnet::Token> &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<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("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(computeApiUrl(
nlohmann::json::parse(std::ifstream(argv[1]))["ApiUrl"]),
aHearthstoneTokenBuilder.buildToken().getToken())
.dump(4)
<< '\n';
return 0;
}
|