From 9fd4f92a9ec8d418cf535308182d76494ff9967a Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 21 Apr 2022 22:47:23 +0200 Subject: [PATCH] added tags, changed name --- Cargo.toml | 2 +- src/api.rs | 5 +++ src/bin/test.rs | 10 +++-- src/external.rs | 2 +- src/lib.rs | 36 +++++++++++++++++- src/structs.rs | 6 +++ src/tagsearchbuilder.rs | 82 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 src/tagsearchbuilder.rs diff --git a/Cargo.toml b/Cargo.toml index 1005b40..67300fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "radiobrowser-lib-rust" +name = "radiobrowser" version = "0.1.0" edition = "2021" diff --git a/src/api.rs b/src/api.rs index 7548524..cef665c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,6 +4,7 @@ use crate::ApiCountry; use crate::CountrySearchBuilder; use crate::LanguageSearchBuilder; use crate::StationSearchBuilder; +use crate::TagSearchBuilder; use serde::de::DeserializeOwned; use std::collections::HashMap; @@ -63,6 +64,10 @@ impl RadioBrowserAPI { LanguageSearchBuilder::new(self.clone()) } + pub fn get_tags(&self) -> TagSearchBuilder { + TagSearchBuilder::new(self.clone()) + } + pub async fn send, Q: DeserializeOwned>( &mut self, endpoint: P, diff --git a/src/bin/test.rs b/src/bin/test.rs index 27b29ec..b35165b 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -1,6 +1,6 @@ use futures::join; -use radiobrowser_lib_rust::RadioBrowserAPI; -use radiobrowser_lib_rust::StationOrder; +use radiobrowser::RadioBrowserAPI; +use radiobrowser::StationOrder; use std::error::Error; #[async_std::main] @@ -8,6 +8,7 @@ async fn main() -> Result<(), Box> { let mut api = RadioBrowserAPI::new().await?; let countries = api.get_countries().send(); let languages = api.get_languages().send(); + let tags = api.get_tags().filter("jazz").send(); let stations = api .get_stations() .name("jazz") @@ -15,11 +16,14 @@ async fn main() -> Result<(), Box> { .order(StationOrder::Clickcount) .send(); let config = api.get_server_config(); - let (stations, config, countries, languages) = join!(stations, config, countries, languages); + let (stations, config, countries, languages, tags) = join!(stations, config, countries, languages, tags); println!("Config: {:#?}", config?); println!("Countries found: {}", countries?.len()); println!("Languages found: {}", languages?.len()); + let tags = tags?; + println!("Tags found: {}", tags.len()); + println!("{:?}", tags); println!("Stations found: {}", stations?.len()); Ok(()) } diff --git a/src/external.rs b/src/external.rs index fc11831..4b2ea0e 100644 --- a/src/external.rs +++ b/src/external.rs @@ -8,7 +8,7 @@ pub async fn post_api, B: AsRef>( endpoint: B, mapjson: HashMap, ) -> Result> { - static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); + static APP_USER_AGENT: &str = concat!("radiobrowser-lib-rust/", env!("CARGO_PKG_VERSION"),); let client = reqwest::Client::builder() .user_agent(APP_USER_AGENT) diff --git a/src/lib.rs b/src/lib.rs index 90595fc..7951b24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,40 @@ +//! +//! +//! # Example +//! ```rust +//! use std::error::Error; +//! use futures::join; +//! use radiobrowser::RadioBrowserAPI; +//! use radiobrowser::StationOrder; +//! +//! #[async_std::main] +//! async fn main() -> Result<(), Box> { +//! let mut api = RadioBrowserAPI::new().await?; +//! let countries = api.get_countries().send(); +//! let languages = api.get_languages().send(); +//! let stations = api +//! .get_stations() +//! .name("jazz") +//! .reverse(true) +//! .order(StationOrder::Clickcount) +//! .send(); +//! let config = api.get_server_config(); +//! let (stations, config, countries, languages) = join!(stations, config, countries, languages); +//! +//! println!("Config: {:#?}", config?); +//! println!("Countries found: {}", countries?.len()); +//! println!("Languages found: {}", languages?.len()); +//! println!("Stations found: {}", stations?.len()); +//! Ok(()) +//! } +//! ``` + mod api; mod external; mod stationsearchbuilder; mod countrysearchbuilder; mod languagesearchbuilder; +mod tagsearchbuilder; mod structs; pub use api::RadioBrowserAPI; @@ -11,7 +43,9 @@ pub use structs::ApiCountry; pub use structs::ApiLanguage; pub use structs::ApiStation; pub use structs::ApiStreamingServer; +pub use structs::ApiTag; pub use stationsearchbuilder::StationSearchBuilder; pub use stationsearchbuilder::StationOrder; pub use countrysearchbuilder::CountrySearchBuilder; -pub use languagesearchbuilder::LanguageSearchBuilder; \ No newline at end of file +pub use languagesearchbuilder::LanguageSearchBuilder; +pub use tagsearchbuilder::TagSearchBuilder; \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs index f5b5b91..f2aa1bd 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -56,6 +56,12 @@ pub struct ApiLanguage { pub stationcount: u32, } +#[derive(PartialEq, Eq, Deserialize, Debug)] +pub struct ApiTag { + pub name: String, + pub stationcount: u32, +} + #[derive(PartialEq, Deserialize, Debug)] pub struct ApiStreamingServer { pub uuid: String, diff --git a/src/tagsearchbuilder.rs b/src/tagsearchbuilder.rs new file mode 100644 index 0000000..506cfa9 --- /dev/null +++ b/src/tagsearchbuilder.rs @@ -0,0 +1,82 @@ +use crate::ApiTag; +use crate::RadioBrowserAPI; +use std::fmt::Display; + +use std::collections::HashMap; +use std::error::Error; + +pub enum TagOrder { + Name, + StationCount, +} + +impl Display for TagOrder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + match self { + TagOrder::Name => write!(f, "name"), + TagOrder::StationCount => write!(f, "stationcount"), + } + } +} + +#[derive(Clone, Debug)] +pub struct TagSearchBuilder { + map: HashMap, + api: RadioBrowserAPI, + filter: Option, +} + +impl TagSearchBuilder { + pub fn new(api: RadioBrowserAPI) -> Self { + TagSearchBuilder { + api, + map: HashMap::new(), + filter: None, + } + } + + pub fn filter>(mut self, filter: P) -> Self { + self.filter = Some(filter.as_ref().to_string()); + self + } + + pub fn order(mut self, order: TagOrder) -> Self { + self.map.insert(String::from("order"), order.to_string()); + self + } + + pub fn reverse(mut self, reverse: bool) -> Self { + self.map + .insert(String::from("reverse"), reverse.to_string()); + self + } + + pub fn offset>(mut self, offset: P) -> Self { + self.map + .insert(String::from("offset"), offset.as_ref().to_string()); + self + } + + pub fn limit>(mut self, limit: P) -> Self { + self.map + .insert(String::from("limit"), limit.as_ref().to_string()); + self + } + + pub fn hidebroken(mut self, hidebroken: bool) -> Self { + self.map + .insert(String::from("hidebroken"), hidebroken.to_string()); + self + } + + pub async fn send(mut self) -> Result, Box> { + if let Some(filter) = self.filter { + Ok(self + .api + .send(format!("/json/tags/{}", filter), self.map) + .await?) + } else { + Ok(self.api.send("/json/tags", self.map).await?) + } + } +}