diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/hearthstoneAPI.cpp (renamed from src/hearthstoneAPIHandler.cpp) | 75 | ||||
| -rw-r--r-- | src/postgresHandler.cpp | 6 |
2 files changed, 29 insertions, 52 deletions
diff --git a/src/hearthstoneAPIHandler.cpp b/src/hearthstoneAPI.cpp index 8845b62..cb72b11 100644 --- a/src/hearthstoneAPIHandler.cpp +++ b/src/hearthstoneAPI.cpp @@ -1,4 +1,4 @@ -#include "hearthstoneAPIHandler.hpp" +#include "hearthstoneAPI.hpp" #include <chrono> #include <curl/curl.h> #include <format> @@ -6,13 +6,9 @@ #include <iostream> #include <regex> -HearthstoneAPIHandler::HearthstoneAPIHandler() - : m_access_token(get_valid_token()), m_header(generate_request_header()) {} +HearthstoneAPI::HearthstoneAPI() : m_access_token(get_valid_token()), m_header(generate_request_header()) {} -size_t HearthstoneAPIHandler::curl_callback_string_output(void *t_contents, - size_t t_size, - size_t t_nmemb, - std::string *t_s) { +size_t HearthstoneAPI::curl_callback_string_output(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) { size_t newLength = t_size * t_nmemb; try { t_s->append((char *)t_contents, newLength); @@ -23,22 +19,18 @@ size_t HearthstoneAPIHandler::curl_callback_string_output(void *t_contents, return newLength; } -std::chrono::seconds HearthstoneAPIHandler::get_current_time() { - return std::chrono::duration_cast<std::chrono::seconds>( - std::chrono::utc_clock::now().time_since_epoch()); +std::chrono::seconds HearthstoneAPI::get_current_time() { + return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()); } -std::string -HearthstoneAPIHandler::extract_string_with_regex(const std::string &t_str, - const std::string t_regex) { +std::string HearthstoneAPI::extract_string_with_regex(const std::string &t_str, const std::string t_regex) { std::regex regex(t_regex); auto it = std::sregex_iterator(t_str.begin(), t_str.end(), regex); std::smatch match = *it; return match[1].str(); } -void HearthstoneAPIHandler::write_new_token_info_to_file( - const std::string &t_file_path) { +void HearthstoneAPI::write_new_token_info_to_file(const std::string &t_file_path) { std::ofstream token_file(t_file_path); CURL *curl = curl_easy_init(); std::string token_info; @@ -46,14 +38,12 @@ void HearthstoneAPIHandler::write_new_token_info_to_file( curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, - curl_callback_string_output); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info); curl_easy_setopt(curl, CURLOPT_USERPWD, "2b50f1123b424d5e9c21971a110fdd04:" "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, - "grant_type=client_credentials"); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); CURLcode result_code; result_code = curl_easy_perform(curl); curl_easy_cleanup(curl); @@ -62,34 +52,29 @@ void HearthstoneAPIHandler::write_new_token_info_to_file( std::exit(EXIT_FAILURE); } } - std::string token = - extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\""); - std::chrono::seconds expires_at = - get_current_time() + std::chrono::hours(24); - token_file << token << " " << expires_at.count(); + std::string token = extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\""); + std::chrono::seconds expires_at = get_current_time() + std::chrono::hours(24); + token_file << token << " " << expires_at.count() << '\n'; } -void HearthstoneAPIHandler::refresh_token_if_needed() { - std::string file_path = - std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); +void HearthstoneAPI::refresh_token_if_needed() { + std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); std::ifstream token_file(file_path); std::string line; std::getline(token_file, line); if (line.empty()) { write_new_token_info_to_file(file_path); } else { - unsigned long expire_at = std::stoull(line.substr(line.find(" ") + 1)); - if (std::chrono::duration_cast<std::chrono::seconds>( - std::chrono::utc_clock::now().time_since_epoch()) + long expire_at = std::stoull(line.substr(line.find(" ") + 1)); + if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::utc_clock::now().time_since_epoch()) .count() >= expire_at) { write_new_token_info_to_file(file_path); } } } -std::string HearthstoneAPIHandler::get_valid_token() { +std::string HearthstoneAPI::get_valid_token() { refresh_token_if_needed(); - std::string file_path = - std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); + std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); std::ifstream token_file(file_path); std::string line; std::getline(token_file, line); @@ -97,31 +82,26 @@ std::string HearthstoneAPIHandler::get_valid_token() { return token; } -std::string HearthstoneAPIHandler::generate_request_header() { +std::string HearthstoneAPI::generate_request_header() { return std::format("Authorization: Bearer {}", m_access_token); } -const std::string -HearthstoneAPIHandler::get_url(const std::string &t_parameters) { - return std::format("https://eu.api.blizzard.com/hearthstone/{}", - t_parameters); +const std::string HearthstoneAPI::get_url(const std::string &t_parameters) { + return std::format("https://eu.api.blizzard.com/hearthstone/{}", t_parameters); } -const std::string HearthstoneAPIHandler::get_set_of_cards_url( - const std::string &t_card_parameters) { +const std::string HearthstoneAPI::get_set_of_cards_url(const std::string &t_card_parameters) { return get_url(std::format("cards{}", t_card_parameters)); } -std::string HearthstoneAPIHandler::make_api_request(const char *t_url, - const char *t_header) { +std::string HearthstoneAPI::make_api_request(const char *t_url, const char *t_header) { CURL *curl = curl_easy_init(); std::string request_output; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, t_url); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, - curl_callback_string_output); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request_output); struct curl_slist *chunk = nullptr; chunk = curl_slist_append(chunk, t_header); @@ -132,13 +112,10 @@ std::string HearthstoneAPIHandler::make_api_request(const char *t_url, return request_output; } -std::string -HearthstoneAPIHandler::make_api_request(const std::string &t_url, - const std::string &t_header) { +std::string HearthstoneAPI::make_api_request(const std::string &t_url, const std::string &t_header) { return make_api_request(t_url.c_str(), t_header.c_str()); } -std::string -HearthstoneAPIHandler::get_cards(const std::string &t_card_parameters) { +std::string HearthstoneAPI::get_cards(const std::string &t_card_parameters) { return make_api_request(get_set_of_cards_url(t_card_parameters), m_header); } diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp index c0276f1..8ca3c20 100644 --- a/src/postgresHandler.cpp +++ b/src/postgresHandler.cpp @@ -1,6 +1,6 @@ #include "postgresHandler.hpp" #include "cardTypeHandlerInfo.hpp" -#include "hearthstoneAPIHandler.hpp" +#include "hearthstoneAPI.hpp" #include <cctype> #include <cstddef> #include <cstdint> @@ -125,7 +125,7 @@ const std::string::size_type endPosOfCardJSON(const std::string &kCardsPageJSON, return kCardsPageJSON.find("}", kSearchStartPos); } -void addCardsToDatabase(HearthstoneAPIHandler &aAPIIntermediate, pqxx::work &aTransaction, +void addCardsToDatabase(HearthstoneAPI &aAPIIntermediate, pqxx::work &aTransaction, const std::string &kURLOfCardsPage) { const std::regex aRegexKeyValueInJSON = regexKeyValueInJSON(); const std::string &kCardsPageJSON = aAPIIntermediate.get_cards(kURLOfCardsPage); @@ -155,7 +155,7 @@ void refresh_cards_table() { pqxx::connection aConnection{ "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; // FIXME: load a .xml file instead pqxx::work aTransaction{aConnection}; - HearthstoneAPIHandler aAPIConnector; + HearthstoneAPI aAPIConnector; std::string aCards = aAPIConnector.get_cards("/?locale=fr_FR&gameMode=constructed"); // FIXME: use a struct as a parameter clearCardDatabase(aTransaction); |
