blob: 2b6596a97e7812589186a42b6486c38b29f7ef5d (
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
|
#include "apiUrl.hpp"
#include <fstream>
#include <hearthstoneApi.hpp>
#include <iostream>
#include <tokenPool.hpp>
class DownloadMethod {
public:
virtual json download() = 0;
virtual ~DownloadMethod() = default;
};
class DefaultDownloadMethod : public DownloadMethod {
public:
DefaultDownloadMethod() = default;
~DefaultDownloadMethod() = default;
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::optional<Token> aToken{TokenPool::availableToken()};
if (aToken.has_value()) {
HearthstoneApi::downloadCards(aApiUrl, aToken.value());
}
return 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); }
json download() {
if (mDownloader) {
return mDownloader->download();
} else {
std::cerr << "No download method available in DownloaderContext\n";
return json({});
}
}
private:
std::unique_ptr<DownloadMethod> mDownloader;
};
int main(int argc, char *argv[]) {
DownloaderContext aDownloaderContext;
std::string aOutputJsonPath;
if (argc < 4) {
TokenPool::feedTokenSource("tests/resources/credentials.json");
aDownloaderContext.setDownloadMethod(std::make_unique<DefaultDownloadMethod>());
aOutputJsonPath = "tests/output/list_of_cards.json";
} else {
std::string aCredentialsFilePath{argv[1]};
TokenPool::feedTokenSource(aCredentialsFilePath);
aOutputJsonPath = argv[2];
}
std::ofstream aOutputJsonFile(aOutputJsonPath);
aOutputJsonFile << aDownloaderContext.download().dump(4) << '\n';
return 0;
}
|