diff --git a/src/api.rs b/src/api.rs index f4d0933..023ac14 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,7 +1,7 @@ -pub async fn fetch_next_queue_item(base_url: &String) -> 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!("{base_url}/{fetch_endpoint}"); + let api_url = format!("{}/{fetch_endpoint}", app.uri); client.get(api_url).send().await } @@ -27,24 +27,26 @@ pub mod parsing { pub mod fetch_song_queue_data { pub async fn get_data( - base_url: &String, + // base_url: &String, + app: &crate::config::App, id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::new(); let endpoint = String::from("api/v2/song/queue"); - let api_url = format!("{base_url}/{endpoint}/{id}"); + let api_url = format!("{}/{endpoint}/{id}", app.uri); client.get(api_url).send().await } } pub mod get_metadata_queue { pub async fn get( - base_url: &String, + // base_url: &String, + app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::new(); let endpoint = String::from("api/v2/song/metadata/queue"); - let api_url = format!("{base_url}/{endpoint}"); + let api_url = format!("{}/{endpoint}", app.uri); client .get(api_url) .query(&[("song_queue_id", song_queue_id)]) @@ -90,12 +92,13 @@ pub mod get_metadata_queue { pub mod get_coverart_queue { pub async fn get( - base_url: &String, + // base_url: &String, + app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::new(); let endpoint = String::from("api/v2/coverart/queue"); - let api_url = format!("{base_url}/{endpoint}"); + let api_url = format!("{}/{endpoint}", app.uri); client .get(api_url) .query(&[("song_queue_id", song_queue_id)]) @@ -104,12 +107,13 @@ pub mod get_coverart_queue { } pub async fn get_data( - base_url: &String, + // base_url: &String, + app: &crate::config::App, coverart_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::new(); let endpoint = String::from("api/v2/coverart/queue/data"); - let api_url = format!("{base_url}/{endpoint}/{coverart_queue_id}"); + let api_url = format!("{}/{endpoint}/{coverart_queue_id}", app.uri); client.get(api_url).send().await } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..8265161 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,5 @@ +#[derive(Default, Debug)] +pub struct App { + pub uri: String, + pub token: icarus_models::login_result::LoginResult +} diff --git a/src/main.rs b/src/main.rs index aa85b54..7eaa327 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ pub mod api; +pub mod config; pub mod responses; pub mod the_rest; pub mod update_queued_song; @@ -8,23 +9,42 @@ 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 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, + token: auth::get_token().await, + // ..Default::default() + }; + // let mut service_login = auth::get_token().await; loop { - println!("Base URL: {app_base_url}"); + println!("Base URL: {:?}", app.uri); - match is_queue_empty(&app_base_url).await { + println!("Token: {:?}", app.token); + + if auth::did_token_expire(&app.token).await { + app.token = auth::get_refresh_token(&app.token).await; + println!("Token refreshed"); + println!("Refreshed token: {:?}", app.token); + } + + // TODO: For now, focus on making sure the token is valid before you get here + continue; + + match is_queue_empty(&app).await { Ok((empty, song_queue_item)) => { if !empty { println!("Queue is not empty"); println!("SongQueueItem: {song_queue_item:?}"); + let song_queue_id = song_queue_item.data[0].id; let user_id = song_queue_item.data[0].user_id; // TODO: Do something with the result later - match some_work(&app_base_url, &song_queue_id, &user_id).await { + match some_work(&app, &song_queue_id, &user_id).await { Ok(( _song, _coverart, @@ -32,7 +52,7 @@ async fn main() -> Result<(), Box> { (coverart_queue_id, coverart_queue_path), )) => { match wipe_data_from_queues( - &app_base_url, + &app, &song_queue_id, &coverart_queue_id, ) @@ -71,18 +91,34 @@ async fn main() -> Result<(), Box> { } } + +mod auth { + pub async fn get_token() -> icarus_models::login_result::LoginResult { + icarus_models::login_result::LoginResult::default() + } + + pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool { + true + } + + pub async fn get_refresh_token(login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { + icarus_models::login_result::LoginResult::default() + } +} + async fn wipe_data_from_queues( - app_base_url: &String, + app: &config::App, song_queue_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid, ) -> Result<(), std::io::Error> { - match the_rest::wipe_data::song_queue::wipe_data(app_base_url, song_queue_id).await { + match the_rest::wipe_data::song_queue::wipe_data(app, song_queue_id).await { Ok(response) => match response .json::() .await { Ok(_resp) => match the_rest::wipe_data::coverart_queue::wipe_data( - app_base_url, + // app_base_url, + app, coverart_queue_id, ) .await @@ -124,9 +160,10 @@ async fn cleanup( } async fn is_queue_empty( - api_url: &String, + // api_url: &String, + app: &config::App, ) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> { - match api::fetch_next_queue_item(api_url).await { + match api::fetch_next_queue_item(app).await { Ok(response) => { match response .json::() @@ -147,7 +184,8 @@ async fn is_queue_empty( } async fn some_work( - app_base_url: &String, + // app_base_url: &String, + app: &crate::config::App, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, ) -> Result< @@ -159,12 +197,12 @@ async fn some_work( ), std::io::Error, > { - match prep_song(app_base_url, song_queue_id).await { + match prep_song(app, song_queue_id).await { Ok((song_queue_path, coverart_queue_path, metadata, coverart_queue_id)) => { match apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await { Ok(_applied) => { match update_queued_song::update_queued_song( - app_base_url, + app, &song_queue_path, song_queue_id, ) @@ -182,7 +220,7 @@ async fn some_work( let song_type = String::from("flac"); match the_rest::create_song::create( - app_base_url, + app, &metadata, user_id, &song_type, @@ -197,7 +235,7 @@ async fn some_work( println!("Response: {resp:?}"); let song = &resp.data[0]; - match the_rest::create_coverart::create(app_base_url, &song.id, &coverart_queue_id).await { + match the_rest::create_coverart::create(app, &song.id, &coverart_queue_id).await { Ok(response) => match response.json::().await { Ok(resp) => { println!("CoverArt sent and successfully parsed response"); @@ -233,7 +271,8 @@ async fn some_work( } async fn prep_song( - api_url: &String, + // api_url: &String, + app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result< ( @@ -244,7 +283,7 @@ async fn prep_song( ), reqwest::Error, > { - match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await { + match api::fetch_song_queue_data::get_data(app, song_queue_id).await { Ok(response) => { // Process data here... match api::parsing::parse_response_into_bytes(response).await { @@ -254,7 +293,7 @@ async fn prep_song( println!("Saved at: {song_queue_path:?}"); - match api::get_metadata_queue::get(api_url, song_queue_id).await { + match api::get_metadata_queue::get(app, song_queue_id).await { Ok(response) => { match response .json::() @@ -269,7 +308,7 @@ async fn prep_song( println!("Created at: {created_at:?}"); println!("Getting coverart queue"); - match api::get_coverart_queue::get(api_url, song_queue_id).await + match api::get_coverart_queue::get(app, song_queue_id).await { Ok(response) => { match response.json::().await { @@ -277,7 +316,7 @@ async fn prep_song( let coverart_queue_id = &response.data[0].id; println!("Coverart queue Id: {coverart_queue_id:?}"); - match api::get_coverart_queue::get_data(api_url, coverart_queue_id).await { + match api::get_coverart_queue::get_data(app, coverart_queue_id).await { Ok(response) => match api::parsing::parse_response_into_bytes(response).await { Ok(coverart_queue_bytes) => { let (directory, filename) = generate_coverart_queue_dir_and_filename().await; diff --git a/src/the_rest.rs b/src/the_rest.rs index dbf85fb..3815239 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -2,7 +2,8 @@ pub mod create_song { pub async fn create( - base_url: &String, + // base_url: &String, + app: &crate::config::App, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, song_type: &String, @@ -28,7 +29,7 @@ pub mod create_song { let client = reqwest::Client::builder().build()?; - let url = format!("{base_url}/api/v2/song"); + let url = format!("{}/api/v2/song", app.uri); let request = client.post(url).json(&payload); request.send().await @@ -46,12 +47,13 @@ pub mod create_song { pub mod create_coverart { pub async fn create( - base_url: &String, + // base_url: &String, + app: &crate::config::App, song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::builder().build()?; - let url = format!("{base_url}/api/v2/coverart"); + let url = format!("{}/api/v2/coverart", app.uri); let payload = get_payload(song_id, coverart_queue_id); let request = client.post(url).json(&payload); @@ -77,11 +79,12 @@ pub mod create_coverart { pub mod wipe_data { pub mod song_queue { pub async fn wipe_data( - base_url: &String, + // base_url: &String, + app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::builder().build()?; - let url = format!("{base_url}/api/v2/song/queue/data/wipe"); + let url = format!("{}/api/v2/song/queue/data/wipe", app.uri); let payload = serde_json::json!({ "song_queue_id": song_queue_id }); @@ -100,11 +103,12 @@ pub mod wipe_data { } pub mod coverart_queue { pub async fn wipe_data( - base_url: &String, + // base_url: &String, + app: &crate::config::App, coverart_queue_id: &uuid::Uuid, ) -> Result { let client = reqwest::Client::builder().build()?; - let url = format!("{base_url}/api/v2/coverart/queue/data/wipe"); + let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri); let payload = serde_json::json!({ "coverart_queue_id": coverart_queue_id }); diff --git a/src/update_queued_song.rs b/src/update_queued_song.rs index b7f3322..512fa74 100644 --- a/src/update_queued_song.rs +++ b/src/update_queued_song.rs @@ -1,5 +1,6 @@ pub async fn update_queued_song( - base_url: &String, + // base_url: &String, + app: &crate::config::App, song_path: &String, song_queue_id: &uuid::Uuid, ) -> Result { @@ -14,7 +15,7 @@ pub async fn update_queued_song( .file_name("track01.flac"), ); - let url = format!("{base_url}/api/v2/song/queue/{song_queue_id}"); + let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri); println!("Url: {url:?}"); let request = client.patch(url).multipart(form);