From 219b95bb2f18c248d4addc87e1a4979237472729 Mon Sep 17 00:00:00 2001 From: KD Date: Sun, 30 Mar 2025 16:00:06 -0400 Subject: [PATCH] Updated icarus_models (#44) * Updated icarus_models * Constants and url changes * Refactoring * Removed functions * Fixing tests --- Cargo.toml | 2 +- src/main.rs | 4 +-- src/managers/commit_manager.rs | 20 ++++++++----- src/syncers/common.rs | 32 ++++++++++++++++++++ src/syncers/delete.rs | 43 +++++++++----------------- src/syncers/download.rs | 38 ++++++----------------- src/syncers/mod.rs | 1 + src/syncers/retrieve_records.rs | 53 +++++++++++---------------------- src/syncers/upload.rs | 25 +++------------- 9 files changed, 93 insertions(+), 125 deletions(-) create mode 100644 src/syncers/common.rs diff --git a/Cargo.toml b/Cargo.toml index 7dffb53..703b059 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.140" tokio = { version = "1.44.1", features = ["full"] } tokio-util = { version = "0.7.14", features = ["codec"] } -icarus-models = { git = "ssh://git@git.kundeng.us/phoenix/icarus-models.git", tag = "v0.1.14" } +icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.2.0" } diff --git a/src/main.rs b/src/main.rs index 69039e8..c47d149 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,11 +52,11 @@ mod tests { let filename: String = if track < 10 { String::from("track0") + &track.to_string() - + icarus_models::constants::DEFAULTMUSICEXTENSION + + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION } else { String::from("track") + &track.to_string() - + icarus_models::constants::DEFAULTMUSICEXTENSION + + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION }; let disc = 1; match managers::commit_manager::retrieve_song( diff --git a/src/managers/commit_manager.rs b/src/managers/commit_manager.rs index 0afa123..b464977 100644 --- a/src/managers/commit_manager.rs +++ b/src/managers/commit_manager.rs @@ -52,7 +52,9 @@ pub fn retrieve_song( song.album = album.title.clone(); song.album_artist = album.artist.clone(); song.artist = track.artist.clone(); - song.audio_type = String::from(icarus_models::constants::DEFAULTMUSICEXTENSION); + song.audio_type = String::from( + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION, + ); song.disc = track.disc.clone(); song.disc_count = album.disc_count.clone(); song.duration = track.duration as i32; @@ -192,8 +194,8 @@ impl CommitManager { match Runtime::new().unwrap().block_on(result_fut) { Ok(o) => { println!("Success"); - let filename = - String::from("audio") + icarus_models::constants::DEFAULTMUSICEXTENSION; + let filename = String::from("audio") + + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION; let data = o.as_bytes(); let mut file = std::fs::File::create(filename).expect("Failed to save"); file.write_all(&data) @@ -420,11 +422,11 @@ impl CommitManager { let filename = if track.track < 10 { "track0".to_owned() + &track.track.to_string() - + icarus_models::constants::DEFAULTMUSICEXTENSION + + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION } else { "track".to_owned() + &track.track.to_string() - + icarus_models::constants::DEFAULTMUSICEXTENSION + + icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION }; songs.push(icarus_models::song::Song { @@ -540,13 +542,15 @@ impl CommitManager { index += 1; } - if extension == icarus_models::constants::WAVEXTENSION[1..] - || extension == icarus_models::constants::FLACEXTENSION[1..] + if extension == icarus_models::constants::file_extensions::audio::WAVEXTENSION[1..] + || extension + == icarus_models::constants::file_extensions::audio::FLACEXTENSION[1..] { return En::SongFile; } else if extension == "json" { return En::MetadataFile; - } else if extension == icarus_models::constants::JPGEXTENSION[1..] + } else if extension + == icarus_models::constants::file_extensions::image::JPGEXTENSION[1..] || extension == "jpeg" || extension == "png" { diff --git a/src/syncers/common.rs b/src/syncers/common.rs new file mode 100644 index 0000000..9fde0dd --- /dev/null +++ b/src/syncers/common.rs @@ -0,0 +1,32 @@ +use crate::models; + +pub fn retrieve_url(api: &models::api::API, with_id: bool, id: i32) -> String { + if with_id { + retrieve_url_with_id(&api, id) + } else { + retrieve_url_reg(&api) + } +} + +fn retrieve_url_reg(api: &models::api::API) -> String { + let mut url: String = String::from(&api.url); + url += &String::from("api/"); + url += &String::from(&api.version); + url += &String::from("/"); + url += &String::from(&api.endpoint); + url += &String::from("/"); + + return url; +} + +fn retrieve_url_with_id(api: &models::api::API, id: i32) -> String { + let mut url: String = String::from(&api.url); + url += &String::from("api/"); + url += &String::from(&api.version); + url += &String::from("/"); + url += &String::from(&api.endpoint); + url += &String::from("/"); + url += &id.to_string(); + + return url; +} diff --git a/src/syncers/delete.rs b/src/syncers/delete.rs index 7370b49..f72561b 100644 --- a/src/syncers/delete.rs +++ b/src/syncers/delete.rs @@ -3,6 +3,7 @@ use std::default::Default; use reqwest; use crate::models; +use crate::syncers; #[derive(Clone, Debug)] pub struct Delete { @@ -24,7 +25,7 @@ impl Delete { song: &icarus_models::song::Song, ) -> Result { self.api.endpoint = "song/data/delete".to_owned(); - let url = self.retrieve_url(&song); + let url = syncers::common::retrieve_url(&self.api, true, song.id); let client = reqwest::Client::builder().build().unwrap(); let access_token = token.bearer_token(); let response = client @@ -33,39 +34,23 @@ impl Delete { .send() .await .unwrap(); - let mut sng = icarus_models::song::Song::default(); match response.status() { reqwest::StatusCode::OK => { println!("Success!"); - let s = response.json::().await; - match s { - Ok(parsed) => { - sng = parsed; - } - Err(er) => { - println!("Error {:?}", er); - } - }; - } - other => { - panic!("Issue occurred: {:?}", other); + + match response.json::().await { + Ok(sng) => Ok(sng), + Err(er) => Err(std::io::Error::new( + std::io::ErrorKind::Other, + er.to_string(), + )), + } } + other => Err(std::io::Error::new( + std::io::ErrorKind::Other, + other.to_string(), + )), } - - return Ok(sng); - } - - fn retrieve_url(&self, song: &icarus_models::song::Song) -> String { - let api = &self.api; - let mut url: String = String::from(&api.url); - url += &String::from("api/"); - url += &String::from(&api.version); - url += &String::from("/"); - url += &String::from(&api.endpoint); - url += &String::from("/"); - url += &song.id.to_string(); - - return url; } } diff --git a/src/syncers/download.rs b/src/syncers/download.rs index 8b915b8..a3a4629 100644 --- a/src/syncers/download.rs +++ b/src/syncers/download.rs @@ -1,6 +1,7 @@ use std::default::Default; use crate::models; +use crate::syncers; pub struct Download { pub api: models::api::API, @@ -27,23 +28,17 @@ impl Download { song: &icarus_models::song::Song, ) -> Result { self.api.endpoint = String::from("song/data/download"); - let url = self.retrieve_url(&song); + let url = syncers::common::retrieve_url(&self.api, true, song.id); let access_token = token.bearer_token(); - let mut headers = reqwest::header::HeaderMap::new(); - headers.insert( - reqwest::header::AUTHORIZATION, - http::header::HeaderValue::from_str(&access_token.clone()).unwrap(), - ); - let client = reqwest::Client::builder().build().unwrap(); - let response = client + + match client .get(&url) .header(reqwest::header::AUTHORIZATION, &access_token) .send() - .await; - - match response { + .await + { Ok(rep) => match rep.status() { reqwest::StatusCode::OK => { let data = rep.text(); @@ -52,35 +47,20 @@ impl Download { return Ok(e); } Err(er) => { - println!("Error {:?}", er); + return Err(MyError::Other(er.to_string())); } } } reqwest::StatusCode::UNAUTHORIZED => { - println!("Need to grab a new token"); + return Err(MyError::Other(String::from("Need to grab a new token"))); } other => { - panic!("Uh oh! Something unexpected happened: {:?}", other); + return Err(MyError::Other(other.to_string())); } }, Err(er) => { return Err(MyError::Request(er)); } } - - return Err(MyError::Other(String::from("Error downloading"))); - } - - fn retrieve_url(&self, song: &icarus_models::song::Song) -> String { - let api = &self.api; - let mut url: String = String::from(&api.url); - url += &String::from("api/"); - url += &String::from(&api.version); - url += &String::from("/"); - url += &String::from(&api.endpoint); - url += &String::from("/"); - url += &song.id.to_string(); - - return url; } } diff --git a/src/syncers/mod.rs b/src/syncers/mod.rs index cb8e326..af770b2 100644 --- a/src/syncers/mod.rs +++ b/src/syncers/mod.rs @@ -1,3 +1,4 @@ +pub mod common; pub mod delete; pub mod download; pub mod retrieve_records; diff --git a/src/syncers/retrieve_records.rs b/src/syncers/retrieve_records.rs index d2e397f..84df6eb 100644 --- a/src/syncers/retrieve_records.rs +++ b/src/syncers/retrieve_records.rs @@ -2,6 +2,7 @@ use std::default::Default; use std::io::Error; use crate::models; +use crate::syncers; pub struct RetrieveRecords { pub api: models::api::API, @@ -21,20 +22,9 @@ impl RetrieveRecords { token: &icarus_models::token::AccessToken, ) -> Result, Error> { self.api.endpoint = String::from("song"); - let mut songs: Vec = Vec::new(); - let url = self.retrieve_url(); + let url = syncers::common::retrieve_url(&self.api, false, 0); let access_token = token.bearer_token(); - let mut headers = reqwest::header::HeaderMap::new(); - headers.insert( - reqwest::header::AUTHORIZATION, - http::header::HeaderValue::from_str(&access_token.clone()).unwrap(), - ); - headers.insert( - reqwest::header::CONTENT_TYPE, - http::header::HeaderValue::from_static("application/json"), - ); - let client = reqwest::Client::builder().build().unwrap(); let response = client .get(&url) @@ -46,35 +36,28 @@ impl RetrieveRecords { match response.status() { reqwest::StatusCode::OK => { // on success, parse our JSON to an APIResponse - let s = response.json::>().await; - match s { - // - Ok(parsed) => { - songs = parsed; + match response.json::>().await { + Ok(parsed) => Ok(parsed), + Err(err) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + err.to_string(), + )); } - Err(_) => println!("Hm, the response didn't match the shape we expected."), - }; + } } reqwest::StatusCode::UNAUTHORIZED => { - println!("Need to grab a new token"); + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Need to grab a new token", + )); } other => { - panic!("Uh oh! Something unexpected happened: {:?}", other); + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + other.to_string(), + )); } } - - return Ok(songs); - } - - fn retrieve_url(&self) -> String { - let api = &self.api; - let mut url: String = String::from(&api.url); - url += &String::from("api/"); - url += &String::from(&api.version); - url += &String::from("/"); - url += &String::from(&api.endpoint); - url += &String::from("/"); - - return url; } } diff --git a/src/syncers/upload.rs b/src/syncers/upload.rs index a7666d2..f17a6ef 100644 --- a/src/syncers/upload.rs +++ b/src/syncers/upload.rs @@ -4,6 +4,7 @@ use http::HeaderValue; use reqwest; use crate::models; +use crate::syncers; pub struct Upload { pub api: models::api::API, @@ -25,7 +26,7 @@ impl Upload { cover: &icarus_models::coverart::CoverArt, ) -> Result { self.api.endpoint = String::from("song/data/upload/with/data"); - let url = self.retrieve_url(); + let url = syncers::common::retrieve_url(&self.api, false, 0); let access_token = token.bearer_token(); if url.is_empty() { @@ -80,9 +81,9 @@ impl Upload { println!("\n{}\n", song_detail); let mut song_filename = String::from("audio"); - song_filename += icarus_models::constants::DEFAULTMUSICEXTENSION; + song_filename += icarus_models::constants::file_extensions::audio::DEFAULTMUSICEXTENSION; let mut cover_filename = String::from("cover"); - cover_filename += icarus_models::constants::JPGEXTENSION; + cover_filename += icarus_models::constants::file_extensions::image::JPGEXTENSION; let form = reqwest::multipart::Form::new() .part( @@ -106,22 +107,4 @@ impl Upload { api.version = String::from("v1"); self.api = api; } - - fn retrieve_url(&self) -> String { - let api = &self.api; - let mut buffer = api.url.clone(); - let count = buffer.len(); - - if buffer.chars().nth(count - 1) != Some('/') { - buffer += "/"; - } - - let mut url: String = String::from(&buffer); - url += &String::from("api/"); - url += &String::from(&api.version); - url += &String::from("/"); - url += &String::from(&api.endpoint); - - return url; - } }