diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-23 15:33:44 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-23 15:33:44 +0100 |
commit | c98f9337940128178401bb6b2d242ce577080ca4 (patch) | |
tree | 6dcf8da983709cf3c4d33e793eed3f8bb68501d7 /oop2eindopdr | |
parent | 295ee857b9b46c27e80051700278e3075590d823 (diff) |
half-implemented TCG API client class
Diffstat (limited to 'oop2eindopdr')
-rw-r--r-- | oop2eindopdr/CacheManager.cpp | 8 | ||||
-rw-r--r-- | oop2eindopdr/Pokedex.h | 7 | ||||
-rw-r--r-- | oop2eindopdr/PokemonCard.h | 5 | ||||
-rw-r--r-- | oop2eindopdr/PokemonTCGAPIClient.cpp | 39 | ||||
-rw-r--r-- | oop2eindopdr/PokemonTCGAPIClient.h | 21 | ||||
-rw-r--r-- | oop2eindopdr/main.cpp | 39 | ||||
-rw-r--r-- | oop2eindopdr/makefile | 2 | ||||
-rw-r--r-- | oop2eindopdr/readme.md | 6 |
8 files changed, 105 insertions, 22 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp index 7de435e..5cc1121 100644 --- a/oop2eindopdr/CacheManager.cpp +++ b/oop2eindopdr/CacheManager.cpp @@ -7,8 +7,8 @@ CacheManager::CacheManager(const char* cache_path) : CacheManager(std::string(cache_path)) { } CacheManager::CacheManager(std::string cache_path) { - this->cache_path = cache_path; - this->verify_cache(); + this->cache_path = cache_path; + this->verify_cache(); } CacheManager::~CacheManager() { @@ -22,12 +22,12 @@ CacheManager::~CacheManager() { } void CacheManager::verify_cache() { - if (!std::filesystem::is_directory(this->cache_path)) this->init_cache(); + if (!std::filesystem::is_directory(this->cache_path)) this->init_cache(); this->update_cache(); } void CacheManager::init_cache() { - std::filesystem::create_directory(this->cache_path); + std::filesystem::create_directory(this->cache_path); this->update_cache(); } diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h index abe8bef..a91b560 100644 --- a/oop2eindopdr/Pokedex.h +++ b/oop2eindopdr/Pokedex.h @@ -5,6 +5,7 @@ #include "PokemonCard.h" +/** @brief user pokedex class, handles caching and api access silently */ class Pokedex { private: std::vector<PokemonCard*> cards; @@ -13,7 +14,11 @@ private: public: Pokedex(); virtual ~Pokedex(); - + + virtual void download_collection(); + virtual void load_collection(); + virtual void verify_collection(); + /** @brief search cards that contain `query` in id field */ virtual PokemonCard* search_card_by_id(std::string query); }; diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h index 1037ffd..c89df9c 100644 --- a/oop2eindopdr/PokemonCard.h +++ b/oop2eindopdr/PokemonCard.h @@ -1,9 +1,14 @@ #pragma once +#include <nlohmann/json.hpp> #include <string> #include <vector> +/** @brief single pokemon card */ class PokemonCard { +private: + nlohmann::json raw_data; + public: PokemonCard(); virtual ~PokemonCard(); diff --git a/oop2eindopdr/PokemonTCGAPIClient.cpp b/oop2eindopdr/PokemonTCGAPIClient.cpp index e69de29..0c950fd 100644 --- a/oop2eindopdr/PokemonTCGAPIClient.cpp +++ b/oop2eindopdr/PokemonTCGAPIClient.cpp @@ -0,0 +1,39 @@ +#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}}); +} + +nlohmann::json PokemonTCGAPIClient::raw_get_sets(const char* query) { + return raw_request("/sets", cpr::Parameters{{"q", query}}); +} + +std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) { + return {}; +} + +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; +} diff --git a/oop2eindopdr/PokemonTCGAPIClient.h b/oop2eindopdr/PokemonTCGAPIClient.h index 45dcbb0..241586b 100644 --- a/oop2eindopdr/PokemonTCGAPIClient.h +++ b/oop2eindopdr/PokemonTCGAPIClient.h @@ -1,3 +1,24 @@ #pragma once +#include <nlohmann/json.hpp> +#include <cpr/cpr.h> + +#include "PokemonCard.h" + +/** @brief direct api access class */ +class PokemonTCGAPIClient { +public: + PokemonTCGAPIClient(); + virtual ~PokemonTCGAPIClient(); + +private: + const char* API_URL = "https://api.pokemontcg.io/v2"; + virtual nlohmann::json raw_request(const char* endpoint, cpr::Parameters params); + virtual nlohmann::json raw_get_cards(const char* query); + virtual nlohmann::json raw_get_sets(const char* query); + +public: + virtual std::vector<PokemonCard*> get_set_cards(const char* set_name); + virtual std::vector<std::string> get_sets(); +}; diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp index c042ffe..62afcac 100644 --- a/oop2eindopdr/main.cpp +++ b/oop2eindopdr/main.cpp @@ -3,7 +3,7 @@ #include "main.h" #include "Pokedex.h" -#include "PokemonCard.h" +#include "PokemonTCGAPIClient.h" using std::endl; using std::cout; @@ -11,30 +11,37 @@ using std::cout; Pokedex* g_pokedex = nullptr; int interactive_mode() { - std::string gert; - bool user_exit = false; - while (!user_exit) { - cout << "interactive mode" << endl; - std::cin >> gert; - } + std::string gert; + bool user_exit = false; + while (!user_exit) { + cout << "interactive mode" << endl; + std::cin >> gert; + } return EXIT_SUCCESS; } int export_mode(int argc, char** argv) { - cout << "export mode! let's convert " << std::string(argv[1]) << " to " << std::string(argv[2]) << endl; + cout << "export mode! let's convert " << std::string(argv[1]) << " to " << std::string(argv[2]) << endl; return EXIT_SUCCESS; } int main(int argc, char** argv) { - if (argc == 1) { // no arguments specified - return interactive_mode(); - } else if (argc == 2 || argc == 3) { - return export_mode(argc, argv); - } else { // three or more arguments - cout << "too many arguments specified!" << endl; - return EXIT_FAILURE; - } + PokemonTCGAPIClient api_client_test; + std::vector<std::string> sets = api_client_test.get_sets(); + for (auto s : sets) + cout << s << endl; + + return EXIT_SUCCESS; + + if (argc == 1) { // no arguments specified + return interactive_mode(); + } else if (argc == 2 || argc == 3) { + return export_mode(argc, argv); + } else { // three or more arguments + cout << "too many arguments specified!" << endl; + return EXIT_FAILURE; + } return EXIT_SUCCESS; } diff --git a/oop2eindopdr/makefile b/oop2eindopdr/makefile index e0992ef..533121d 100644 --- a/oop2eindopdr/makefile +++ b/oop2eindopdr/makefile @@ -5,7 +5,7 @@ TARGET = main OUTPUT_ZIP = Eindopdracht_2180996.zip CFLAGS += -std=c++17 -LFLAGS += -lstdc++ +LFLAGS += -lstdc++ -lcpr -lzip SRCS := $(wildcard *.cpp) OBJS := $(patsubst %.cpp,%.o, $(SRCS)) diff --git a/oop2eindopdr/readme.md b/oop2eindopdr/readme.md index d423e78..e3c75d9 100644 --- a/oop2eindopdr/readme.md +++ b/oop2eindopdr/readme.md @@ -1,5 +1,8 @@ # eindopdracht +in dit document staat overal pokémon verkeerd geschreven als pokemon om alle +broncode ascii-vriendelijk te houden. + combinatie readme en kladblok tijdens ontwikkeling ## gebruikte libraries @@ -48,6 +51,7 @@ case cli_argument.count ``` cache/ date + index swshp-SWSH001/ info.json card.png @@ -61,6 +65,8 @@ cache/ - `date` bevat een unix timestamp wanneer de cache voor het laatst geupdate is (gebruikt om te checken of de cache stale is) +- de mappen direct onder de cache map zijn de volledige id's van de pokemon +- `index` bevat op losse regels de namen van de mappen met pokemon info - `info.json` bevat voor elke kaart de json api response voor die kaart ## zip formaat (voorbeeld) |