added languages

This commit is contained in:
Alex 2022-04-21 21:18:17 +02:00
parent 8a803edd53
commit 2582a64dff
4 changed files with 101 additions and 10 deletions

View file

@ -1,8 +1,9 @@
use crate::CountrySearchBuilder;
use crate::StationSearchBuilder;
use crate::external::post_api;
use crate::ApiConfig;
use crate::ApiCountry;
use crate::external::post_api;
use crate::CountrySearchBuilder;
use crate::LanguageSearchBuilder;
use crate::StationSearchBuilder;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
@ -50,14 +51,18 @@ impl RadioBrowserAPI {
Ok(self.post_api("/json/config").await?)
}
pub fn search(&self) -> StationSearchBuilder {
pub fn get_stations(&self) -> StationSearchBuilder {
StationSearchBuilder::new(self.clone())
}
pub fn get_countries(&mut self) -> CountrySearchBuilder {
pub fn get_countries(&self) -> CountrySearchBuilder {
CountrySearchBuilder::new(self.clone())
}
pub fn get_languages(&self) -> LanguageSearchBuilder {
LanguageSearchBuilder::new(self.clone())
}
pub async fn send<P: AsRef<str>, Q: DeserializeOwned>(
&mut self,
endpoint: P,
@ -98,4 +103,4 @@ impl RadioBrowserAPI {
println!("Servers: {:?}", list);
Ok(list)
}
}
}

View file

@ -6,18 +6,20 @@ use std::error::Error;
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut api = RadioBrowserAPI::new().await?;
let countries = api.get_countries().filter("a").send();
let countries = api.get_countries().send();
let languages = api.get_languages().send();
let stations = api
.search()
.get_stations()
.name("jazz")
.reverse(true)
.order(StationOrder::Clickcount)
.send();
let config = api.get_server_config();
let (stations, config, countries) = join!(stations, config, countries);
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(())
}

View file

@ -0,0 +1,82 @@
use crate::ApiLanguage;
use crate::RadioBrowserAPI;
use std::fmt::Display;
use std::collections::HashMap;
use std::error::Error;
pub enum LanguageOrder {
Name,
StationCount,
}
impl Display for LanguageOrder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
LanguageOrder::Name => write!(f, "name"),
LanguageOrder::StationCount => write!(f, "stationcount"),
}
}
}
#[derive(Clone, Debug)]
pub struct LanguageSearchBuilder {
map: HashMap<String, String>,
api: RadioBrowserAPI,
filter: Option<String>,
}
impl LanguageSearchBuilder {
pub fn new(api: RadioBrowserAPI) -> Self {
LanguageSearchBuilder {
api,
map: HashMap::new(),
filter: None,
}
}
pub fn filter<P: AsRef<str>>(mut self, filter: P) -> Self {
self.filter = Some(filter.as_ref().to_string());
self
}
pub fn order(mut self, order: LanguageOrder) -> 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<P: AsRef<str>>(mut self, offset: P) -> Self {
self.map
.insert(String::from("offset"), offset.as_ref().to_string());
self
}
pub fn limit<P: AsRef<str>>(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<Vec<ApiLanguage>, Box<dyn Error>> {
if let Some(filter) = self.filter {
Ok(self
.api
.send(format!("/json/languages/{}", filter), self.map)
.await?)
} else {
Ok(self.api.send("/json/languages", self.map).await?)
}
}
}

View file

@ -2,6 +2,7 @@ mod api;
mod external;
mod stationsearchbuilder;
mod countrysearchbuilder;
mod languagesearchbuilder;
mod structs;
pub use api::RadioBrowserAPI;
@ -12,4 +13,5 @@ pub use structs::ApiStation;
pub use structs::ApiStreamingServer;
pub use stationsearchbuilder::StationSearchBuilder;
pub use stationsearchbuilder::StationOrder;
pub use countrysearchbuilder::CountrySearchBuilder;
pub use countrysearchbuilder::CountrySearchBuilder;
pub use languagesearchbuilder::LanguageSearchBuilder;