mirror of
https://gitlab.com/hladislav/radiobrowser-lib-rust.git
synced 2025-04-30 07:44:11 +00:00
added tags, changed name
This commit is contained in:
parent
15bad37787
commit
9fd4f92a9e
7 changed files with 137 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "radiobrowser-lib-rust"
|
name = "radiobrowser"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ use crate::ApiCountry;
|
||||||
use crate::CountrySearchBuilder;
|
use crate::CountrySearchBuilder;
|
||||||
use crate::LanguageSearchBuilder;
|
use crate::LanguageSearchBuilder;
|
||||||
use crate::StationSearchBuilder;
|
use crate::StationSearchBuilder;
|
||||||
|
use crate::TagSearchBuilder;
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -63,6 +64,10 @@ impl RadioBrowserAPI {
|
||||||
LanguageSearchBuilder::new(self.clone())
|
LanguageSearchBuilder::new(self.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_tags(&self) -> TagSearchBuilder {
|
||||||
|
TagSearchBuilder::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn send<P: AsRef<str>, Q: DeserializeOwned>(
|
pub async fn send<P: AsRef<str>, Q: DeserializeOwned>(
|
||||||
&mut self,
|
&mut self,
|
||||||
endpoint: P,
|
endpoint: P,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use futures::join;
|
use futures::join;
|
||||||
use radiobrowser_lib_rust::RadioBrowserAPI;
|
use radiobrowser::RadioBrowserAPI;
|
||||||
use radiobrowser_lib_rust::StationOrder;
|
use radiobrowser::StationOrder;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
|
@ -8,6 +8,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mut api = RadioBrowserAPI::new().await?;
|
let mut api = RadioBrowserAPI::new().await?;
|
||||||
let countries = api.get_countries().send();
|
let countries = api.get_countries().send();
|
||||||
let languages = api.get_languages().send();
|
let languages = api.get_languages().send();
|
||||||
|
let tags = api.get_tags().filter("jazz").send();
|
||||||
let stations = api
|
let stations = api
|
||||||
.get_stations()
|
.get_stations()
|
||||||
.name("jazz")
|
.name("jazz")
|
||||||
|
@ -15,11 +16,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.order(StationOrder::Clickcount)
|
.order(StationOrder::Clickcount)
|
||||||
.send();
|
.send();
|
||||||
let config = api.get_server_config();
|
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!("Config: {:#?}", config?);
|
||||||
println!("Countries found: {}", countries?.len());
|
println!("Countries found: {}", countries?.len());
|
||||||
println!("Languages found: {}", languages?.len());
|
println!("Languages found: {}", languages?.len());
|
||||||
|
let tags = tags?;
|
||||||
|
println!("Tags found: {}", tags.len());
|
||||||
|
println!("{:?}", tags);
|
||||||
println!("Stations found: {}", stations?.len());
|
println!("Stations found: {}", stations?.len());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub async fn post_api<P: DeserializeOwned, A: AsRef<str>, B: AsRef<str>>(
|
||||||
endpoint: B,
|
endpoint: B,
|
||||||
mapjson: HashMap<String, String>,
|
mapjson: HashMap<String, String>,
|
||||||
) -> Result<P, Box<dyn Error>> {
|
) -> 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()
|
let client = reqwest::Client::builder()
|
||||||
.user_agent(APP_USER_AGENT)
|
.user_agent(APP_USER_AGENT)
|
||||||
|
|
34
src/lib.rs
34
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<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 api;
|
||||||
mod external;
|
mod external;
|
||||||
mod stationsearchbuilder;
|
mod stationsearchbuilder;
|
||||||
mod countrysearchbuilder;
|
mod countrysearchbuilder;
|
||||||
mod languagesearchbuilder;
|
mod languagesearchbuilder;
|
||||||
|
mod tagsearchbuilder;
|
||||||
mod structs;
|
mod structs;
|
||||||
|
|
||||||
pub use api::RadioBrowserAPI;
|
pub use api::RadioBrowserAPI;
|
||||||
|
@ -11,7 +43,9 @@ pub use structs::ApiCountry;
|
||||||
pub use structs::ApiLanguage;
|
pub use structs::ApiLanguage;
|
||||||
pub use structs::ApiStation;
|
pub use structs::ApiStation;
|
||||||
pub use structs::ApiStreamingServer;
|
pub use structs::ApiStreamingServer;
|
||||||
|
pub use structs::ApiTag;
|
||||||
pub use stationsearchbuilder::StationSearchBuilder;
|
pub use stationsearchbuilder::StationSearchBuilder;
|
||||||
pub use stationsearchbuilder::StationOrder;
|
pub use stationsearchbuilder::StationOrder;
|
||||||
pub use countrysearchbuilder::CountrySearchBuilder;
|
pub use countrysearchbuilder::CountrySearchBuilder;
|
||||||
pub use languagesearchbuilder::LanguageSearchBuilder;
|
pub use languagesearchbuilder::LanguageSearchBuilder;
|
||||||
|
pub use tagsearchbuilder::TagSearchBuilder;
|
|
@ -56,6 +56,12 @@ pub struct ApiLanguage {
|
||||||
pub stationcount: u32,
|
pub stationcount: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Deserialize, Debug)]
|
||||||
|
pub struct ApiTag {
|
||||||
|
pub name: String,
|
||||||
|
pub stationcount: u32,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Deserialize, Debug)]
|
#[derive(PartialEq, Deserialize, Debug)]
|
||||||
pub struct ApiStreamingServer {
|
pub struct ApiStreamingServer {
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
|
|
82
src/tagsearchbuilder.rs
Normal file
82
src/tagsearchbuilder.rs
Normal 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?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue