summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/hearthstoneAPIHandler.cpp136
-rw-r--r--src/main.cpp131
2 files changed, 140 insertions, 127 deletions
diff --git a/src/hearthstoneAPIHandler.cpp b/src/hearthstoneAPIHandler.cpp
new file mode 100644
index 0000000..78f084b
--- /dev/null
+++ b/src/hearthstoneAPIHandler.cpp
@@ -0,0 +1,136 @@
+#include "hearthstoneAPIHandler.h"
+#include <chrono>
+#include <curl/curl.h>
+#include <format>
+#include <fstream>
+#include <iostream>
+#include <regex>
+
+HearthstoneAPIHandler::HearthstoneAPIHandler()
+ : m_access_token(get_valid_token()), m_header(generate_request_header()) {}
+
+size_t HearthstoneAPIHandler::curl_callback_token_request(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);
+ } catch (std::bad_alloc &e) {
+ // handle memory problem
+ return 0;
+ }
+ return newLength;
+}
+
+std::chrono::hours HearthstoneAPIHandler::get_current_time() {
+ return std::chrono::duration_cast<std::chrono::hours>(
+ 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::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 &file_path) {
+ std::ofstream token_file(file_path);
+ CURL *curl = curl_easy_init();
+ std::string token_info;
+ if (curl) {
+ 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_token_request);
+ 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");
+ CURLcode result_code;
+ result_code = curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ if (result_code != CURLE_OK) {
+ std::cerr << "ERROR: token request failed" << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ }
+ std::string token =
+ extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\"");
+ std::chrono::hours expires_at = get_current_time() + std::chrono::hours(24);
+ token_file << token << " " << expires_at.count();
+}
+
+void HearthstoneAPIHandler::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::hours>(
+ 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() {
+ 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);
+ std::string token = line.substr(0, line.find(" "));
+ return token;
+}
+
+std::string HearthstoneAPIHandler::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 HearthstoneAPIHandler::get_set_of_cards_url(
+ const std::string &t_card_parameters) {
+ return get_url(std::format("cards/{}", t_card_parameters));
+}
+
+void HearthstoneAPIHandler::make_api_request(const char *t_url,
+ const char *t_header) {
+ CURL *curl = curl_easy_init();
+ 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);
+ struct curl_slist *chunk = nullptr;
+ chunk = curl_slist_append(chunk, t_header);
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
+ curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ }
+}
+
+void HearthstoneAPIHandler::make_api_request(const std::string &t_url,
+ const std::string &t_header) {
+ make_api_request(t_url.c_str(), t_header.c_str());
+}
+
+void HearthstoneAPIHandler::get_cards(const std::string &t_card_parameters) {
+ make_api_request(get_set_of_cards_url(t_card_parameters), m_header);
+}
diff --git a/src/main.cpp b/src/main.cpp
index 5db146d..60c921f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,133 +1,12 @@
-#include <cctype>
-#include <chrono>
-#include <cstdlib>
-#include <curl/curl.h>
-#include <curl/easy.h>
-#include <fstream>
+#include "hearthstoneAPIHandler.h"
#include <iostream>
#include <pqxx/pqxx>
-#include <regex>
const pqxx::result execute_query(pqxx::work &t_transaction,
std::string_view t_query) {
return t_transaction.exec(t_query);
}
-void make_api_request(const char *t_url, const char *t_header) {
- CURL *curl = curl_easy_init();
- 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);
- struct curl_slist *chunk = nullptr;
- chunk = curl_slist_append(chunk, t_header);
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
- curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- }
-}
-
-void make_api_request(const std::string &t_url, const std::string &t_header) {
- make_api_request(t_url.c_str(), t_header.c_str());
-}
-
-std::string generate_request_header(const std::string &t_access_token) {
- return std::format("Authorization: Bearer {}", t_access_token);
-}
-
-std::chrono::hours get_current_time() {
- return std::chrono::duration_cast<std::chrono::hours>(
- std::chrono::utc_clock::now().time_since_epoch());
-}
-
-size_t curl_callback_token_request(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);
- } catch (std::bad_alloc &e) {
- // handle memory problem
- return 0;
- }
- return newLength;
-}
-
-std::string 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 write_new_token_info_to_file(const std::string &file_path) {
- std::ofstream token_file(file_path);
- CURL *curl = curl_easy_init();
- std::string token_info;
- if (curl) {
- 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_token_request);
- 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");
- CURLcode result_code;
- result_code = curl_easy_perform(curl);
- curl_easy_cleanup(curl);
- if (result_code != CURLE_OK) {
- std::cerr << "ERROR: token request failed" << std::endl;
- std::exit(EXIT_FAILURE);
- }
- }
- std::string token =
- extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\"");
- std::chrono::hours expires_at = get_current_time() + std::chrono::hours(24);
- token_file << token << " " << expires_at.count();
-}
-
-void 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::hours>(
- std::chrono::utc_clock::now().time_since_epoch())
- .count() >= expire_at) {
- write_new_token_info_to_file(file_path);
- }
- }
-}
-
-std::string get_valid_token() {
- 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);
- std::string token = line.substr(0, line.find(" "));
- return token;
-}
-
-const std::string get_url(const std::string &t_parameters) {
- return std::format("https://eu.api.blizzard.com/hearthstone/{}",
- t_parameters);
-}
-
-const std::string get_set_of_cards_url(const std::string &t_card_parameters) {
- return get_url(std::format("cards/{}", t_card_parameters));
-}
-
void display_query_result(pqxx::result t_r) {
// FIXME: not flexible
for (auto row : t_r) {
@@ -139,14 +18,12 @@ void display_query_result(pqxx::result t_r) {
}
int main() {
- // TODO: Create a class to simplify the calls
+ HearthstoneAPIHandler hs_handler;
+ hs_handler.get_cards("678?locale=en_US");
+
pqxx::connection cx{
"postgresql://aotrix@localhost:5432/hearthstonegameinfo"};
pqxx::work tx{cx};
- std::string access_token = get_valid_token();
- std::string header = generate_request_header(access_token);
- make_api_request(get_set_of_cards_url("100?locale=fr_FR"), header);
- std::cout << std::endl << std::endl;
display_query_result(execute_query(tx, "SELECT * FROM cards"));
return 0;
}