summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 9ff2856ec615e0a181c549706afadefae85e8da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <curl/curl.h>
#include <format>
#include <iostream>
#include <pqxx/pqxx>
#include <string_view>

const pqxx::result execute_query(pqxx::work &transaction,
                                 std::string_view query) {
    return transaction.exec(query);
}

void do_curl_request(const char *url, const char *header) {
    CURL *curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        struct curl_slist *chunk = nullptr;
        chunk = curl_slist_append(chunk, header);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
}

void do_curl_request(const std::string &url, const std::string &header) {
    do_curl_request(url.c_str(), header.c_str());
}

std::string generate_request_header(const std::string &t_access_token) {
    return std::format("Authorization: Bearer {}", t_access_token);
}

const std::string get_url() {
    // FIXME: not flexible
    return "https://eu.api.blizzard.com/hearthstone/cards/678?locale=fr_FR";
}

bool is_last_generated_token_still_valid() {
    // TODO:
    return false;
}

std::string get_new_token() {
    // TODO:
    // curl -u 2b50f1123b424d5e9c21971a110fdd04:MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6
    // -d grant_type=client_credentials https://oauth.battle.net/token
    return "";
}

void display_result(pqxx::result r) {
    // FIXME: not flexible
    for (auto row : r) {
        std::cout << std::format(
            "{}; {}; {}; {}; {}", row[0].as<std::string>(),
            row[1].as<std::string>(), row[2].as<std::string>(),
            row[3].as<std::string>(), row[4].as<std::string>());
    }
}

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"));
    return 0;
}