aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/PokemonTCGAPIClient.cpp
blob: 3285f6a2a1fd83d13b133dd3f9ae4b69ef21dd9f (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
#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_cards_by_query(const char* query) {
	nlohmann::json raw_data = raw_get_cards(query);
	std::vector<PokemonCard*> out;
	for (nlohmann::json card_json : raw_data["data"]) {
		out.push_back(new PokemonCard::from_json(card_json));
	}
	return out;
}

std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) {
	return get_cards_by_query((std::string("set.id:") + std::string(set_name)).c_str());
}

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;
}