summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 1fcbbe4..5b67817 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,27 +6,27 @@
#include <pqxx/pqxx>
#include <regex>
-const pqxx::result execute_query(pqxx::work &transaction,
- std::string_view query) {
- return transaction.exec(query);
+const pqxx::result execute_query(pqxx::work &t_transaction,
+ std::string_view t_query) {
+ return t_transaction.exec(t_query);
}
-void do_curl_request(const char *url, const char *header) {
+void do_curl_request(const char *t_url, const char *t_header) {
CURL *curl = curl_easy_init();
if (curl) {
- curl_easy_setopt(curl, CURLOPT_URL, url);
+ 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, header);
+ chunk = curl_slist_append(chunk, t_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());
+void do_curl_request(const std::string &t_url, const std::string &t_header) {
+ do_curl_request(t_url.c_str(), t_header.c_str());
}
std::string generate_request_header(const std::string &t_access_token) {
@@ -43,11 +43,11 @@ bool is_last_generated_token_still_valid() {
return false;
}
-size_t curl_callback_token_request(void *contents, size_t size, size_t nmemb,
- std::string *s) {
- size_t newLength = size * nmemb;
+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 {
- s->append((char *)contents, newLength);
+ t_s->append((char *)t_contents, newLength);
} catch (std::bad_alloc &e) {
// handle memory problem
return 0;
@@ -55,8 +55,15 @@ size_t curl_callback_token_request(void *contents, size_t size, size_t nmemb,
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();
+}
+
std::string get_valid_token() {
- // TODO: retrieve token from output
CURL *curl = curl_easy_init();
std::string output;
if (curl) {
@@ -79,20 +86,18 @@ std::string get_valid_token() {
std::exit(EXIT_FAILURE);
}
}
- // FIXME: token_regex is not valid
- std::regex token_regex("access_token:\"(\\w+)\"");
- std::cout << output << std::endl;
- auto token_it =
- std::sregex_iterator(output.begin(), output.end(), token_regex);
- std::smatch token = *token_it;
- std::string token_str = token.str();
- std::cout << std::format("Token trouvé: {}", token_str) << std::endl;
- return "";
+ std::string token =
+ extract_string_with_regex(output, "\"access_token\":\"(\\w+)\"");
+ std::string expires_in =
+ extract_string_with_regex(output, "\"expires_in\":(\\w+)");
+ std::cout << std::format("Token trouvé: {}, {}", token, expires_in)
+ << std::endl;
+ return token;
}
-void display_result(pqxx::result r) {
+void display_result(pqxx::result t_r) {
// FIXME: not flexible
- for (auto row : r) {
+ for (auto row : t_r) {
std::cout << std::format(
"{}; {}; {}; {}; {}", row[0].as<std::string>(),
row[1].as<std::string>(), row[2].as<std::string>(),