test getRandomSongs endpoint

This commit is contained in:
Erica Z 2024-11-01 09:43:55 +01:00
parent 2a87d6d9dc
commit b43a8ac4d0
3 changed files with 38 additions and 1 deletions

View file

@ -62,6 +62,7 @@ fn main() -> glib::ExitCode {
)
.unwrap();
client.ping().await.unwrap();
println!("{:#?}", client.get_random_songs(10).await.unwrap());
}
});

View file

@ -92,7 +92,7 @@ impl Client {
async fn get<T: serde::de::DeserializeOwned>(
&self,
path: &[&str],
query: &[&str],
query: &[(&str, &str)],
) -> Result<T, Error> {
let mut url = self.base_url.clone();
url.path_segments_mut()
@ -115,4 +115,13 @@ impl Client {
pub async fn ping(&self) -> Result<(), Error> {
self.get(&["rest", "ping"], &[]).await
}
pub async fn get_random_songs(&self, size: u32) -> Result<Vec<schema::Child>, Error> {
self.get::<schema::RandomSongsOuter>(
&["rest", "getRandomSongs"],
&[("size", &size.to_string())],
)
.await
.map(|response| response.random_songs.song)
}
}

View file

@ -23,3 +23,30 @@ pub struct Error {
pub code: u32,
pub message: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RandomSongsOuter {
pub random_songs: RandomSongs,
}
#[derive(Debug, Deserialize)]
pub struct RandomSongs {
pub song: Vec<Child>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Child {
pub id: String,
pub title: String,
pub album: String,
pub artist: String,
pub track: u32,
pub year: u32,
pub starred: Option<()>,
pub duration: u64,
pub play_count: Option<u32>,
pub genre: Option<String>,
pub cover_art: String,
}