From 09a129abc8afb6c1437c6aac459350a5b86aebba Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 12 Aug 2025 17:31:54 -0400 Subject: [PATCH 01/17] tsk-41: Updated gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index fedaa2b..c2a0c1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target .env +.env.local +.env.docker -- 2.43.0 From 002de87f5e2959c85395ba187755aa19791cbaff Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 12 Aug 2025 17:32:42 -0400 Subject: [PATCH 02/17] tsk-41: Added SECRET_KEY env variable --- .env.docker.sample | 1 + .env.sample | 1 + 2 files changed, 2 insertions(+) diff --git a/.env.docker.sample b/.env.docker.sample index be6a3d3..fd71c8f 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -1,2 +1,3 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 +SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH diff --git a/.env.sample b/.env.sample index be6a3d3..fd71c8f 100644 --- a/.env.sample +++ b/.env.sample @@ -1,2 +1,3 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 +SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH -- 2.43.0 From ce2e4feb0238a0f5527bff84021f5ad6ceb3642f Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 12 Aug 2025 17:41:30 -0400 Subject: [PATCH 03/17] tsk-41: Dependencies version bump Includes icarus_meta, icarus_models, and icarus_envy --- Cargo.lock | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 6 ++-- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 553b8e5..1f5c007 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,21 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100" + [[package]] name = "atomic-waker" version = "1.1.2" @@ -501,8 +516,8 @@ dependencies = [ [[package]] name = "icarus_envy" -version = "0.3.0" -source = "git+ssh://git@git.kundeng.us/phoenix/icarus_envy.git?tag=v0.3.0-devel-d73fba9899-006#d73fba9899372b0655a90cb426645930135152da" +version = "0.3.1" +source = "git+ssh://git@git.kundeng.us/phoenix/icarus_envy.git?tag=v0.3.1-main-3cd42dab6b-006#3cd42dab6b2503609883f5f57ad3508755c34a2e" dependencies = [ "const_format", "dotenvy", @@ -511,16 +526,17 @@ dependencies = [ [[package]] name = "icarus_meta" version = "0.3.0" -source = "git+ssh://git@git.kundeng.us/phoenix/icarus_meta.git?tag=v0.3.0-devel-f4b71de969-680#f4b71de9692029a706b1ce82c39f6715c560158e" +source = "git+ssh://git@git.kundeng.us/phoenix/icarus_meta.git?tag=v0.3.0#f87c049ab3139995230485b33038a0425f2e7430" dependencies = [ "lofty", ] [[package]] name = "icarus_models" -version = "0.4.5" -source = "git+ssh://git@git.kundeng.us/phoenix/icarus_models.git?tag=v0.4.5-devel-655d05dabb-111#655d05dabbdadb9b28940564a1eb82470aa4f166" +version = "0.5.5" +source = "git+ssh://git@git.kundeng.us/phoenix/icarus_models.git?tag=v0.5.5-devel-bd793db08e-111#bd793db08e06b256ffecd9f4528e55e3026fede7" dependencies = [ + "josekit", "rand", "serde", "serde_json", @@ -667,6 +683,23 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "josekit" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a808e078330e6af222eb0044b71d4b1ff981bfef43e7bc8133a88234e0c86a0c" +dependencies = [ + "anyhow", + "base64", + "flate2", + "openssl", + "regex", + "serde", + "serde_json", + "thiserror", + "time", +] + [[package]] name = "js-sys" version = "0.3.77" @@ -1009,6 +1042,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "reqwest" version = "0.12.20" @@ -1195,6 +1257,7 @@ version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ + "indexmap", "itoa", "memchr", "ryu", @@ -1347,6 +1410,26 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "thiserror" +version = "2.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b0949c3a6c842cbde3f1686d6eea5a010516deb7085f79db747562d4102f41e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc5b44b4ab9c2fdd0e0512e6bece8388e214c0749f5862b114cc5b7a25daf227" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.41" diff --git a/Cargo.toml b/Cargo.toml index cd5e6fe..5f5a472 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,6 @@ serde_json = { version = "1.0.140" } time = { version = "0.3.41", features = ["macros", "serde"] } uuid = { version = "1.17.0", features = ["v4", "serde"] } rand = { version = "0.9.1" } -icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0-devel-f4b71de969-680" } -icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.5-devel-655d05dabb-111" } -icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.0-devel-d73fba9899-006" } +icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0" } +icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.5.5-devel-bd793db08e-111" } +icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.1-main-3cd42dab6b-006" } -- 2.43.0 From 3ea008868a1f40284177170ff8ff6d28440a2a3f Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 12 Aug 2025 18:40:11 -0400 Subject: [PATCH 04/17] tsk-41: Refactored code to prepare for auth implementation --- src/api.rs | 24 +++++++----- src/config.rs | 5 +++ src/main.rs | 79 +++++++++++++++++++++++++++++---------- src/the_rest.rs | 20 ++++++---- src/update_queued_song.rs | 5 ++- 5 files changed, 93 insertions(+), 40 deletions(-) create mode 100644 src/config.rs 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); -- 2.43.0 From 0695df3f725c5f4f37565f5b3eecd829af71a385 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 12 Aug 2025 18:42:30 -0400 Subject: [PATCH 05/17] tsk-41: Added TODOs --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index 7eaa327..74cc46a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,14 +93,18 @@ async fn main() -> Result<(), Box> { mod auth { + // TODO: Implement function pub async fn get_token() -> icarus_models::login_result::LoginResult { icarus_models::login_result::LoginResult::default() } + // TODO: Implement function. 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 { true } + // TODO: Implement function pub async fn get_refresh_token(login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { icarus_models::login_result::LoginResult::default() } -- 2.43.0 From 6fafb93aaf102766531a8f9bb8a639717c129184 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 13 Aug 2025 16:50:24 -0400 Subject: [PATCH 06/17] tsk-41: Adding env variables used to get service token --- .env.docker.sample | 1 + .env.sample | 1 + 2 files changed, 2 insertions(+) diff --git a/.env.docker.sample b/.env.docker.sample index fd71c8f..18a8806 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -1,3 +1,4 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH +SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH diff --git a/.env.sample b/.env.sample index fd71c8f..18a8806 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,4 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH +SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH -- 2.43.0 From 80c4c7950b32767d5b38ca7d171067dc3d620277 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 13 Aug 2025 16:56:30 -0400 Subject: [PATCH 07/17] tsk-41: Can fetch the token now --- src/api.rs | 10 +++++++++ src/main.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/api.rs b/src/api.rs index 023ac14..9a380ec 100644 --- a/src/api.rs +++ b/src/api.rs @@ -133,3 +133,13 @@ pub mod get_coverart_queue { } } } + +pub mod service_token { + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} diff --git a/src/main.rs b/src/main.rs index 74cc46a..83792e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,18 +15,29 @@ 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, - token: auth::get_token().await, - // ..Default::default() + // token: auth::get_token(&app).await, + ..Default::default() }; + match auth::get_token(&app).await { + Ok(login_result) => { + app.token = login_result; + } + Err(err) => { + eprintln!("Error: {err:?}"); + std::process::exit(-1); + } + }; + tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; // let mut service_login = auth::get_token().await; loop { println!("Base URL: {:?}", app.uri); println!("Token: {:?}", app.token); + tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; if auth::did_token_expire(&app.token).await { - app.token = auth::get_refresh_token(&app.token).await; + app.token = auth::get_refresh_token(&app, &app.token).await; println!("Token refreshed"); println!("Refreshed token: {:?}", app.token); } @@ -94,8 +105,48 @@ async fn main() -> Result<(), Box> { mod auth { // TODO: Implement function - pub async fn get_token() -> icarus_models::login_result::LoginResult { - icarus_models::login_result::LoginResult::default() + pub async fn get_token(app: &crate::config::App) -> Result { + /* + * + ) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/song/metadata/queue"); + let api_url = format!("{}/{endpoint}", app.uri); + client + .get(api_url) + .query(&[("song_queue_id", song_queue_id)]) + .send() + */ + let client = reqwest::Client::new(); + let endpoint = String::from("api/v2/service/login"); + let api_url = format!("{}/{endpoint}", app.uri); + + let payload = serde_json::json!({ + "passphrase": icarus_envy::environment::get_service_passphrase().await, + }); + + println!("Payload: {payload:?}"); + + 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())) + } + } + + // icarus_models::login_result::LoginResult::default() } // TODO: Implement function. Might want to put the functionality within icarus_models @@ -105,7 +156,7 @@ mod auth { } // TODO: Implement function - pub async fn get_refresh_token(login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { + pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { icarus_models::login_result::LoginResult::default() } } -- 2.43.0 From 62a1f8a00319a7266954c5b3fc67a391439430ed Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 13 Aug 2025 17:03:48 -0400 Subject: [PATCH 08/17] tsk-41: Token expiration check --- src/main.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 83792e7..a70a0c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,6 @@ async fn main() -> Result<(), Box> { std::process::exit(-1); } }; - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; // let mut service_login = auth::get_token().await; loop { @@ -37,10 +36,15 @@ async fn main() -> Result<(), Box> { tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; if auth::did_token_expire(&app.token).await { + println!("Token did expire"); app.token = auth::get_refresh_token(&app, &app.token).await; + 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; // TODO: For now, focus on making sure the token is valid before you get here continue; @@ -104,19 +108,7 @@ async fn main() -> Result<(), Box> { mod auth { - // TODO: Implement function pub async fn get_token(app: &crate::config::App) -> Result { - /* - * - ) -> Result { - let client = reqwest::Client::new(); - let endpoint = String::from("api/v2/song/metadata/queue"); - let api_url = format!("{}/{endpoint}", app.uri); - client - .get(api_url) - .query(&[("song_queue_id", song_queue_id)]) - .send() - */ let client = reqwest::Client::new(); let endpoint = String::from("api/v2/service/login"); let api_url = format!("{}/{endpoint}", app.uri); @@ -125,8 +117,6 @@ mod auth { "passphrase": icarus_envy::environment::get_service_passphrase().await, }); - println!("Payload: {payload:?}"); - match client.post(api_url).json(&payload) .send().await { Ok(response) => match response.json::().await { @@ -145,14 +135,15 @@ mod auth { Err(std::io::Error::other(err.to_string())) } } - - // icarus_models::login_result::LoginResult::default() } // TODO: Implement function. 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 { - true + let current_time = time::OffsetDateTime::now_utc(); + let expire_time = time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap(); + current_time > expire_time + // true } // TODO: Implement function -- 2.43.0 From b5f7943938a113f96a03b16bd8f27a6fbf65eaf2 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 13 Aug 2025 17:55:30 -0400 Subject: [PATCH 09/17] tsk-41: Starting work to add auth to the requests --- src/api.rs | 21 ++++++++++++++++++++- src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/api.rs b/src/api.rs index 9a380ec..51e888d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,7 +2,16 @@ pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result (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) } pub mod parsing { @@ -143,3 +152,13 @@ pub mod service_token { } } } + +pub mod refresh_token { + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} diff --git a/src/main.rs b/src/main.rs index a70a0c0..5644458 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,11 +33,18 @@ async fn main() -> Result<(), Box> { println!("Base URL: {:?}", app.uri); println!("Token: {:?}", app.token); - tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; if auth::did_token_expire(&app.token).await { println!("Token did expire"); - app.token = auth::get_refresh_token(&app, &app.token).await; + app.token = match auth::get_refresh_token(&app, &app.token).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); @@ -46,14 +53,14 @@ async fn main() -> Result<(), Box> { } tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await; - // 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:?}"); + // TODO: For now, focus on making sure the token is valid before you get here + continue; let song_queue_id = song_queue_item.data[0].id; let user_id = song_queue_item.data[0].user_id; @@ -137,18 +144,40 @@ mod auth { } } - // TODO: Implement function. Might want to put the functionality within icarus_models - // at some point + // 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 - // true } - // TODO: Implement function - pub async fn get_refresh_token(app: &crate::config::App, login_result: &icarus_models::login_result::LoginResult) -> icarus_models::login_result::LoginResult { - icarus_models::login_result::LoginResult::default() + // 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.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(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())) + } + } } } -- 2.43.0 From 88ae596ae1ce31e0be9e92aa35cd15203bde019d Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 18:20:28 -0400 Subject: [PATCH 10/17] tsk-41: icarus_envy version bump --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f5c007..11b9077 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -516,8 +516,8 @@ dependencies = [ [[package]] name = "icarus_envy" -version = "0.3.1" -source = "git+ssh://git@git.kundeng.us/phoenix/icarus_envy.git?tag=v0.3.1-main-3cd42dab6b-006#3cd42dab6b2503609883f5f57ad3508755c34a2e" +version = "0.3.2" +source = "git+ssh://git@git.kundeng.us/phoenix/icarus_envy.git?tag=v0.3.2-18-d498f60046-006#d498f600468c7e5fcf03a640f8c5b9476765fe96" dependencies = [ "const_format", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 5f5a472..3bd9a4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ uuid = { version = "1.17.0", features = ["v4", "serde"] } rand = { version = "0.9.1" } icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.5.5-devel-bd793db08e-111" } -icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.1-main-3cd42dab6b-006" } +icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.2-18-d498f60046-006" } -- 2.43.0 From 7e9392ef1f687bd5f55e0f1356c1e7e6fda1d790 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 18:23:56 -0400 Subject: [PATCH 11/17] tsk-41: Updated env files --- .env.docker.sample | 1 + .env.sample | 1 + 2 files changed, 2 insertions(+) diff --git a/.env.docker.sample b/.env.docker.sample index 18a8806..141f691 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -1,4 +1,5 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 +ICARUS_AUTH_BASE_API_URL=http://localhost:3001 SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH diff --git a/.env.sample b/.env.sample index 18a8806..141f691 100644 --- a/.env.sample +++ b/.env.sample @@ -1,4 +1,5 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 +ICARUS_AUTH_BASE_API_URL=http://localhost:3001 SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH -- 2.43.0 From aa93e6cc4a95b08fbf948ec968be2531c10c9274 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 18:48:33 -0400 Subject: [PATCH 12/17] tsk-41: Added auth header to the appropriate request calls --- src/api.rs | 12 +++++++++--- src/config.rs | 1 + src/main.rs | 5 +++-- src/the_rest.rs | 12 ++++++++---- src/update_queued_song.rs | 3 ++- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/api.rs b/src/api.rs index 51e888d..ff479c6 100644 --- a/src/api.rs +++ b/src/api.rs @@ -3,7 +3,7 @@ pub async fn fetch_next_queue_item(app: &crate::config::App) -> Result 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() }; @@ -118,7 +119,7 @@ mod auth { 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.uri); + let api_url = format!("{}/{endpoint}", app.auth_uri); let payload = serde_json::json!({ "passphrase": icarus_envy::environment::get_service_passphrase().await, @@ -155,7 +156,7 @@ mod auth { 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.uri); + let api_url = format!("{}/{endpoint}", app.auth_uri); let payload = serde_json::json!({ "access_token": login_result.token diff --git a/src/the_rest.rs b/src/the_rest.rs index 3815239..40967bc 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -30,8 +30,9 @@ pub mod create_song { let client = reqwest::Client::builder().build()?; let url = format!("{}/api/v2/song", app.uri); + let (key, header) = crate::api::auth_header(app).await; - let request = client.post(url).json(&payload); + let request = client.post(url).json(&payload).header(key, header); request.send().await } @@ -55,7 +56,8 @@ pub mod create_coverart { let client = reqwest::Client::builder().build()?; let url = format!("{}/api/v2/coverart", app.uri); let payload = get_payload(song_id, coverart_queue_id); - let request = client.post(url).json(&payload); + let (key, header) = crate::api::auth_header(app).await; + let request = client.post(url).json(&payload).header(key, header); request.send().await } @@ -88,7 +90,8 @@ pub mod wipe_data { let payload = serde_json::json!({ "song_queue_id": song_queue_id }); - let request = client.patch(url).json(&payload); + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).json(&payload).header(key, header); request.send().await } @@ -112,7 +115,8 @@ pub mod wipe_data { let payload = serde_json::json!({ "coverart_queue_id": coverart_queue_id }); - let request = client.patch(url).json(&payload); + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).json(&payload).header(key, header); request.send().await } diff --git a/src/update_queued_song.rs b/src/update_queued_song.rs index 512fa74..b1a2394 100644 --- a/src/update_queued_song.rs +++ b/src/update_queued_song.rs @@ -18,7 +18,8 @@ pub async fn update_queued_song( let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri); println!("Url: {url:?}"); - let request = client.patch(url).multipart(form); + let (key, header) = crate::api::auth_header(app).await; + let request = client.patch(url).multipart(form).header(key, header); let response = request.send().await?; -- 2.43.0 From b8bbd66fd3ea4a29e759f8f45fd642d2e476c0df Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 18:57:28 -0400 Subject: [PATCH 13/17] tsk-41: Service now works --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 05ac867..6912de9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,8 +60,6 @@ async fn main() -> Result<(), Box> { if !empty { println!("Queue is not empty"); println!("SongQueueItem: {song_queue_item:?}"); - // TODO: For now, focus on making sure the token is valid before you get here - continue; let song_queue_id = song_queue_item.data[0].id; let user_id = song_queue_item.data[0].user_id; -- 2.43.0 From 67d2729c464643465a3220d28690fc29ad41e28a Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 19:04:34 -0400 Subject: [PATCH 14/17] 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, -- 2.43.0 From 99cb6318d6229cd5a3b3f1f8e41e62469c567c52 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 19:06:25 -0400 Subject: [PATCH 15/17] tsk-41: Removing unused env variable --- .env.docker.sample | 1 - .env.sample | 1 - 2 files changed, 2 deletions(-) diff --git a/.env.docker.sample b/.env.docker.sample index 141f691..f116df7 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -1,5 +1,4 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 ICARUS_AUTH_BASE_API_URL=http://localhost:3001 -SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH diff --git a/.env.sample b/.env.sample index 141f691..f116df7 100644 --- a/.env.sample +++ b/.env.sample @@ -1,5 +1,4 @@ ROOT_DIRECTORY=/home/songparser/mydata ICARUS_BASE_API_URL=http://localhost:3000 ICARUS_AUTH_BASE_API_URL=http://localhost:3001 -SECRET_KEY=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH -- 2.43.0 From af67d5bea04686fe354fe937ca01a893ef589dd0 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 19:08:24 -0400 Subject: [PATCH 16/17] tsk-41: Lingering cleanup --- src/api.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/api.rs b/src/api.rs index 4b7a8c7..ff23283 100644 --- a/src/api.rs +++ b/src/api.rs @@ -39,7 +39,6 @@ pub mod parsing { pub mod fetch_song_queue_data { pub async fn get_data( - // base_url: &String, app: &crate::config::App, id: &uuid::Uuid, ) -> Result { @@ -53,7 +52,6 @@ pub mod fetch_song_queue_data { pub mod get_metadata_queue { pub async fn get( - // base_url: &String, app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { @@ -107,7 +105,6 @@ pub mod get_metadata_queue { pub mod get_coverart_queue { pub async fn get( - // base_url: &String, app: &crate::config::App, song_queue_id: &uuid::Uuid, ) -> Result { @@ -124,7 +121,6 @@ pub mod get_coverart_queue { } pub async fn get_data( - // base_url: &String, app: &crate::config::App, coverart_queue_id: &uuid::Uuid, ) -> Result { -- 2.43.0 From 0b8ae56eb47b89ff70af6a1189446dccff766cf0 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 14 Aug 2025 19:08:35 -0400 Subject: [PATCH 17/17] tsk-41: Version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11b9077..af8391a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1318,7 +1318,7 @@ dependencies = [ [[package]] name = "songparser" -version = "0.2.4" +version = "0.2.5" dependencies = [ "futures", "icarus_envy", diff --git a/Cargo.toml b/Cargo.toml index 3bd9a4a..3d91bd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "songparser" -version = "0.2.4" +version = "0.2.5" edition = "2024" rust-version = "1.88" -- 2.43.0