From a85668d3cd8ddbe182386f4020855961634300e7 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 27 Aug 2025 15:15:00 -0400 Subject: [PATCH 1/5] Added print statement and TODO --- src/managers/commit_manager.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/managers/commit_manager.rs b/src/managers/commit_manager.rs index b3098e1..a155771 100644 --- a/src/managers/commit_manager.rs +++ b/src/managers/commit_manager.rs @@ -230,6 +230,8 @@ impl CommitManager { let api = prsr.retrieve_api(); let token = self.parse_token(&api); + println!("Token {token:?}"); + let mut repo = syncers::retrieve_records::RetrieveRecords { api: api.clone() }; let result_fut = repo.get_all_songs(&token); @@ -248,6 +250,7 @@ impl CommitManager { } } + // TODO: Remove at some point fn upload_song(&self) { println!("Deleting song"); panic!("Not supported"); -- 2.47.3 From f4bd001d64f5077ae02ad152e2ed5764b3756f68 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 27 Aug 2025 15:58:31 -0400 Subject: [PATCH 2/5] Refactoring token fetching code Adding two APIs. One for auth and the other for the main app --- src/managers/action_managers.rs | 2 ++ src/managers/commit_manager.rs | 52 ++++++++++++++++++++------------- src/managers/token_manager.rs | 2 +- src/parsers/api_parser.rs | 49 ++++++++++++++++++++++++------- 4 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/managers/action_managers.rs b/src/managers/action_managers.rs index a526ecf..680981c 100644 --- a/src/managers/action_managers.rs +++ b/src/managers/action_managers.rs @@ -35,6 +35,7 @@ impl ActionManager { String::from("-p"), String::from("-t"), String::from("-h"), + String::from("-ha"), String::from("-s"), String::from("-sd"), String::from("-sr"), @@ -75,6 +76,7 @@ impl ActionManager { let mut flg = models::flags::Flags::default(); + // TODO: Refactor this if self.is_valid_flag(flag) && self.does_flag_have_value(flag) { println!("Flag has value"); flg.flag = String::from(flag); diff --git a/src/managers/commit_manager.rs b/src/managers/commit_manager.rs index a155771..22e3f4e 100644 --- a/src/managers/commit_manager.rs +++ b/src/managers/commit_manager.rs @@ -88,6 +88,8 @@ impl CommitManager { println!("{mapped_action:?}"); + // TODO: Move code to get token here and then pass it to the respective functions + match mapped_action { ActionValues::DeleteAct => self.delete_song(), ActionValues::DownloadAct => self.download_song(), @@ -132,13 +134,14 @@ impl CommitManager { fn delete_song(&self) { let mut prsr = parsers::api_parser::APIParser { + apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), - api: models::api::Api::default(), }; - prsr.parse_api(); - let api = prsr.retrieve_api(); + prsr.parse_api(parsers::api_parser::APIType::Main); + prsr.parse_api(parsers::api_parser::APIType::Auth); + let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth); - let token = self.parse_token(&api); + let token = self.parse_token(&auth_api); println!("Deleting song"); @@ -153,6 +156,7 @@ impl CommitManager { } } + let api = prsr.retrieve_api(parsers::api_parser::APIType::Main); let mut del = syncers::delete::Delete { api: api.clone() }; println!("Deleting song.."); @@ -175,14 +179,17 @@ impl CommitManager { let song_id = uuid::Uuid::from_str(dwn.as_str()).unwrap(); let mut prsr = parsers::api_parser::APIParser { - api: models::api::Api::default(), + apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), }; - prsr.parse_api(); + // parsers::api_parser::APIType + prsr.parse_api(parsers::api_parser::APIType::Main); + prsr.parse_api(parsers::api_parser::APIType::Auth); - let api = prsr.retrieve_api(); - let token = self.parse_token(&api); + let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth); + let token = self.parse_token(&auth_api); println!("Message: {}", token.message); + let api = prsr.retrieve_api(parsers::api_parser::APIType::Main); let mut dwn_loader = syncers::download::Download { api: api.clone() }; let song = icarus_models::song::Song { @@ -223,15 +230,18 @@ impl CommitManager { } let mut prsr = parsers::api_parser::APIParser { - api: models::api::Api::default(), + apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), }; - prsr.parse_api(); + prsr.parse_api(parsers::api_parser::APIType::Main); + prsr.parse_api(parsers::api_parser::APIType::Auth); - let api = prsr.retrieve_api(); - let token = self.parse_token(&api); + let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth); + let token = self.parse_token(&auth_api); println!("Token {token:?}"); + let api = prsr.retrieve_api(parsers::api_parser::APIType::Main); + let mut repo = syncers::retrieve_records::RetrieveRecords { api: api.clone() }; let result_fut = repo.get_all_songs(&token); @@ -321,13 +331,14 @@ impl CommitManager { cover_path: &str, ) -> Result<()> { let mut prsr = parsers::api_parser::APIParser { - api: models::api::Api::default(), + apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), }; - prsr.parse_api(); + prsr.parse_api(parsers::api_parser::APIType::Main); + prsr.parse_api(parsers::api_parser::APIType::Auth); - let api = prsr.retrieve_api(); - let token = self.parse_token(&api); + let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth); + let token = self.parse_token(&auth_api); println!("Token: {:?}", token.token); @@ -447,12 +458,13 @@ impl CommitManager { fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> { let mut prsr = parsers::api_parser::APIParser { - api: models::api::Api::default(), + apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), }; - prsr.parse_api(); - let api = prsr.retrieve_api(); - let token = self.parse_token(&api); + prsr.parse_api(parsers::api_parser::APIType::Main); + prsr.parse_api(parsers::api_parser::APIType::Auth); + let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth); + let token = self.parse_token(&auth_api); let directory_path = std::path::Path::new(&sourcepath); diff --git a/src/managers/token_manager.rs b/src/managers/token_manager.rs index dfa5267..1cfe53b 100644 --- a/src/managers/token_manager.rs +++ b/src/managers/token_manager.rs @@ -63,7 +63,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); } diff --git a/src/parsers/api_parser.rs b/src/parsers/api_parser.rs index b12c232..2ac7af8 100644 --- a/src/parsers/api_parser.rs +++ b/src/parsers/api_parser.rs @@ -2,16 +2,26 @@ use crate::models; #[derive(Clone, Debug)] pub struct APIParser { - pub api: models::api::Api, + pub apis: Vec, pub ica_act: models::icarus_action::IcarusAction, } +pub const API_VERSION: &str = "v2"; + +pub enum APIType { + Main, + Auth, +} + impl APIParser { - pub fn retrieve_api(&self) -> models::api::Api { - self.api.clone() + pub fn retrieve_api(&self, api_type: APIType) -> models::api::Api { + match api_type { + APIType::Main => self.apis[0].clone(), + APIType::Auth => self.apis[1].clone() + } } - pub fn parse_api(&mut self) { + pub fn parse_api(&mut self, api_type: APIType) { let flags = self.ica_act.flags.clone(); println!("Parsing api"); @@ -19,16 +29,33 @@ impl APIParser { let arg = elem.flag; let value = elem.value; - if arg == "-h" { - if value.chars().nth(value.len() - 1) == Some('/') { - self.api.url = value; - } else { - self.api.url = value + "/"; + match api_type { + APIType::Main => { + if arg == "-h" { + if value.chars().nth(value.len() - 1) == Some('/') { + self.apis[0].url = value; + } else { + self.apis[0].url = value + "/"; + } + break; + } + }, + APIType::Auth => { + if arg == "-ha" { + if value.chars().nth(value.len() - 1) == Some('/') { + self.apis[1].url = value; + } else { + self.apis[1].url = value + "/"; + } + break; + } } - break; } + } - self.api.version = "v1".to_string(); + // for api in self.apis { + // } + // self.api.version = String::from(API_VERSION); } } -- 2.47.3 From 65d0e35c85800283317c552749fcc5efc6b45cf2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 27 Aug 2025 16:09:10 -0400 Subject: [PATCH 3/5] Fixed token code --- src/managers/token_manager.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/managers/token_manager.rs b/src/managers/token_manager.rs index 1cfe53b..a539f0e 100644 --- a/src/managers/token_manager.rs +++ b/src/managers/token_manager.rs @@ -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, + } + } +} + 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::().await { - Ok(parsed) => { - token = parsed; + match response.json::().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."), }; -- 2.47.3 From ea723d87f2a5f1107e64640728d85837b378741f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 27 Aug 2025 16:18:31 -0400 Subject: [PATCH 4/5] Code formatting --- src/parsers/api_parser.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/parsers/api_parser.rs b/src/parsers/api_parser.rs index 2ac7af8..230df46 100644 --- a/src/parsers/api_parser.rs +++ b/src/parsers/api_parser.rs @@ -17,7 +17,7 @@ impl APIParser { pub fn retrieve_api(&self, api_type: APIType) -> models::api::Api { match api_type { APIType::Main => self.apis[0].clone(), - APIType::Auth => self.apis[1].clone() + APIType::Auth => self.apis[1].clone(), } } @@ -39,7 +39,7 @@ impl APIParser { } break; } - }, + } APIType::Auth => { if arg == "-ha" { if value.chars().nth(value.len() - 1) == Some('/') { @@ -51,7 +51,6 @@ impl APIParser { } } } - } // for api in self.apis { -- 2.47.3 From 15effe60c5596876da39f3519fc64bffb80e318c Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Wed, 27 Aug 2025 16:28:50 -0400 Subject: [PATCH 5/5] Cleanup --- src/managers/commit_manager.rs | 1 - src/parsers/api_parser.rs | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/managers/commit_manager.rs b/src/managers/commit_manager.rs index 22e3f4e..3eb3709 100644 --- a/src/managers/commit_manager.rs +++ b/src/managers/commit_manager.rs @@ -182,7 +182,6 @@ impl CommitManager { apis: vec![models::api::Api::default(), models::api::Api::default()], ica_act: self.ica_action.clone(), }; - // parsers::api_parser::APIType prsr.parse_api(parsers::api_parser::APIType::Main); prsr.parse_api(parsers::api_parser::APIType::Auth); diff --git a/src/parsers/api_parser.rs b/src/parsers/api_parser.rs index 230df46..61b86dc 100644 --- a/src/parsers/api_parser.rs +++ b/src/parsers/api_parser.rs @@ -35,7 +35,7 @@ impl APIParser { if value.chars().nth(value.len() - 1) == Some('/') { self.apis[0].url = value; } else { - self.apis[0].url = value + "/"; + self.apis[0].url = format!("{value}/"); } break; } @@ -45,7 +45,7 @@ impl APIParser { if value.chars().nth(value.len() - 1) == Some('/') { self.apis[1].url = value; } else { - self.apis[1].url = value + "/"; + self.apis[1].url = format!("{value}/"); } break; } @@ -53,8 +53,8 @@ impl APIParser { } } - // for api in self.apis { - // } - // self.api.version = String::from(API_VERSION); + for api in &mut self.apis { + api.version = String::from(API_VERSION); + } } } -- 2.47.3