#include "PokemonTCGAPIClient.h" #include #include 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 PokemonTCGAPIClient::get_cards_by_query(const char* query) { nlohmann::json raw_data = raw_get_cards(query); std::vector out; for (nlohmann::json card_json : raw_data["data"]) { out.push_back(new PokemonCard::from_json(card_json)); } return out; } std::vector 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 PokemonTCGAPIClient::get_sets() { nlohmann::json raw_data = raw_get_sets("legalities.standard:legal"); std::vector sets; for (unsigned s = 0; s < raw_data["count"]; s++) sets.push_back(raw_data["data"][s]["id"]); return sets; }