From 763ffcfaa601d229a7a0dc59f02d0221eea3ea6c Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Thu, 14 Nov 2024 17:18:02 +0100 Subject: [PATCH] i am so stupid --- src/main.cpp | 7 +++---- src/main_menu.cpp | 5 +---- src/net.cpp | 26 +++++++++++++++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 897dbf3..ffc2101 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,9 +23,6 @@ int main(int argc, char **argv) { // error signal handlers signal(SIGSEGV, safe_exit); - // main_menu(); - // bakaapi::is_logged_in(); - /* std::cout << "enter school bakalari url:\n"; while (true) { std::cout << "(or q to quit )"; @@ -46,7 +43,7 @@ int main(int argc, char **argv) { if (baka_api_url.back() != '/') { baka_api_url.append("/"); } -*/ + { std::string username; std::cout << "enter username: "; @@ -59,6 +56,8 @@ int main(int argc, char **argv) { // std::cin >> password; bakaapi::login(username, password); + + main_menu(); } return 0; } \ No newline at end of file diff --git a/src/main_menu.cpp b/src/main_menu.cpp index a0cd0fa..9b65ac1 100644 --- a/src/main_menu.cpp +++ b/src/main_menu.cpp @@ -57,10 +57,7 @@ void main_menu() { /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); - char title_text[40] = "Main Menu - login status: "; - // strcat(title_text, bool_to_string(bakaapi::is_logged_in()).c_str()); - - print_in_middle(my_menu_win, 1, 0, 40, title_text, COLOR_PAIR(1)); + print_in_middle(my_menu_win, 1, 0, 40, "Main Menu", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); mvwaddch(my_menu_win, 2, 39, ACS_RTEE); diff --git a/src/net.cpp b/src/net.cpp index f0ec6bd..48fa854 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -28,8 +28,9 @@ size_t WriteCallback(void *contents, size_t size, size_t nmemb, return totalSize; } -std::string send_curl_request(std::string endpoint, std::string type, - std::string req_data) { +std::tuple send_curl_request(std::string endpoint, + std::string type, + std::string req_data) { std::string response; std::string url = baka_api_url + endpoint; @@ -62,9 +63,12 @@ std::string send_curl_request(std::string endpoint, std::string type, } curl_easy_perform(curl); // Perform the request + int http_code = 0; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + curl_easy_cleanup(curl); // Cleanup - return response; + return {response, http_code}; } namespace bakaapi { @@ -76,7 +80,12 @@ void login(std::string username, std::string password) { std::format("client_id=ANDR&grant_type=password&username={}&password={}", username, password); - std::string response = send_curl_request("api/login", "POST", req_data); + auto [response, http_code] = send_curl_request("api/login", "POST", req_data); + if (http_code != 200) { + std::cerr << RED "[ERROR] " << RESET << http_code + << "is non 200 response\n"; + safe_exit(55); + } std::string savedir_path = std::getenv("HOME"); savedir_path.append("/.local/share/bakatui"); @@ -124,7 +133,12 @@ void refresh_access_token() { "token&refresh_token={}", refresh_token); - std::string response = send_curl_request("api/login", "POST", req_data); + auto [response, http_code] = send_curl_request("api/login", "POST", req_data); + if (http_code != 200) { + std::cerr << RED "[ERROR] " << RESET << http_code + << "is non 200 response\n"; + safe_exit(55); + } { std::ofstream authfile_out; @@ -137,4 +151,6 @@ void refresh_access_token() { access_token = resp_parsed["access_token"]; } + +bool is_logged_in() {} } // namespace bakaapi \ No newline at end of file