summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/hearthstoneAPIHandler.h10
-rw-r--r--src/hearthstoneAPIHandler.cpp26
-rw-r--r--src/main.cpp2
3 files changed, 23 insertions, 15 deletions
diff --git a/include/hearthstoneAPIHandler.h b/include/hearthstoneAPIHandler.h
index 718b98c..c752f6e 100644
--- a/include/hearthstoneAPIHandler.h
+++ b/include/hearthstoneAPIHandler.h
@@ -5,10 +5,10 @@
class HearthstoneAPIHandler {
public:
HearthstoneAPIHandler();
- void get_cards(const std::string &t_card_parameters);
+ std::string get_cards(const std::string &t_card_parameters);
private:
- static size_t curl_callback_token_request(void *t_contents, size_t t_size,
+ static size_t curl_callback_string_output(void *t_contents, size_t t_size,
size_t t_nmemb, std::string *t_s);
std::chrono::hours get_current_time();
std::string extract_string_with_regex(const std::string &t_str,
@@ -20,9 +20,9 @@ class HearthstoneAPIHandler {
const std::string get_url(const std::string &t_parameters);
const std::string
get_set_of_cards_url(const std::string &t_card_parameters);
- void make_api_request(const char *t_url, const char *t_header);
- void make_api_request(const std::string &t_url,
- const std::string &t_header);
+ std::string make_api_request(const char *t_url, const char *t_header);
+ std::string make_api_request(const std::string &t_url,
+ const std::string &t_header);
std::string m_access_token;
std::string m_header;
diff --git a/src/hearthstoneAPIHandler.cpp b/src/hearthstoneAPIHandler.cpp
index 78f084b..6528ed0 100644
--- a/src/hearthstoneAPIHandler.cpp
+++ b/src/hearthstoneAPIHandler.cpp
@@ -9,7 +9,7 @@
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 HearthstoneAPIHandler::curl_callback_string_output(void *t_contents,
size_t t_size,
size_t t_nmemb,
std::string *t_s) {
@@ -47,7 +47,7 @@ void HearthstoneAPIHandler::write_new_token_info_to_file(
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_callback_string_output);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info);
curl_easy_setopt(curl, CURLOPT_USERPWD,
"2b50f1123b424d5e9c21971a110fdd04:"
@@ -111,26 +111,34 @@ const std::string HearthstoneAPIHandler::get_set_of_cards_url(
return get_url(std::format("cards/{}", t_card_parameters));
}
-void HearthstoneAPIHandler::make_api_request(const char *t_url,
- const char *t_header) {
+std::string HearthstoneAPIHandler::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_WRITEDATA, &request_output);
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);
}
+ return request_output;
}
-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());
+std::string
+HearthstoneAPIHandler::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());
}
-void HearthstoneAPIHandler::get_cards(const std::string &t_card_parameters) {
- make_api_request(get_set_of_cards_url(t_card_parameters), m_header);
+std::string
+HearthstoneAPIHandler::get_cards(const std::string &t_card_parameters) {
+ // TODO: Use a parser to generate a std::map or something like that
+ return make_api_request(get_set_of_cards_url(t_card_parameters), m_header);
}
diff --git a/src/main.cpp b/src/main.cpp
index 60c921f..90a4fe6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -19,7 +19,7 @@ void display_query_result(pqxx::result t_r) {
int main() {
HearthstoneAPIHandler hs_handler;
- hs_handler.get_cards("678?locale=en_US");
+ std::cout << hs_handler.get_cards("678?locale=en_US") << std::endl;
pqxx::connection cx{
"postgresql://aotrix@localhost:5432/hearthstonegameinfo"};