Token fetch (#56)

* Added print statement and TODO

* Refactoring token fetching code

Adding two APIs. One for auth and the other for the main app

* Fixed token code

* Code formatting

* Cleanup
This commit was merged in pull request #56.
This commit is contained in:
KD
2025-08-27 16:33:18 -04:00
committed by GitHub
parent 77cb5c8599
commit 8a07718ce8
4 changed files with 93 additions and 35 deletions
+20 -4
View File
@@ -2,6 +2,16 @@ use std::default::Default;
use crate::models;
mod response {
pub mod token {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Response {
pub message: String,
pub data: Vec<icarus_models::login_result::LoginResult>,
}
}
}
pub struct TokenManager {
pub user: icarus_models::user::User,
pub api: models::api::Api,
@@ -43,9 +53,15 @@ impl TokenManager {
match response.status() {
reqwest::StatusCode::OK => {
// on success, parse our JSON to an APIResponse
match response.json::<icarus_models::token::AccessToken>().await {
Ok(parsed) => {
token = parsed;
match response.json::<response::token::Response>().await {
Ok(response) => {
let login_result = &response.data[0];
token.user_id = login_result.id;
token.username = login_result.username.clone();
token.token = login_result.token.clone();
token.token_type = login_result.token_type.clone();
token.expiration = login_result.expiration;
token.message = response.message;
}
Err(_) => println!("Hm, the response didn't match the shape we expected."),
};
@@ -63,7 +79,7 @@ impl TokenManager {
pub fn init(&mut self) {
let api = &mut self.api;
api.version = String::from("v1");
api.version = String::from(crate::parsers::api_parser::API_VERSION);
api.endpoint = format!("api/{}/login", api.version);
}