summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp53
1 files changed, 42 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 9ff2856..dbc9f8e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,7 @@
+#include <algorithm>
+#include <cctype>
#include <curl/curl.h>
+#include <curl/easy.h>
#include <format>
#include <iostream>
#include <pqxx/pqxx>
@@ -13,6 +16,8 @@ void do_curl_request(const char *url, const char *header) {
CURL *curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, 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, header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
@@ -39,10 +44,38 @@ bool is_last_generated_token_still_valid() {
return false;
}
-std::string get_new_token() {
- // TODO:
- // curl -u 2b50f1123b424d5e9c21971a110fdd04:MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6
- // -d grant_type=client_credentials https://oauth.battle.net/token
+size_t curl_callback_token_request(void *contents, size_t size, size_t nmemb,
+ std::string *s) {
+ size_t newLength = size * nmemb;
+ try {
+ s->append((char *)contents, newLength);
+ } catch (std::bad_alloc &e) {
+ // handle memory problem
+ return 0;
+ }
+ return newLength;
+}
+
+std::string get_valid_token() {
+ // TODO: retrieve token from output
+ CURL *curl = curl_easy_init();
+ std::string output;
+ 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, &output);
+ curl_easy_setopt(curl, CURLOPT_USERPWD,
+ "2b50f1123b424d5e9c21971a110fdd04:"
+ "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6");
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
+ "grant_type=client_credentials");
+ curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ }
+ std::cout << output << std::endl;
return "";
}
@@ -60,12 +93,10 @@ int main() {
pqxx::connection cx{
"postgresql://aotrix@localhost:5432/hearthstonegameinfo"};
pqxx::work tx{cx};
- std::string access_token{"EU5xHPtrrZdPWXDBDe26WJuNnG0wyXk1rl"};
- // don't need to call the function every time
- std::string header = generate_request_header(access_token);
- // get_url(), otherwise...
- do_curl_request(get_url(), header);
- std::cout << std::endl << std::endl;
- display_result(execute_query(tx, "SELECT * FROM cards"));
+ std::string access_token = get_valid_token();
+ // std::string header = generate_request_header(access_token);
+ // do_curl_request(get_url(), header);
+ // std::cout << std::endl << std::endl;
+ // display_result(execute_query(tx, "SELECT * FROM cards"));
return 0;
}