From c9732ccb3d0ba2f44db833c8bdc12db00f50d7df Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 13 Jul 2025 19:35:06 -0400 Subject: [PATCH 1/9] Moved responses module --- src/main.rs | 157 +---------------------------------------------- src/responses.rs | 16 +++++ 2 files changed, 18 insertions(+), 155 deletions(-) create mode 100644 src/responses.rs diff --git a/src/main.rs b/src/main.rs index 8532b50..376d232 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +pub mod api; +pub mod responses; pub mod the_rest; pub mod update_queued_song; pub mod util; @@ -375,158 +377,3 @@ pub async fn apply_metadata( Err(err) => Err(err), } } - -mod responses { - pub mod fetch_next_queue_item { - use serde::{Deserialize, Serialize}; - - #[derive(Debug, Deserialize, Serialize)] - pub struct QueueItem { - pub id: uuid::Uuid, - pub filename: String, - pub status: String, - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct SongQueueItem { - pub message: String, - pub data: Vec, - } - } -} - -mod api { - pub async fn fetch_next_queue_item( - base_url: &String, - ) -> Result { - let client = reqwest::Client::new(); - let fetch_endpoint = String::from("api/v2/song/queue/next"); - let api_url = format!("{base_url}/{fetch_endpoint}"); - client.get(api_url).send().await - } - - pub mod parsing { - use futures::StreamExt; - - pub async fn parse_response_into_bytes( - response: reqwest::Response, - ) -> Result, reqwest::Error> { - // TODO: At some point, handle the flow if the size is small or - // large - let mut byte_stream = response.bytes_stream(); - let mut all_bytes = Vec::new(); - - while let Some(chunk) = byte_stream.next().await { - let chunk = chunk?; - all_bytes.extend_from_slice(&chunk); - } - - Ok(all_bytes) - } - } - - pub mod fetch_song_queue_data { - pub async fn get_data( - base_url: &String, - 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}"); - client.get(api_url).send().await - } - } - - pub mod get_metadata_queue { - pub async fn get( - base_url: &String, - 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}"); - client - .get(api_url) - .query(&[("song_queue_id", song_queue_id)]) - .send() - .await - } - - pub mod response { - use serde::{Deserialize, Serialize}; - - #[derive(Clone, Debug, Deserialize, Serialize)] - pub struct Metadata { - pub song_queue_id: uuid::Uuid, - pub album: String, - pub album_artist: String, - pub artist: String, - pub disc: i32, - pub disc_count: i32, - pub duration: i64, - pub genre: String, - pub title: String, - pub track: i32, - pub track_count: i32, - pub year: i32, - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct QueueItem { - pub id: uuid::Uuid, - pub metadata: Metadata, - #[serde(with = "time::serde::rfc3339")] - pub created_at: time::OffsetDateTime, - pub song_queue_id: uuid::Uuid, - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - } - - pub mod get_coverart_queue { - pub async fn get( - base_url: &String, - 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}"); - client - .get(api_url) - .query(&[("song_queue_id", song_queue_id)]) - .send() - .await - } - - pub async fn get_data( - base_url: &String, - 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}"); - client.get(api_url).send().await - } - - pub mod response { - use serde::{Deserialize, Serialize}; - - #[derive(Debug, Deserialize, Serialize)] - pub struct CoverArtQueue { - pub id: uuid::Uuid, - pub song_queue_id: uuid::Uuid, - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, - } - } - } -} diff --git a/src/responses.rs b/src/responses.rs new file mode 100644 index 0000000..c5a090e --- /dev/null +++ b/src/responses.rs @@ -0,0 +1,16 @@ +pub mod fetch_next_queue_item { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Deserialize, Serialize)] + pub struct QueueItem { + pub id: uuid::Uuid, + pub filename: String, + pub status: String, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct SongQueueItem { + pub message: String, + pub data: Vec, + } +} -- 2.43.0 From 13d8f808fa2b0d47aa3209a9008835512f81c8dd Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 13 Jul 2025 19:35:17 -0400 Subject: [PATCH 2/9] Moved api module --- src/api.rs | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/api.rs diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000..5a2e330 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,134 @@ +pub async fn fetch_next_queue_item( + base_url: &String, +) -> Result { + let client = reqwest::Client::new(); + let fetch_endpoint = String::from("api/v2/song/queue/next"); + let api_url = format!("{base_url}/{fetch_endpoint}"); + client.get(api_url).send().await +} + +pub mod parsing { + use futures::StreamExt; + + pub async fn parse_response_into_bytes( + response: reqwest::Response, + ) -> Result, reqwest::Error> { + // TODO: At some point, handle the flow if the size is small or + // large + let mut byte_stream = response.bytes_stream(); + let mut all_bytes = Vec::new(); + + while let Some(chunk) = byte_stream.next().await { + let chunk = chunk?; + all_bytes.extend_from_slice(&chunk); + } + + Ok(all_bytes) + } +} + +pub mod fetch_song_queue_data { + pub async fn get_data( + base_url: &String, + 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}"); + client.get(api_url).send().await + } +} + +pub mod get_metadata_queue { + pub async fn get( + base_url: &String, + 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}"); + client + .get(api_url) + .query(&[("song_queue_id", song_queue_id)]) + .send() + .await + } + + pub mod response { + use serde::{Deserialize, Serialize}; + + #[derive(Clone, Debug, Deserialize, Serialize)] + pub struct Metadata { + pub song_queue_id: uuid::Uuid, + pub album: String, + pub album_artist: String, + pub artist: String, + pub disc: i32, + pub disc_count: i32, + pub duration: i64, + pub genre: String, + pub title: String, + pub track: i32, + pub track_count: i32, + pub year: i32, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct QueueItem { + pub id: uuid::Uuid, + pub metadata: Metadata, + #[serde(with = "time::serde::rfc3339")] + pub created_at: time::OffsetDateTime, + pub song_queue_id: uuid::Uuid, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + +pub mod get_coverart_queue { + pub async fn get( + base_url: &String, + 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}"); + client + .get(api_url) + .query(&[("song_queue_id", song_queue_id)]) + .send() + .await + } + + pub async fn get_data( + base_url: &String, + 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}"); + client.get(api_url).send().await + } + + pub mod response { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Deserialize, Serialize)] + pub struct CoverArtQueue { + pub id: uuid::Uuid, + pub song_queue_id: uuid::Uuid, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } +} + -- 2.43.0 From b16102de4cd991c189d65ce23f61c4cc7415334d Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 13 Jul 2025 19:35:53 -0400 Subject: [PATCH 3/9] Added early create song code --- src/the_rest.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/the_rest.rs b/src/the_rest.rs index ae021fd..fae5311 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -1,6 +1,59 @@ // TODO: Refactor this file when this app is functional // TODO: Create song +pub async fn create_song(base_url: &String, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, song_type: &String) -> Result { + let payload = serde_json::json!( + { + "album": &metadata_queue.album, + "album_artist": &metadata_queue.album_artist, + "artist": &metadata_queue.artist, + "disc": metadata_queue.disc, + "disc_count": metadata_queue.disc_count, + "duration": metadata_queue.duration, + "genre": &metadata_queue.genre, + "title": &metadata_queue.title, + "track": metadata_queue.track, + "track_count": metadata_queue.track_count, + "year": metadata_queue.year, + "audio_type": &song_type, + "user_id": &user_id, + "song_queue_id": &metadata_queue.song_queue_id, + } + ); + + let client = reqwest::Client::builder().build()?; + + let url = format!("{base_url}/api/v2/song"); + + let request = client.post(url).json(&payload); + let response = request.send().await?; + + Ok(response) +} + +pub mod response { +} + + /* + * + * { + "album": "Are You Experienced?", + "album_artist": "The Jimi Hendrix Experience", + "artist": "The Jimi Hendrix Experience", + "disc": 1, + "disc_count": 1, + "duration": 213, + "genre": "Psychadelic Rock", + "title": "Hey Joe", + "track": 1, + "track_count": 17, + "year": 1967, + "audio_type": "flac", + "user_id": "{{user_id}}", + "song_queue_id": "{{queued_song_id}}" +} + * + */ // TODO: Create coverart // TODO: Wipe data from queued song // TODO: Wipe data from queued coverart -- 2.43.0 From eed7b329a36498299a6f0c23b14708ea791bc92a Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 13 Jul 2025 20:02:46 -0400 Subject: [PATCH 4/9] Added function call and TODOs --- src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 376d232..36c5a46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -88,7 +88,20 @@ async fn some_work( { Ok(_inner_response) => { println!("Response: {_inner_response:?}"); - Ok(()) + + // TODO: Do not hard code this. Check if one of the existing + // endpoints already have the user_id + let user_id = uuid::Uuid::new_v4(); + // TODO: Place this somewhere else + let song_type = String::from("flac"); + match the_rest::create_song(app_base_url, &metadata, &user_id, &song_type).await { + Ok(_) => { + Ok(()) + } + Err(err) => { + Err(std::io::Error::other(err.to_string())) + } + } } Err(err) => Err(std::io::Error::other(err.to_string())), } -- 2.43.0 From 66900d64d6bf666b59c20a4c58642600fc3f4a68 Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 14 Jul 2025 20:43:02 -0400 Subject: [PATCH 5/9] Fixed function to create song and added Response --- src/the_rest.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/the_rest.rs b/src/the_rest.rs index fae5311..b55ba1e 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -1,7 +1,8 @@ // TODO: Refactor this file when this app is functional // TODO: Create song -pub async fn create_song(base_url: &String, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, song_type: &String) -> Result { +pub mod create_song { +pub async fn create(base_url: &String, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, song_type: &String) -> Result { let payload = serde_json::json!( { "album": &metadata_queue.album, @@ -14,7 +15,7 @@ pub async fn create_song(base_url: &String, metadata_queue: &crate::api::get_met "title": &metadata_queue.title, "track": metadata_queue.track, "track_count": metadata_queue.track_count, - "year": metadata_queue.year, + "date": metadata_queue.year.to_string(), "audio_type": &song_type, "user_id": &user_id, "song_queue_id": &metadata_queue.song_queue_id, @@ -26,12 +27,16 @@ pub async fn create_song(base_url: &String, metadata_queue: &crate::api::get_met let url = format!("{base_url}/api/v2/song"); let request = client.post(url).json(&payload); - let response = request.send().await?; - - Ok(response) + request.send().await } pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } +} } /* -- 2.43.0 From 3d7f60c24fec5ab3d233d1f0b321c139e2beea04 Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 14 Jul 2025 20:43:37 -0400 Subject: [PATCH 6/9] Added functionality to create song and made changes to prep_song() --- src/main.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 36c5a46..6e82e3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,7 +71,7 @@ async fn some_work( song_queue_id: &uuid::Uuid, ) -> Result<(), std::io::Error> { match prep_song(app_base_url, song_queue_id).await { - Ok((song_queue_path, coverart_queue_path, metadata)) => { + 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( @@ -94,9 +94,20 @@ async fn some_work( let user_id = uuid::Uuid::new_v4(); // TODO: Place this somewhere else let song_type = String::from("flac"); - match the_rest::create_song(app_base_url, &metadata, &user_id, &song_type).await { - Ok(_) => { - Ok(()) + // Err(std::io::Error::other(err.to_string())) + match the_rest::create_song::create(app_base_url, &metadata, &user_id, &song_type).await { + Ok(response) => match response.json::().await { + Ok(resp) => { + println!("Response: {resp:?}"); + + let song = &resp.data[0]; + println!("Song id: {:?}", song.id); + // println!("Response json: {:?}", response.text().await); + Ok(()) + } + Err(err) => { + Err(std::io::Error::other(err.to_string())) + } } Err(err) => { Err(std::io::Error::other(err.to_string())) @@ -119,7 +130,7 @@ async fn some_work( async fn prep_song( api_url: &String, song_queue_id: &uuid::Uuid, -) -> Result<(String, String, api::get_metadata_queue::response::Metadata), reqwest::Error> { +) -> Result<(String, String, api::get_metadata_queue::response::Metadata, uuid::Uuid), reqwest::Error> { match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await { Ok(response) => { // Process data here... @@ -163,7 +174,7 @@ async fn prep_song( let c_path = util::path_buf_to_string(&coverart_queue_path); let s_path = util::path_buf_to_string(&song_queue_path); - Ok((s_path, c_path, metadata.clone())) + Ok((s_path, c_path, metadata.clone(), *coverart_queue_id)) } Err(err) => { Err(err) -- 2.43.0 From 4012678a0c271e91464d0ec599ad9fb43794929d Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 14 Jul 2025 20:48:13 -0400 Subject: [PATCH 7/9] Removing commented json --- src/the_rest.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/the_rest.rs b/src/the_rest.rs index b55ba1e..3cc35f0 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -39,26 +39,6 @@ pub mod response { } } - /* - * - * { - "album": "Are You Experienced?", - "album_artist": "The Jimi Hendrix Experience", - "artist": "The Jimi Hendrix Experience", - "disc": 1, - "disc_count": 1, - "duration": 213, - "genre": "Psychadelic Rock", - "title": "Hey Joe", - "track": 1, - "track_count": 17, - "year": 1967, - "audio_type": "flac", - "user_id": "{{user_id}}", - "song_queue_id": "{{queued_song_id}}" -} - * - */ // TODO: Create coverart // TODO: Wipe data from queued song // TODO: Wipe data from queued coverart -- 2.43.0 From d0fad97d892a445773c8cbd6c5e974bec1fca8ce Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 14 Jul 2025 20:48:31 -0400 Subject: [PATCH 8/9] Adding some code that will be used later on --- src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6e82e3f..c4b1da7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,13 @@ async fn some_work( println!("Response: {resp:?}"); let song = &resp.data[0]; - println!("Song id: {:?}", song.id); + let url = format!("{app_base_url}/api/v2/coverart"); + let payload = serde_json::json!({ + "song_id": &song.id, + "coverart_queue_id": &coverart_queue_id, + }); + println!("Payload: {payload:?}"); + println!("Url: {url:?}"); // println!("Response json: {:?}", response.text().await); Ok(()) } -- 2.43.0 From efc73518d776ea35c2c365a769c0ba1f711e30ba Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 14 Jul 2025 20:48:53 -0400 Subject: [PATCH 9/9] Code formatting --- src/api.rs | 5 +---- src/main.rs | 36 +++++++++++++++++++++++++----------- src/the_rest.rs | 33 +++++++++++++++++++-------------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/api.rs b/src/api.rs index 5a2e330..f4d0933 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,6 +1,4 @@ -pub async fn fetch_next_queue_item( - base_url: &String, -) -> Result { +pub async fn fetch_next_queue_item(base_url: &String) -> Result { let client = reqwest::Client::new(); let fetch_endpoint = String::from("api/v2/song/queue/next"); let api_url = format!("{base_url}/{fetch_endpoint}"); @@ -131,4 +129,3 @@ pub mod get_coverart_queue { } } } - diff --git a/src/main.rs b/src/main.rs index c4b1da7..7bb1141 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,9 +94,19 @@ async fn some_work( let user_id = uuid::Uuid::new_v4(); // TODO: Place this somewhere else let song_type = String::from("flac"); - // Err(std::io::Error::other(err.to_string())) - match the_rest::create_song::create(app_base_url, &metadata, &user_id, &song_type).await { - Ok(response) => match response.json::().await { + // Err(std::io::Error::other(err.to_string())) + match the_rest::create_song::create( + app_base_url, + &metadata, + &user_id, + &song_type, + ) + .await + { + Ok(response) => match response + .json::() + .await + { Ok(resp) => { println!("Response: {resp:?}"); @@ -111,13 +121,9 @@ async fn some_work( // println!("Response json: {:?}", response.text().await); 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())), @@ -136,7 +142,15 @@ async fn some_work( async fn prep_song( api_url: &String, song_queue_id: &uuid::Uuid, -) -> Result<(String, String, api::get_metadata_queue::response::Metadata, uuid::Uuid), reqwest::Error> { +) -> Result< + ( + String, + String, + api::get_metadata_queue::response::Metadata, + uuid::Uuid, + ), + reqwest::Error, +> { match api::fetch_song_queue_data::get_data(api_url, song_queue_id).await { Ok(response) => { // Process data here... diff --git a/src/the_rest.rs b/src/the_rest.rs index 3cc35f0..8afffe7 100644 --- a/src/the_rest.rs +++ b/src/the_rest.rs @@ -2,8 +2,13 @@ // TODO: Create song pub mod create_song { -pub async fn create(base_url: &String, metadata_queue: &crate::api::get_metadata_queue::response::Metadata, user_id: &uuid::Uuid, song_type: &String) -> Result { - let payload = serde_json::json!( + pub async fn create( + base_url: &String, + metadata_queue: &crate::api::get_metadata_queue::response::Metadata, + user_id: &uuid::Uuid, + song_type: &String, + ) -> Result { + let payload = serde_json::json!( { "album": &metadata_queue.album, "album_artist": &metadata_queue.album_artist, @@ -22,21 +27,21 @@ pub async fn create(base_url: &String, metadata_queue: &crate::api::get_metadata } ); - let client = reqwest::Client::builder().build()?; + let client = reqwest::Client::builder().build()?; - let url = format!("{base_url}/api/v2/song"); + let url = format!("{base_url}/api/v2/song"); - let request = client.post(url).json(&payload); - request.send().await -} - -pub mod response { - #[derive(Debug, serde::Deserialize, serde::Serialize)] - pub struct Response { - pub message: String, - pub data: Vec, + let request = client.post(url).json(&payload); + request.send().await + } + + pub mod response { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } } -} } // TODO: Create coverart -- 2.43.0