From 67d2729c464643465a3220d28690fc29ad41e28a Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 19:04:34 -0400 Subject: [PATCH] tsk-41: Code cleanup --- src/api.rs | 13 +-- src/config.rs | 2 +- src/main.rs | 165 ++++++++++++++++---------------------- src/the_rest.rs | 4 - src/update_queued_song.rs | 1 - 5 files changed, 80 insertions(+), 105 deletions(-) diff --git a/src/api.rs b/src/api.rs index ff479c6..4b7a8c7 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,14 +1,17 @@ -pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result { +pub async fn fetch_next_queue_item( + app: &crate::config::App, +) -> Result { let client = reqwest::Client::new(); let fetch_endpoint = String::from("api/v2/song/queue/next"); let api_url = format!("{}/{fetch_endpoint}", app.uri); let (key, header) = auth_header(app).await; - - client.get(api_url).header(key, header) - .send().await + + client.get(api_url).header(key, header).send().await } -pub async fn auth_header(app: &crate::config::App) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) { +pub async fn auth_header( + app: &crate::config::App, +) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) { let bearer = format!("Bearer {}", app.token.token); let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap(); (reqwest::header::AUTHORIZATION, header_value) diff --git a/src/config.rs b/src/config.rs index 06615ec..8f07e8c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,5 +2,5 @@ pub struct App { pub uri: String, pub auth_uri: String, - pub token: icarus_models::login_result::LoginResult + pub token: icarus_models::login_result::LoginResult, } diff --git a/src/main.rs b/src/main.rs index 6912de9..86b306c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,14 +9,11 @@ use std::io::Write; pub const SECONDS_TO_SLEEP: u64 = 5; - #[tokio::main] async fn main() -> Result<(), Box> { - // let app_base_url = icarus_envy::environment::get_icarus_base_api_url().await; let mut app = config::App { uri: icarus_envy::environment::get_icarus_base_api_url().await, auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url().await, - // token: auth::get_token(&app).await, ..Default::default() }; match auth::get_token(&app).await { @@ -28,32 +25,27 @@ async fn main() -> Result<(), Box> { std::process::exit(-1); } }; - // let mut service_login = auth::get_token().await; loop { println!("Base URL: {:?}", app.uri); - + println!("Auth URL: {:?}", app.auth_uri); println!("Token: {:?}", app.token); if auth::did_token_expire(&app.token).await { - println!("Token did expire"); - app.token = match auth::get_refresh_token(&app, &app.token).await { - Ok(login_result) => { - login_result - } + println!("Token expired"); + app.token = match auth::get_refresh_token(&app).await { + Ok(login_result) => login_result, Err(err) => { eprintln!("Error: {err:?}"); continue; } }; - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; + println!("Token refreshed"); println!("Refreshed token: {:?}", app.token); } else { println!("Token did not expire"); } - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; - match is_queue_empty(&app).await { Ok((empty, song_queue_item)) => { @@ -72,12 +64,8 @@ async fn main() -> Result<(), Box> { (song_queue_id, song_queue_path), (coverart_queue_id, coverart_queue_path), )) => { - match wipe_data_from_queues( - &app, - &song_queue_id, - &coverart_queue_id, - ) - .await + match wipe_data_from_queues(&app, &song_queue_id, &coverart_queue_id) + .await { Ok(_) => { match cleanup(&song_queue_path, &coverart_queue_path).await { @@ -112,56 +100,23 @@ async fn main() -> Result<(), Box> { } } - mod auth { - pub async fn get_token(app: &crate::config::App) -> Result { + pub async fn get_token( + app: &crate::config::App, + ) -> Result { let client = reqwest::Client::new(); let endpoint = String::from("api/v2/service/login"); let api_url = format!("{}/{endpoint}", app.auth_uri); - + let payload = serde_json::json!({ "passphrase": icarus_envy::environment::get_service_passphrase().await, }); - match client.post(api_url).json(&payload) - .send().await { - Ok(response) => match response.json::().await { - Ok(resp) => { - if resp.data.is_empty() { - Err(std::io::Error::other(String::from("No token returned"))) - } else { - Ok(resp.data[0].clone()) - } - } - Err(err) => { - Err(std::io::Error::other(err.to_string())) - } - } - Err(err) => { - Err(std::io::Error::other(err.to_string())) - } - } - } - - // TODO: Might want to put the functionality within icarus_models at some point - pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool { - let current_time = time::OffsetDateTime::now_utc(); - let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); - current_time > expire_time - } - - // TODO: Refactor this to only have one function parameter - pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> Result { - let client = reqwest::Client::new(); - let endpoint = String::from("api/v2/token/refresh"); - let api_url = format!("{}/{endpoint}", app.auth_uri); - - let payload = serde_json::json!({ - "access_token": login_result.token - }); - match client.post(api_url).json(&payload).send().await { - Ok(response) => match response.json::().await { + Ok(response) => match response + .json::() + .await + { Ok(resp) => { if resp.data.is_empty() { Err(std::io::Error::other(String::from("No token returned"))) @@ -169,13 +124,46 @@ mod auth { Ok(resp.data[0].clone()) } } - Err(err) => { - Err(std::io::Error::other(err.to_string())) + Err(err) => Err(std::io::Error::other(err.to_string())), + }, + Err(err) => Err(std::io::Error::other(err.to_string())), + } + } + + // TODO: Might want to put the functionality within icarus_models at some point + pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool { + let current_time = time::OffsetDateTime::now_utc(); + let expire_time = + time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); + current_time > expire_time + } + + pub async fn get_refresh_token( + app: &crate::config::App, + ) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/token/refresh"); + let api_url = format!("{}/{endpoint}", app.auth_uri); + + let payload = serde_json::json!({ + "access_token": app.token.token + }); + + match client.post(api_url).json(&payload).send().await { + Ok(response) => match response + .json::() + .await + { + Ok(resp) => { + if resp.data.is_empty() { + Err(std::io::Error::other(String::from("No token returned"))) + } else { + Ok(resp.data[0].clone()) + } } - } - Err(err) => { - Err(std::io::Error::other(err.to_string())) - } + Err(err) => Err(std::io::Error::other(err.to_string())), + }, + Err(err) => Err(std::io::Error::other(err.to_string())), } } } @@ -190,26 +178,22 @@ async fn wipe_data_from_queues( .json::() .await { - Ok(_resp) => match the_rest::wipe_data::coverart_queue::wipe_data( - // app_base_url, - app, - coverart_queue_id, - ) - .await - { - Ok(inner_response) => match inner_response - .json::() - .await - { - Ok(_inner_resp) => { - println!("Wiped data from CoverArt queue"); - println!("Resp: {_inner_resp:?}"); - Ok(()) - } + Ok(_resp) => { + match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await { + Ok(inner_response) => match inner_response + .json::() + .await + { + Ok(_inner_resp) => { + println!("Wiped data from CoverArt queue"); + println!("Resp: {_inner_resp:?}"); + Ok(()) + } + Err(err) => Err(std::io::Error::other(err.to_string())), + }, Err(err) => Err(std::io::Error::other(err.to_string())), - }, - Err(err) => Err(std::io::Error::other(err.to_string())), - }, + } + } Err(err) => Err(std::io::Error::other(err.to_string())), }, Err(err) => Err(std::io::Error::other(err.to_string())), @@ -234,7 +218,6 @@ async fn cleanup( } async fn is_queue_empty( - // api_url: &String, app: &config::App, ) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> { match api::fetch_next_queue_item(app).await { @@ -258,7 +241,6 @@ async fn is_queue_empty( } async fn some_work( - // app_base_url: &String, app: &crate::config::App, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, @@ -294,10 +276,7 @@ async fn some_work( let song_type = String::from("flac"); match the_rest::create_song::create( - app, - &metadata, - user_id, - &song_type, + app, &metadata, user_id, &song_type, ) .await { @@ -345,7 +324,6 @@ async fn some_work( } async fn prep_song( - // api_url: &String, app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result< @@ -382,8 +360,7 @@ async fn prep_song( println!("Created at: {created_at:?}"); println!("Getting coverart queue"); - match api::get_coverart_queue::get(app, song_queue_id).await - { + match api::get_coverart_queue::get(app, song_queue_id).await { Ok(response) => { match response.json::().await { Ok(response) => { diff --git a/src/the_rest.rs b/src/the_rest.rs index 40967bc..08aeb4b 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -2,7 +2,6 @@ pub mod create_song { pub async fn create( - // base_url: &String, app: &crate::config::App, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, @@ -48,7 +47,6 @@ pub mod create_song { pub mod create_coverart { pub async fn create( - // base_url: &String, app: &crate::config::App, song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid, @@ -81,7 +79,6 @@ pub mod create_coverart { pub mod wipe_data { pub mod song_queue { pub async fn wipe_data( - // base_url: &String, app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { @@ -106,7 +103,6 @@ pub mod wipe_data { } pub mod coverart_queue { pub async fn wipe_data( - // base_url: &String, app: &crate::config::App, coverart_queue_id: &uuid::Uuid, ) -> Result { diff --git a/src/update_queued_song.rs b/src/update_queued_song.rs index b1a2394..7a95dac 100644 --- a/src/update_queued_song.rs +++ b/src/update_queued_song.rs @@ -1,5 +1,4 @@ pub async fn update_queued_song( - // base_url: &String, app: &crate::config::App, song_path: &String, song_queue_id: &uuid::Uuid,