This commit is contained in:
parent
4ba8710a71
commit
6985b21d37
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@
|
|||||||
compile_commands.json
|
compile_commands.json
|
||||||
core*
|
core*
|
||||||
build
|
build
|
||||||
test-data
|
test-data
|
||||||
|
log
|
@ -44,41 +44,45 @@ std::string bool_to_string(bool bool_in) { return bool_in ? "true" : "false"; }
|
|||||||
|
|
||||||
std::string SoRAuthFile(bool save, std::string data) {
|
std::string SoRAuthFile(bool save, std::string data) {
|
||||||
|
|
||||||
std::string savedir_path = std::getenv("HOME");
|
std::string home = std::getenv("HOME");
|
||||||
savedir_path.append("/.local/share/bakatui");
|
if (home.empty()) {
|
||||||
|
std::cerr << RED "[ERROR] " RESET << "HOME environment variable not set.\n";
|
||||||
|
safe_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
DIR *savedir = opendir(savedir_path.c_str());
|
std::string savedir_path = home;
|
||||||
if (savedir) {
|
savedir_path.append("/.local/share/bakatui");
|
||||||
/* Directory exists. */
|
|
||||||
closedir(savedir);
|
|
||||||
} else if (ENOENT == errno) {
|
|
||||||
/* Directory does not exist. */
|
|
||||||
std::filesystem::create_directories(savedir_path);
|
|
||||||
} else {
|
|
||||||
/* opendir() failed for some other reason. */
|
|
||||||
std::cerr << "cannot access ~/.local/share/bakatui\n";
|
|
||||||
safe_exit(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string authfile_path = std::string(savedir_path) + "/auth";
|
|
||||||
|
|
||||||
if (save) {
|
if (!std::filesystem::exists(savedir_path)) {
|
||||||
std::ofstream authfile(authfile_path);
|
if (!std::filesystem::create_directories(savedir_path)) {
|
||||||
authfile << data;
|
std::cerr << RED "[ERROR] " RESET << "Failed to create directory: " << savedir_path << "\n";
|
||||||
authfile.close();
|
safe_exit(EXIT_FAILURE);
|
||||||
return "";
|
}
|
||||||
} else {
|
}
|
||||||
std::ifstream authfile(authfile_path);
|
|
||||||
if (authfile.is_open()) {
|
std::string authfile_path = savedir_path + "/auth";
|
||||||
|
|
||||||
|
if (save) {
|
||||||
|
std::ofstream authfile(authfile_path);
|
||||||
|
if (!authfile.is_open()) {
|
||||||
|
std::cerr << RED "[ERROR] " RESET << "Failed to open auth file for writing.\n";
|
||||||
|
safe_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
authfile << data;
|
||||||
|
authfile.close();
|
||||||
|
return "";
|
||||||
|
} else {
|
||||||
|
std::ifstream authfile(authfile_path);
|
||||||
|
if (!authfile.is_open()) {
|
||||||
|
std::cerr << RED "[ERROR] " RESET << "Failed to open auth file for reading.\n";
|
||||||
|
safe_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
data.assign((std::istreambuf_iterator<char>(authfile)),
|
data.assign((std::istreambuf_iterator<char>(authfile)),
|
||||||
std::istreambuf_iterator<char>());
|
std::istreambuf_iterator<char>());
|
||||||
authfile.close();
|
authfile.close();
|
||||||
} else {
|
return data;
|
||||||
std::cerr << RED "[ERROR] " << RESET << "Failed to open auth file\n";
|
|
||||||
safe_exit(-2);
|
|
||||||
}
|
}
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_input_and_login() {
|
void get_input_and_login() {
|
||||||
|
@ -90,6 +90,7 @@ void main_menu() {
|
|||||||
menu_driver(my_menu, REQ_UP_ITEM);
|
menu_driver(my_menu, REQ_UP_ITEM);
|
||||||
break;
|
break;
|
||||||
case 10: // ENTER
|
case 10: // ENTER
|
||||||
|
endwin();
|
||||||
choicesFuncs[item_index(current_item(my_menu))]();
|
choicesFuncs[item_index(current_item(my_menu))]();
|
||||||
pos_menu_cursor(my_menu);
|
pos_menu_cursor(my_menu);
|
||||||
break;
|
break;
|
||||||
|
18
src/net.cpp
18
src/net.cpp
@ -20,8 +20,12 @@
|
|||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
// metods
|
// metods
|
||||||
#define GET 0
|
enum {
|
||||||
#define POST 1
|
GET,
|
||||||
|
POST,
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string access_token;
|
||||||
|
|
||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
|
|
||||||
@ -76,14 +80,12 @@ 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);
|
||||||
|
|
||||||
curl_easy_cleanup(curl); // Cleanup
|
|
||||||
|
|
||||||
return {response, http_code};
|
return {response, http_code};
|
||||||
}
|
}
|
||||||
namespace bakaapi {
|
namespace bakaapi {
|
||||||
|
|
||||||
std::string access_token;
|
|
||||||
|
|
||||||
void login(std::string username, std::string password) {
|
void login(std::string username, std::string password) {
|
||||||
|
|
||||||
std::string req_data =
|
std::string req_data =
|
||||||
@ -119,6 +121,8 @@ void login(std::string username, std::string password) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void refresh_access_token() {
|
void refresh_access_token() {
|
||||||
|
// DEBUG
|
||||||
|
std::clog << "refreshing access token please wait...\n";
|
||||||
|
|
||||||
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
||||||
|
|
||||||
@ -129,6 +133,8 @@ void refresh_access_token() {
|
|||||||
"token&refresh_token={}",
|
"token&refresh_token={}",
|
||||||
refresh_token);
|
refresh_token);
|
||||||
|
|
||||||
|
// DEBUG
|
||||||
|
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
|
||||||
@ -155,6 +161,8 @@ json get_grades() {
|
|||||||
auto [response, http_code] = send_curl_request("api/3/marks", GET, req_data);
|
auto [response, http_code] = send_curl_request("api/3/marks", GET, req_data);
|
||||||
|
|
||||||
if(http_code != 200) {
|
if(http_code != 200) {
|
||||||
|
// DEBUG
|
||||||
|
std::clog << "Failed geting marks code: " << http_code << "\nrequest: " << req_data <<"\nresponse: " << response << std::endl;
|
||||||
refresh_access_token();
|
refresh_access_token();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user