mirror of
https://gitlab.com/hladislav/radiobrowser-lib-rust.git
synced 2025-04-29 23:34:12 +00:00
replaced box dyn error with rberror
This commit is contained in:
parent
0f5ed27d26
commit
3703d731d1
17 changed files with 91 additions and 61 deletions
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Replaced Box dyn Error with RbError enum
|
||||||
|
|
||||||
## [0.6.1] 2023-10-20
|
## [0.6.1] 2023-10-20
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
@ -24,7 +24,7 @@ radiobrowser = { version = "*", features = ["blocking"] }
|
||||||
use radiobrowser::blocking::RadioBrowserAPI;
|
use radiobrowser::blocking::RadioBrowserAPI;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), RbError> {
|
||||||
let mut api = RadioBrowserAPI::new()?;
|
let mut api = RadioBrowserAPI::new()?;
|
||||||
let servers = RadioBrowserAPI::get_default_servers()?;
|
let servers = RadioBrowserAPI::get_default_servers()?;
|
||||||
println!("Servers: {:?}", servers);
|
println!("Servers: {:?}", servers);
|
||||||
|
@ -50,7 +50,7 @@ use radiobrowser::StationOrder;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), RbError> {
|
||||||
let mut api = RadioBrowserAPI::new().await?;
|
let mut api = RadioBrowserAPI::new().await?;
|
||||||
let stations = api
|
let stations = api
|
||||||
.get_stations()
|
.get_stations()
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use futures::join;
|
use futures::join;
|
||||||
use radiobrowser::RadioBrowserAPI;
|
use radiobrowser::RadioBrowserAPI;
|
||||||
|
use radiobrowser::RbError;
|
||||||
use radiobrowser::StationOrder;
|
use radiobrowser::StationOrder;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), RbError> {
|
||||||
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();
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use radiobrowser::blocking::RadioBrowserAPI;
|
use radiobrowser::{blocking::RadioBrowserAPI, RbError};
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), RbError> {
|
||||||
let mut api = RadioBrowserAPI::new()?;
|
let mut api = RadioBrowserAPI::new()?;
|
||||||
let servers = RadioBrowserAPI::get_default_servers()?;
|
let servers = RadioBrowserAPI::get_default_servers()?;
|
||||||
println!("Servers: {:?}", servers);
|
println!("Servers: {:?}", servers);
|
||||||
|
|
30
src/api.rs
30
src/api.rs
|
@ -2,6 +2,7 @@ use crate::ApiStationClickResult;
|
||||||
use crate::ApiStationHistory;
|
use crate::ApiStationHistory;
|
||||||
use crate::ApiStationVoteResult;
|
use crate::ApiStationVoteResult;
|
||||||
use crate::ApiStatus;
|
use crate::ApiStatus;
|
||||||
|
use crate::RbError;
|
||||||
use crate::external::post_api;
|
use crate::external::post_api;
|
||||||
use crate::ApiConfig;
|
use crate::ApiConfig;
|
||||||
use crate::CountrySearchBuilder;
|
use crate::CountrySearchBuilder;
|
||||||
|
@ -11,7 +12,6 @@ use crate::TagSearchBuilder;
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
|
@ -27,10 +27,10 @@ use async_std_resolver::{config, resolver};
|
||||||
///
|
///
|
||||||
/// Example
|
/// Example
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use std::error::Error;
|
/// use radiobrowser::RbError;
|
||||||
/// use radiobrowser::RadioBrowserAPI;
|
/// use radiobrowser::RadioBrowserAPI;
|
||||||
/// #[async_std::main]
|
/// #[async_std::main]
|
||||||
/// async fn main() -> Result<(), Box<dyn Error>> {
|
/// async fn main() -> Result<(), RbError> {
|
||||||
/// let mut api = RadioBrowserAPI::new().await?;
|
/// let mut api = RadioBrowserAPI::new().await?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
|
@ -45,7 +45,7 @@ impl RadioBrowserAPI {
|
||||||
/// Create a new instance of a radiobrowser api client.
|
/// Create a new instance of a radiobrowser api client.
|
||||||
/// It will fetch a list of radiobrowser server with get_default_servers()
|
/// It will fetch a list of radiobrowser server with get_default_servers()
|
||||||
/// and save it internally.
|
/// and save it internally.
|
||||||
pub async fn new() -> Result<Self, Box<dyn Error>> {
|
pub async fn new() -> Result<Self, RbError> {
|
||||||
Ok(RadioBrowserAPI {
|
Ok(RadioBrowserAPI {
|
||||||
servers: RadioBrowserAPI::get_default_servers().await?,
|
servers: RadioBrowserAPI::get_default_servers().await?,
|
||||||
current: 0,
|
current: 0,
|
||||||
|
@ -54,7 +54,7 @@ impl RadioBrowserAPI {
|
||||||
|
|
||||||
/// Create a new instance of a radiobrowser api client from
|
/// Create a new instance of a radiobrowser api client from
|
||||||
/// a single dns name. Use this is you want to connect to a single named server.
|
/// a single dns name. Use this is you want to connect to a single named server.
|
||||||
pub async fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
|
pub async fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, RbError> {
|
||||||
Ok(RadioBrowserAPI {
|
Ok(RadioBrowserAPI {
|
||||||
servers: vec![dnsname.as_ref().to_string()],
|
servers: vec![dnsname.as_ref().to_string()],
|
||||||
current: 0,
|
current: 0,
|
||||||
|
@ -63,7 +63,7 @@ impl RadioBrowserAPI {
|
||||||
|
|
||||||
/// Create a new instance of a radiobrowser api client from
|
/// Create a new instance of a radiobrowser api client from
|
||||||
/// a dns srv record which may have multiple dns A/AAAA records.
|
/// a dns srv record which may have multiple dns A/AAAA records.
|
||||||
pub async fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, Box<dyn Error>> {
|
pub async fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, RbError> {
|
||||||
Ok(RadioBrowserAPI {
|
Ok(RadioBrowserAPI {
|
||||||
servers: RadioBrowserAPI::get_servers_from_dns_srv(srvname).await?,
|
servers: RadioBrowserAPI::get_servers_from_dns_srv(srvname).await?,
|
||||||
current: 0,
|
current: 0,
|
||||||
|
@ -83,12 +83,12 @@ impl RadioBrowserAPI {
|
||||||
async fn post_api<P: DeserializeOwned, A: AsRef<str>>(
|
async fn post_api<P: DeserializeOwned, A: AsRef<str>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
endpoint: A,
|
endpoint: A,
|
||||||
) -> Result<P, Box<dyn Error>> {
|
) -> Result<P, RbError> {
|
||||||
let mapjson = HashMap::new();
|
let mapjson = HashMap::new();
|
||||||
post_api(self.get_current_server(), endpoint.as_ref(), mapjson).await
|
post_api(self.get_current_server(), endpoint.as_ref(), mapjson).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, Box<dyn Error>> {
|
pub async fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, RbError> {
|
||||||
let query = match last_change_uuid {
|
let query = match last_change_uuid {
|
||||||
Some(uuid) => format!("/json/stations/changed?limit={}&lastchangeuuid={}", limit, uuid),
|
Some(uuid) => format!("/json/stations/changed?limit={}&lastchangeuuid={}", limit, uuid),
|
||||||
None => format!("/json/stations/changed?limit={}", limit)
|
None => format!("/json/stations/changed?limit={}", limit)
|
||||||
|
@ -96,21 +96,21 @@ impl RadioBrowserAPI {
|
||||||
Ok(self.post_api(query).await?)
|
Ok(self.post_api(query).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_config(&mut self) -> Result<ApiConfig, Box<dyn Error>> {
|
pub async fn get_server_config(&mut self) -> Result<ApiConfig, RbError> {
|
||||||
Ok(self.post_api("/json/config").await?)
|
Ok(self.post_api("/json/config").await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_status(&mut self) -> Result<ApiStatus, Box<dyn Error>> {
|
pub async fn get_server_status(&mut self) -> Result<ApiStatus, RbError> {
|
||||||
Ok(self.post_api("/json/stats").await?)
|
Ok(self.post_api("/json/stats").await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a click to a station found by stationuuid
|
/// Add a click to a station found by stationuuid
|
||||||
pub async fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, Box<dyn Error>> {
|
pub async fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, RbError> {
|
||||||
Ok(self.post_api(format!("/json/url/{}",stationuuid.as_ref())).await?)
|
Ok(self.post_api(format!("/json/url/{}",stationuuid.as_ref())).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a vote to a station found by a stationuuid
|
/// Add a vote to a station found by a stationuuid
|
||||||
pub async fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, Box<dyn Error>> {
|
pub async fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, RbError> {
|
||||||
Ok(self.post_api(format!("/json/vote/{}",stationuuid.as_ref())).await?)
|
Ok(self.post_api(format!("/json/vote/{}",stationuuid.as_ref())).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,16 +134,16 @@ impl RadioBrowserAPI {
|
||||||
&mut self,
|
&mut self,
|
||||||
endpoint: P,
|
endpoint: P,
|
||||||
mapjson: HashMap<String, String>,
|
mapjson: HashMap<String, String>,
|
||||||
) -> Result<Q, Box<dyn Error>> {
|
) -> Result<Q, RbError> {
|
||||||
post_api(self.get_current_server(), endpoint, mapjson).await
|
post_api(self.get_current_server(), endpoint, mapjson).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
pub async fn get_default_servers() -> Result<Vec<String>, RbError> {
|
||||||
trace!("get_default_servers()");
|
trace!("get_default_servers()");
|
||||||
RadioBrowserAPI::get_servers_from_dns_srv("_api._tcp.radio-browser.info").await
|
RadioBrowserAPI::get_servers_from_dns_srv("_api._tcp.radio-browser.info").await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_servers_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Vec<String>, Box<dyn Error>> {
|
async fn get_servers_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Vec<String>, RbError> {
|
||||||
trace!("get_servers_from_dns_srv()");
|
trace!("get_servers_from_dns_srv()");
|
||||||
let resolver = resolver(
|
let resolver = resolver(
|
||||||
config::ResolverConfig::default(),
|
config::ResolverConfig::default(),
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::ApiStationHistory;
|
use crate::ApiStationHistory;
|
||||||
|
use crate::RbError;
|
||||||
use crate::blocking::stationsearchbuilder::StationSearchBuilder;
|
use crate::blocking::stationsearchbuilder::StationSearchBuilder;
|
||||||
use crate::blocking::CountrySearchBuilder;
|
use crate::blocking::CountrySearchBuilder;
|
||||||
use crate::blocking::LanguageSearchBuilder;
|
use crate::blocking::LanguageSearchBuilder;
|
||||||
|
@ -10,7 +11,6 @@ use crate::ApiConfig;
|
||||||
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RadioBrowserAPI {
|
pub struct RadioBrowserAPI {
|
||||||
|
@ -20,40 +20,40 @@ pub struct RadioBrowserAPI {
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
|
|
||||||
impl RadioBrowserAPI {
|
impl RadioBrowserAPI {
|
||||||
pub fn new() -> Result<Self, Box<dyn Error>> {
|
pub fn new() -> Result<Self, RbError> {
|
||||||
task::block_on(async { crate::RadioBrowserAPI::new().await })
|
task::block_on(async { crate::RadioBrowserAPI::new().await })
|
||||||
.map(|api| RadioBrowserAPI { api })
|
.map(|api| RadioBrowserAPI { api })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
|
pub fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, RbError> {
|
||||||
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_a(dnsname).await })
|
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_a(dnsname).await })
|
||||||
.map(|api| RadioBrowserAPI { api })
|
.map(|api| RadioBrowserAPI { api })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, Box<dyn Error>> {
|
pub fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, RbError> {
|
||||||
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_srv(srvname).await })
|
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_srv(srvname).await })
|
||||||
.map(|api| RadioBrowserAPI { api })
|
.map(|api| RadioBrowserAPI { api })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_server_status(&mut self) -> Result<ApiStatus, Box<dyn Error>> {
|
pub fn get_server_status(&mut self) -> Result<ApiStatus, RbError> {
|
||||||
task::block_on(async { self.api.get_server_status().await })
|
task::block_on(async { self.api.get_server_status().await })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, Box<dyn Error>> {
|
pub fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, RbError> {
|
||||||
task::block_on(async { self.api.get_station_changes(limit, last_change_uuid).await })
|
task::block_on(async { self.api.get_station_changes(limit, last_change_uuid).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_server_config(&mut self) -> Result<ApiConfig, Box<dyn Error>> {
|
pub fn get_server_config(&mut self) -> Result<ApiConfig, RbError> {
|
||||||
task::block_on(async { self.api.get_server_config().await })
|
task::block_on(async { self.api.get_server_config().await })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a click to a station found by stationuuid
|
/// Add a click to a station found by stationuuid
|
||||||
pub fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, Box<dyn Error>> {
|
pub fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, RbError> {
|
||||||
task::block_on(async { self.api.station_click(stationuuid).await })
|
task::block_on(async { self.api.station_click(stationuuid).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a vote to a station found by a stationuuid
|
/// Add a vote to a station found by a stationuuid
|
||||||
pub fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, Box<dyn Error>> {
|
pub fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, RbError> {
|
||||||
task::block_on(async { self.api.station_vote(stationuuid).await })
|
task::block_on(async { self.api.station_vote(stationuuid).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +77,11 @@ impl RadioBrowserAPI {
|
||||||
&mut self,
|
&mut self,
|
||||||
endpoint: P,
|
endpoint: P,
|
||||||
mapjson: HashMap<String, String>,
|
mapjson: HashMap<String, String>,
|
||||||
) -> Result<Q, Box<dyn Error>> {
|
) -> Result<Q, RbError> {
|
||||||
task::block_on(async { self.api.send(endpoint, mapjson).await })
|
task::block_on(async { self.api.send(endpoint, mapjson).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
pub fn get_default_servers() -> Result<Vec<String>, RbError> {
|
||||||
task::block_on(async { crate::RadioBrowserAPI::get_default_servers().await })
|
task::block_on(async { crate::RadioBrowserAPI::get_default_servers().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ApiCountry;
|
use crate::{ApiCountry, RbError};
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct CountrySearchBuilder {
|
pub struct CountrySearchBuilder {
|
||||||
|
@ -48,7 +47,7 @@ impl CountrySearchBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(self) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
|
pub fn send(self) -> Result<Vec<ApiCountry>, RbError> {
|
||||||
task::block_on(async { self.builder.send().await })
|
task::block_on(async { self.builder.send().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ApiLanguage;
|
use crate::{ApiLanguage, RbError};
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct LanguageSearchBuilder {
|
pub struct LanguageSearchBuilder {
|
||||||
|
@ -48,7 +47,7 @@ impl LanguageSearchBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(self) -> Result<Vec<ApiLanguage>, Box<dyn Error>> {
|
pub fn send(self) -> Result<Vec<ApiLanguage>, RbError> {
|
||||||
task::block_on(async { self.builder.send().await })
|
task::block_on(async { self.builder.send().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::structs::ApiStation;
|
use crate::structs::ApiStation;
|
||||||
use crate::StationOrder;
|
use crate::{StationOrder, RbError};
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct StationSearchBuilder {
|
pub struct StationSearchBuilder {
|
||||||
|
@ -151,7 +150,7 @@ impl StationSearchBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(self) -> Result<Vec<ApiStation>, Box<dyn Error>> {
|
pub fn send(self) -> Result<Vec<ApiStation>, RbError> {
|
||||||
task::block_on(async { self.builder.send().await })
|
task::block_on(async { self.builder.send().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::ApiTag;
|
use crate::{ApiTag, RbError};
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct TagSearchBuilder {
|
pub struct TagSearchBuilder {
|
||||||
|
@ -48,7 +47,7 @@ impl TagSearchBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(self) -> Result<Vec<ApiTag>, Box<dyn Error>> {
|
pub fn send(self) -> Result<Vec<ApiTag>, RbError> {
|
||||||
task::block_on(async { self.builder.send().await })
|
task::block_on(async { self.builder.send().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::ApiCountry;
|
use crate::ApiCountry;
|
||||||
use crate::RadioBrowserAPI;
|
use crate::RadioBrowserAPI;
|
||||||
|
use crate::RbError;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
pub enum CountryOrder {
|
pub enum CountryOrder {
|
||||||
Name,
|
Name,
|
||||||
|
@ -69,7 +68,7 @@ impl CountrySearchBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(mut self) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
|
pub async fn send(mut self) -> Result<Vec<ApiCountry>, RbError> {
|
||||||
if let Some(filter) = self.filter {
|
if let Some(filter) = self.filter {
|
||||||
Ok(self
|
Ok(self
|
||||||
.api
|
.api
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
use crate::RbError;
|
||||||
|
|
||||||
pub async fn post_api<P: DeserializeOwned, A: AsRef<str>, B: AsRef<str>>(
|
pub async fn post_api<P: DeserializeOwned, A: AsRef<str>, B: AsRef<str>>(
|
||||||
server: A,
|
server: A,
|
||||||
endpoint: B,
|
endpoint: B,
|
||||||
mapjson: HashMap<String, String>,
|
mapjson: HashMap<String, String>,
|
||||||
) -> Result<P, Box<dyn Error>> {
|
) -> Result<P, RbError> {
|
||||||
static APP_USER_AGENT: &str = concat!("radiobrowser-lib-rust/", 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()
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::ApiLanguage;
|
use crate::ApiLanguage;
|
||||||
use crate::RadioBrowserAPI;
|
use crate::RadioBrowserAPI;
|
||||||
|
use crate::RbError;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
pub enum LanguageOrder {
|
pub enum LanguageOrder {
|
||||||
Name,
|
Name,
|
||||||
|
@ -69,7 +68,7 @@ impl LanguageSearchBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(mut self) -> Result<Vec<ApiLanguage>, Box<dyn Error>> {
|
pub async fn send(mut self) -> Result<Vec<ApiLanguage>, RbError> {
|
||||||
if let Some(filter) = self.filter {
|
if let Some(filter) = self.filter {
|
||||||
Ok(self
|
Ok(self
|
||||||
.api
|
.api
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -7,9 +7,9 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use radiobrowser::blocking::RadioBrowserAPI;
|
//! use radiobrowser::blocking::RadioBrowserAPI;
|
||||||
//! use std::error::Error;
|
//! use radiobrowser::RbError;
|
||||||
//!
|
//!
|
||||||
//! fn main() -> Result<(), Box<dyn Error>> {
|
//! fn main() -> Result<(), RbError> {
|
||||||
//! let api = RadioBrowserAPI::new()?;
|
//! let api = RadioBrowserAPI::new()?;
|
||||||
//! let servers = RadioBrowserAPI::get_default_servers()?;
|
//! let servers = RadioBrowserAPI::get_default_servers()?;
|
||||||
//! println!("Servers: {:?}", servers);
|
//! println!("Servers: {:?}", servers);
|
||||||
|
@ -23,13 +23,13 @@
|
||||||
//!
|
//!
|
||||||
//! # Example async
|
//! # Example async
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use std::error::Error;
|
//! use radiobrowser::RbError;
|
||||||
//! use futures::join;
|
//! use futures::join;
|
||||||
//! use radiobrowser::RadioBrowserAPI;
|
//! use radiobrowser::RadioBrowserAPI;
|
||||||
//! use radiobrowser::StationOrder;
|
//! use radiobrowser::StationOrder;
|
||||||
//!
|
//!
|
||||||
//! #[async_std::main]
|
//! #[async_std::main]
|
||||||
//! async fn main() -> Result<(), Box<dyn Error>> {
|
//! async fn main() -> Result<(), RbError> {
|
||||||
//! 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();
|
||||||
|
@ -60,7 +60,9 @@ mod countrysearchbuilder;
|
||||||
mod languagesearchbuilder;
|
mod languagesearchbuilder;
|
||||||
mod tagsearchbuilder;
|
mod tagsearchbuilder;
|
||||||
mod structs;
|
mod structs;
|
||||||
|
mod rb_error;
|
||||||
|
|
||||||
|
pub use rb_error::RbError;
|
||||||
pub use api::RadioBrowserAPI;
|
pub use api::RadioBrowserAPI;
|
||||||
pub use structs::ApiConfig;
|
pub use structs::ApiConfig;
|
||||||
pub use structs::ApiCountry;
|
pub use structs::ApiCountry;
|
||||||
|
|
30
src/rb_error.rs
Normal file
30
src/rb_error.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
use async_std_resolver::ResolveError;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RbError {
|
||||||
|
Reqwest(reqwest::Error),
|
||||||
|
Resolve(ResolveError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for RbError {}
|
||||||
|
|
||||||
|
impl std::fmt::Display for RbError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
RbError::Reqwest(err) => write!(f, "Reqwest: {}", err),
|
||||||
|
RbError::Resolve(err) => write!(f, "Resolve: {}", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for RbError {
|
||||||
|
fn from(value: reqwest::Error) -> Self {
|
||||||
|
RbError::Reqwest(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ResolveError> for RbError {
|
||||||
|
fn from(value: ResolveError) -> Self {
|
||||||
|
RbError::Resolve(value)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
|
use crate::RbError;
|
||||||
use crate::api::RadioBrowserAPI;
|
use crate::api::RadioBrowserAPI;
|
||||||
use crate::structs::ApiStation;
|
use crate::structs::ApiStation;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
pub enum StationOrder {
|
pub enum StationOrder {
|
||||||
Name,
|
Name,
|
||||||
|
@ -206,7 +206,7 @@ impl StationSearchBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(mut self) -> Result<Vec<ApiStation>, Box<dyn Error>> {
|
pub async fn send(mut self) -> Result<Vec<ApiStation>, RbError> {
|
||||||
Ok(self.api.send("/json/stations/search", self.map).await?)
|
Ok(self.api.send("/json/stations/search", self.map).await?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use crate::ApiTag;
|
use crate::ApiTag;
|
||||||
use crate::RadioBrowserAPI;
|
use crate::RadioBrowserAPI;
|
||||||
|
use crate::RbError;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
pub enum TagOrder {
|
pub enum TagOrder {
|
||||||
Name,
|
Name,
|
||||||
|
@ -69,7 +69,7 @@ impl TagSearchBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(mut self) -> Result<Vec<ApiTag>, Box<dyn Error>> {
|
pub async fn send(mut self) -> Result<Vec<ApiTag>, RbError> {
|
||||||
if let Some(filter) = self.filter {
|
if let Some(filter) = self.filter {
|
||||||
Ok(self
|
Ok(self
|
||||||
.api
|
.api
|
||||||
|
|
Loading…
Reference in a new issue