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
|
#include "PokemonTCGAPIClient.h"
#include <iostream>
#include <cpr/cpr.h>
PokemonTCGAPIClient::PokemonTCGAPIClient() {
}
PokemonTCGAPIClient::~PokemonTCGAPIClient() {
}
nlohmann::json PokemonTCGAPIClient::raw_request(const char* endpoint, cpr::Parameters params) {
std::string full_url = API_URL;
full_url.append(endpoint);
cpr::Response res = cpr::Get(cpr::Url{full_url}, params);
return nlohmann::json::parse(res.text);
}
nlohmann::json PokemonTCGAPIClient::raw_get_cards(const char* query) {
return raw_request("/cards", cpr::Parameters{{"q", query}, {"select", "id,name,hp,attacks,number,images"}});
}
nlohmann::json PokemonTCGAPIClient::raw_get_sets(const char* query) {
return raw_request("/sets", cpr::Parameters{{"q", query}});
}
nlohmann::json PokemonTCGAPIClient::raw_get_card(const char* id) {
std::string full_query = "id:";
full_query.append(id);
return raw_request("/cards", cpr::Parameters{{"q", full_query}});
}
std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) {
std::string query = "set.id:";
query.append(set_name);
nlohmann::json raw_data = raw_get_cards(query.c_str());
std::vector<PokemonCard*> out;
for (nlohmann::json card_json : raw_data["data"]) {
out.push_back(new PokemonCard::from_json(card_json));
}
return out;
}
PokemonCard* PokemonTCGAPIClient::get_full_card(const char* id) {
nlohmann::json raw_cards = raw_get_card(id);
if (raw_cards["data"].size() == 0) return nullptr;
return new PokemonCard::from_json(raw_cards["data"][0]);
}
std::vector<std::string> PokemonTCGAPIClient::get_sets() {
nlohmann::json raw_data = raw_get_sets("legalities.standard:legal");
std::vector<std::string> sets;
for (unsigned s = 0; s < raw_data["count"]; s++)
sets.push_back(raw_data["data"][s]["id"]);
return sets;
}
|