diff --git a/src/bin/test.rs b/src/bin/test.rs index 49cdae9..4a991c0 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -7,6 +7,8 @@ async fn main() -> Result<(), Box> { let config = api.get_server_config().await?; println!("{:#?}", config); let countries = api.get_countries().await?; - println!("{:?}", countries); + println!("{:#?}", countries); + let stations = api.search().name("jazz").send().await?; + println!("{:#?}", stations); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 692c7cf..1121cef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ use reqwest; use serde::de::DeserializeOwned; use serde::Deserialize; +use std::collections::HashMap; use std::error::Error; use rand::seq::SliceRandom; @@ -14,7 +15,7 @@ use chrono::DateTime; use chrono::Utc; #[derive(PartialEq, Deserialize, Debug)] -pub struct Station { +pub struct ApiStation { pub changeuuid: String, pub stationuuid: String, pub serveruuid: Option, @@ -102,6 +103,40 @@ pub struct ApiConfig { pub language_to_code_filepath: String, } +#[derive(Clone, Debug)] +pub struct SearchBuilder { + map: HashMap, + api: RadioBrowserAPI, +} + +impl SearchBuilder { + pub fn new(api: RadioBrowserAPI) -> Self { + SearchBuilder { + api, + map: HashMap::new(), + } + } + + pub fn name>(mut self, name: P) -> Self { + self.map + .insert(String::from("name"), name.as_ref().to_string()); + self + } + + pub fn countrycode>(mut self, countrycode: P) -> Self { + self.map.insert( + String::from("countrycode"), + countrycode.as_ref().to_string(), + ); + self + } + + pub async fn send(mut self) -> Result, Box> { + Ok(self.api.send("/json/stations/search", self.map).await?) + } +} + +#[derive(Clone, Debug)] pub struct RadioBrowserAPI { servers: Vec, current: usize, @@ -124,22 +159,16 @@ impl RadioBrowserAPI { } } - pub async fn post_api>( + pub async fn post_api>( &mut self, - endpoint: O, + endpoint: A, ) -> Result> { - let client = reqwest::Client::new(); - let res = client - .post(format!( - "{}{}", - self.get_current_server(), - endpoint.as_ref() - )) - .send() - .await? - .json::

() - .await?; - Ok(res) + let mapjson = HashMap::new(); + post_api(self.get_current_server(), endpoint.as_ref(), mapjson).await + } + + pub fn search(&self) -> SearchBuilder { + SearchBuilder::new(self.clone()) } pub async fn get_server_config(&mut self) -> Result> { @@ -150,6 +179,14 @@ impl RadioBrowserAPI { Ok(self.post_api("/json/countries").await?) } + pub async fn send, Q: DeserializeOwned>( + &mut self, + endpoint: P, + mapjson: HashMap, + ) -> Result> { + post_api(self.get_current_server(), endpoint, mapjson).await + } + pub async fn get_countries_filtered>( &mut self, filter: P, @@ -183,3 +220,19 @@ impl RadioBrowserAPI { Ok(list) } } + +pub async fn post_api, B: AsRef>( + server: A, + endpoint: B, + mapjson: HashMap, +) -> Result> { + let client = reqwest::Client::new(); + let res = client + .post(format!("{}{}", server.as_ref(), endpoint.as_ref())) + .json(&mapjson) + .send() + .await? + .json::

() + .await?; + Ok(res) +}