blob: 64fa93589798ae00c689d805b8bfbe37baac5cbd (
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
|
#include "apiUrl.hpp"
#include <fstream>
#include <hearthstoneApi.hpp>
#include <hearthstoneTokenBuilder.hpp>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <tokenBuilder.hpp>
class DownloadMethod {
public:
virtual nlohmann::json download(const std::shared_ptr<bnet::Token> &iToken) = 0;
virtual ~DownloadMethod() = default;
};
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);
if (iToken) {
return bnet::HearthstoneApi::downloadCards(aApiUrl, *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 std::shared_ptr<bnet::Token> &iToken) {
if (mDownloader) {
return mDownloader->download(iToken);
} else {
std::cerr << "No download method available in DownloaderContext\n";
return nlohmann::json({});
}
}
private:
std::unique_ptr<DownloadMethod> mDownloader;
};
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");
}
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';
return 0;
}
|