tiny refactor

This commit is contained in:
PoliEcho 2025-03-06 12:00:26 +01:00
parent 4f852161aa
commit 0d11dbec32
8 changed files with 47 additions and 11 deletions

View File

@ -1 +1,2 @@
#define VERSION "0.5" #define VERSION "0.5"
#define NAME "bakatui"

View File

@ -5,6 +5,7 @@
#include "marks.h" #include "marks.h"
#include "net.h" #include "net.h"
#include <csignal> #include <csignal>
#include <cstdlib>
#include <curl/curl.h> #include <curl/curl.h>
#include <curses.h> #include <curses.h>
#include <fstream> #include <fstream>
@ -12,9 +13,18 @@
#include <regex> #include <regex>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include "const.h"
std::string baka_api_url; std::string baka_api_url;
void PrintHelp() {
std::cout << "Usage: " << NAME << " [OPTIONS]" << "\n"
<< "-h Show this help menu\n"
<< "-v Show version\n"
<< "-L Force new login\n";
safe_exit(0);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
// signal handlers // signal handlers
signal(SIGTERM, safe_exit); signal(SIGTERM, safe_exit);
@ -25,10 +35,17 @@ int main(int argc, char **argv) {
// error signal handlers // error signal handlers
signal(SIGSEGV, safe_exit); signal(SIGSEGV, safe_exit);
// marks_page();
{ {
int opt;
while ((opt = getopt(argc, argv, "vf:")) != -1) { // "f:" expects a value
switch (opt) {
case 'h': PrintHelp(); break;
case 'L': break;
case 'v': break;
default: /* handle error */;
}
}
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";

View File

@ -12,7 +12,7 @@
#define CTRLD 4 #define CTRLD 4
char *choices[] = { char *choices[] = {
"login", "Marks", "shedule", "Komens", "login", "Marks", "timetable", "Komens",
"Homework", "Absence", "Exit", (char *)NULL, "Homework", "Absence", "Exit", (char *)NULL,
}; };
void (*choicesFuncs[])() = { void (*choicesFuncs[])() = {

View File

@ -58,7 +58,7 @@ void win_show(WINDOW *win, char *label, int label_color, int width, int height,
void marks_page() { void marks_page() {
// DONT FORGET TO UNCOMMENT // DONT FORGET TO UNCOMMENT
json resp_from_api = bakaapi::get_grades(); json resp_from_api = bakaapi::get_data_from_endpoint("api/3/marks");
// std::ifstream f("test-data/marks3.json"); // std::ifstream f("test-data/marks3.json");
// json resp_from_api = json::parse(f); // json resp_from_api = json::parse(f);

View File

@ -155,21 +155,25 @@ void refresh_access_token() {
access_token = resp_parsed["access_token"]; access_token = resp_parsed["access_token"];
} }
void is_access_token_empty() {
json get_grades() {
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
json get_data_from_endpoint(std::string endpoint) {
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("api/3/marks", 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 marks code: " << http_code << "\nrequest: " << req_data <<"\nresponse: " << response << std::endl; std::clog << "Failed geting data from endpoint: " << endpoint << " code: " << http_code << "\nrequest: " << req_data <<"\nresponse: " << response << std::endl;
refresh_access_token(); refresh_access_token();
} }

View File

@ -8,5 +8,5 @@ extern CURL *curl;
namespace bakaapi { namespace bakaapi {
void login(std::string username, std::string password); void login(std::string username, std::string password);
void refresh_access_token(); void refresh_access_token();
json get_grades(); json get_data_from_endpoint(std::string endpoint);
} // namespace bakaapi } // namespace bakaapi

13
src/timetable.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "timetable.h"
#define NLINES 10
#define NCOLS 40
#define DEFAULT_X_OFFSET 10
#define DEFAULT_Y_OFFSET 2
#define DEFAULT_PADDING 4
void timetable_page() {
}

1
src/timetable.h Normal file
View File

@ -0,0 +1 @@
void timetable_page();