huh
This commit is contained in:
parent
b0f58f5f40
commit
a84b70991d
@ -7,6 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
void safe_exit(int code) {
|
void safe_exit(int code) {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
@ -60,9 +61,15 @@ std::string SoRAuthFile(bool save, std::string data){
|
|||||||
std::string authfile_path = std::string(savedir_path) + "/auth";
|
std::string authfile_path = std::string(savedir_path) + "/auth";
|
||||||
|
|
||||||
if(save){
|
if(save){
|
||||||
|
std::ofstream authfile(authfile_path);
|
||||||
|
authfile << data;
|
||||||
|
authfile.close();
|
||||||
|
return "";
|
||||||
} else {
|
} else {
|
||||||
|
std::ifstream authfile(authfile_path);
|
||||||
|
authfile >> data;
|
||||||
|
authfile.close();
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
void safe_exit(int code);
|
void safe_exit(int code);
|
||||||
std::string bool_to_string(bool bool_in);
|
std::string bool_to_string(bool bool_in);
|
||||||
|
std::string SoRAuthFile(bool save, std::string data);
|
||||||
48
src/net.cpp
48
src/net.cpp
@ -52,9 +52,6 @@ std::tuple<std::string, int> send_curl_request(std::string endpoint,
|
|||||||
|
|
||||||
if (type == "POST") {
|
if (type == "POST") {
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||||
} else {
|
|
||||||
std::cerr << RED "[ERROR] " << RESET "invalid method\n";
|
|
||||||
safe_exit(-5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -87,31 +84,12 @@ void login(std::string username, std::string password) {
|
|||||||
safe_exit(55);
|
safe_exit(55);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string savedir_path = std::getenv("HOME");
|
SoRAuthFile(true, response);
|
||||||
savedir_path.append("/.local/share/bakatui");
|
|
||||||
|
|
||||||
DIR *savedir = opendir(savedir_path.c_str());
|
|
||||||
if (savedir) {
|
|
||||||
/* 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";
|
|
||||||
std::ofstream authfile;
|
|
||||||
authfile.open(authfile_path);
|
|
||||||
authfile << response;
|
|
||||||
authfile.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
{
|
||||||
|
std::string savedir_path = std::getenv("HOME");
|
||||||
|
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);
|
||||||
@ -128,12 +106,8 @@ void login(std::string username, std::string password) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void refresh_access_token() {
|
void refresh_access_token() {
|
||||||
std::string savedir_path = std::getenv("HOME");
|
|
||||||
savedir_path.append("/.local/share/bakatui");
|
|
||||||
std::string authfile_path = std::string(savedir_path) + "/auth";
|
|
||||||
|
|
||||||
std::ifstream authfile(authfile_path);
|
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
||||||
json authfile_parsed = json::parse(authfile);
|
|
||||||
|
|
||||||
std::string refresh_token = authfile_parsed["refresh_token"];
|
std::string refresh_token = authfile_parsed["refresh_token"];
|
||||||
|
|
||||||
@ -149,12 +123,7 @@ void refresh_access_token() {
|
|||||||
safe_exit(55);
|
safe_exit(55);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
SoRAuthFile(true, response);
|
||||||
std::ofstream authfile_out;
|
|
||||||
authfile_out.open(authfile_path);
|
|
||||||
authfile_out << response;
|
|
||||||
authfile_out.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
json resp_parsed = json::parse(response);
|
json resp_parsed = json::parse(response);
|
||||||
|
|
||||||
@ -163,13 +132,14 @@ void refresh_access_token() {
|
|||||||
|
|
||||||
json get_grades() {
|
json get_grades() {
|
||||||
if(access_token.empty()) {
|
if(access_token.empty()) {
|
||||||
|
json authfile_parsed = json::parse(SoRAuthFile(false, ""));
|
||||||
|
access_token = authfile_parsed["access_token"];
|
||||||
}
|
}
|
||||||
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", "POST", req_data);
|
auto [response, http_code] = send_curl_request("api/3/marks", "GET", req_data);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -3,5 +3,4 @@
|
|||||||
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();
|
||||||
bool is_logged_in();
|
|
||||||
} // namespace bakaapi
|
} // namespace bakaapi
|
||||||
Loading…
x
Reference in New Issue
Block a user