added tags, changed name

This commit is contained in:
Alex 2022-04-21 22:47:23 +02:00
parent 15bad37787
commit 9fd4f92a9e
7 changed files with 137 additions and 6 deletions

View file

@ -1,5 +1,5 @@
[package]
name = "radiobrowser-lib-rust"
name = "radiobrowser"
version = "0.1.0"
edition = "2021"

View file

@ -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<P: AsRef<str>, Q: DeserializeOwned>(
&mut self,
endpoint: P,

View file

@ -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<dyn Error>> {
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<dyn Error>> {
.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(())
}

View file

@ -8,7 +8,7 @@ pub async fn post_api<P: DeserializeOwned, A: AsRef<str>, B: AsRef<str>>(
endpoint: B,
mapjson: HashMap<String, String>,
) -> Result<P, Box<dyn Error>> {
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)

View file

@ -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<dyn Error>> {
//! 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;
pub use tagsearchbuilder::TagSearchBuilder;

View file

@ -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,

82
src/tagsearchbuilder.rs Normal file
View file

@ -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<String, String>,
api: RadioBrowserAPI,
filter: Option<String>,
}
impl TagSearchBuilder {
pub fn new(api: RadioBrowserAPI) -> Self {
TagSearchBuilder {
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: 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<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<ApiTag>, Box<dyn Error>> {
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?)
}
}
}