This commit is contained in:
parent
3a78917e10
commit
d1e34a5c10
75
src/net.cpp
75
src/net.cpp
@ -1,5 +1,6 @@
|
|||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
|
#include "const.h"
|
||||||
#include "helper_funcs.h"
|
#include "helper_funcs.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
@ -16,7 +17,6 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <nlohmann/json_fwd.hpp>
|
#include <nlohmann/json_fwd.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "const.h"
|
|
||||||
|
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
@ -38,9 +38,8 @@ size_t WriteCallback(void *contents, size_t size, size_t nmemb,
|
|||||||
return totalSize;
|
return totalSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<std::string, int> send_curl_request(std::string endpoint,
|
std::tuple<std::string, int>
|
||||||
uint8_t type,
|
send_curl_request(std::string endpoint, uint8_t type, std::string req_data) {
|
||||||
std::string req_data) {
|
|
||||||
std::string response;
|
std::string response;
|
||||||
std::string url = baka_api_url + endpoint;
|
std::string url = baka_api_url + endpoint;
|
||||||
if (type == GET) {
|
if (type == GET) {
|
||||||
@ -54,28 +53,32 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint,
|
|||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
if (config.ignoressl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
|
|
||||||
|
|
||||||
struct curl_slist *headers = NULL;
|
struct curl_slist *headers = NULL;
|
||||||
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
|
headers = curl_slist_append(
|
||||||
headers = curl_slist_append(headers, std::format("User-Agent: bakatui/{}", VERSION).c_str());
|
headers, "Content-Type: application/x-www-form-urlencoded");
|
||||||
|
headers = curl_slist_append(
|
||||||
|
headers, std::format("User-Agent: bakatui/{}", VERSION).c_str());
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case GET:
|
case GET:
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
||||||
break;
|
break;
|
||||||
case POST:
|
case POST:
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req_data.c_str());
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req_data.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cerr << RED "[ERROR] " << RESET "invalid metod\n";
|
std::cerr << RED "[ERROR] " << RESET "invalid metod\n";
|
||||||
safe_exit(EINVAL);
|
safe_exit(EINVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -87,8 +90,6 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint,
|
|||||||
int http_code = 0;
|
int http_code = 0;
|
||||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return {response, http_code};
|
return {response, http_code};
|
||||||
}
|
}
|
||||||
namespace bakaapi {
|
namespace bakaapi {
|
||||||
@ -111,7 +112,7 @@ void login(std::string username, std::string password) {
|
|||||||
{
|
{
|
||||||
std::string savedir_path = std::getenv("HOME");
|
std::string savedir_path = std::getenv("HOME");
|
||||||
savedir_path.append("/.local/share/bakatui");
|
savedir_path.append("/.local/share/bakatui");
|
||||||
|
|
||||||
std::string urlfile_path = std::string(savedir_path) + "/url";
|
std::string urlfile_path = std::string(savedir_path) + "/url";
|
||||||
std::ofstream urlfile;
|
std::ofstream urlfile;
|
||||||
urlfile.open(urlfile_path);
|
urlfile.open(urlfile_path);
|
||||||
@ -141,7 +142,8 @@ void refresh_access_token() {
|
|||||||
refresh_token);
|
refresh_token);
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
std::clog << "calling send_curl_request() with folowing req_data\n" << req_data << std::endl;
|
std::clog << "calling send_curl_request() with folowing req_data\n"
|
||||||
|
<< req_data << std::endl;
|
||||||
auto [response, http_code] = send_curl_request("api/login", POST, req_data);
|
auto [response, http_code] = send_curl_request("api/login", POST, req_data);
|
||||||
if (http_code != 200) {
|
if (http_code != 200) {
|
||||||
std::cerr << RED "[ERROR] " << RESET << http_code
|
std::cerr << RED "[ERROR] " << RESET << http_code
|
||||||
@ -156,27 +158,28 @@ void refresh_access_token() {
|
|||||||
access_token = resp_parsed["access_token"];
|
access_token = resp_parsed["access_token"];
|
||||||
}
|
}
|
||||||
void is_access_token_empty() {
|
void is_access_token_empty() {
|
||||||
if(access_token.empty()) {
|
if (access_token.empty()) {
|
||||||
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
||||||
access_token = authfile_parsed["access_token"];
|
access_token = authfile_parsed["access_token"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// supports all endpoints that only require access_token
|
// supports all endpoints that only require access_token
|
||||||
json get_data_from_endpoint(std::string endpoint) {
|
json get_data_from_endpoint(std::string endpoint) {
|
||||||
is_access_token_empty();
|
is_access_token_empty();
|
||||||
std::string req_data =
|
std::string req_data =
|
||||||
std::format("Authorization=Bearer&access_token={}",
|
std::format("Authorization=Bearer&access_token={}", access_token);
|
||||||
access_token);
|
|
||||||
|
|
||||||
auto [response, http_code] = send_curl_request(endpoint, GET, req_data);
|
auto [response, http_code] = send_curl_request(endpoint, GET, req_data);
|
||||||
|
|
||||||
if(http_code != 200) {
|
if (http_code != 200) {
|
||||||
// DEBUG
|
// DEBUG
|
||||||
std::clog << "Failed geting data from endpoint: " << endpoint << " code: " << http_code << "\nrequest: " << req_data <<"\nresponse: " << response << std::endl;
|
std::clog << "Failed geting data from endpoint: " << endpoint
|
||||||
refresh_access_token();
|
<< " code: " << http_code << "\nrequest: " << req_data
|
||||||
}
|
<< "\nresponse: " << response << std::endl;
|
||||||
|
refresh_access_token();
|
||||||
|
}
|
||||||
|
|
||||||
return json::parse(response);
|
return json::parse(response);
|
||||||
}
|
}
|
||||||
} // namespace bakaapi
|
} // namespace bakaapi
|
Loading…
x
Reference in New Issue
Block a user