mirror of
https://gitlab.com/hladislav/radiobrowser-lib-rust.git
synced 2025-04-29 23:34:12 +00:00
changeable server names
This commit is contained in:
parent
62b1ca0a96
commit
4427a09c41
3 changed files with 41 additions and 8 deletions
33
src/api.rs
33
src/api.rs
|
@ -43,11 +43,29 @@ pub struct RadioBrowserAPI {
|
||||||
|
|
||||||
impl RadioBrowserAPI {
|
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_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, Box<dyn Error>> {
|
||||||
Ok(RadioBrowserAPI {
|
Ok(RadioBrowserAPI {
|
||||||
servers: RadioBrowserAPI::get_servers().await?,
|
servers: RadioBrowserAPI::get_default_servers().await?,
|
||||||
|
current: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
pub async fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(RadioBrowserAPI {
|
||||||
|
servers: vec![dnsname.as_ref().to_string()],
|
||||||
|
current: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new instance of a radiobrowser api client from
|
||||||
|
/// 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>> {
|
||||||
|
Ok(RadioBrowserAPI {
|
||||||
|
servers: RadioBrowserAPI::get_servers_from_dns_srv(srvname).await?,
|
||||||
current: 0,
|
current: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -120,8 +138,13 @@ impl RadioBrowserAPI {
|
||||||
post_api(self.get_current_server(), endpoint, mapjson).await
|
post_api(self.get_current_server(), endpoint, mapjson).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
pub async fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
||||||
trace!("get_servers()");
|
trace!("get_default_servers()");
|
||||||
|
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>> {
|
||||||
|
trace!("get_servers_from_dns_srv()");
|
||||||
let resolver = resolver(
|
let resolver = resolver(
|
||||||
config::ResolverConfig::default(),
|
config::ResolverConfig::default(),
|
||||||
config::ResolverOpts::default(),
|
config::ResolverOpts::default(),
|
||||||
|
@ -129,7 +152,7 @@ impl RadioBrowserAPI {
|
||||||
.await?;
|
.await?;
|
||||||
let response = resolver
|
let response = resolver
|
||||||
.lookup(
|
.lookup(
|
||||||
"_api._tcp.radio-browser.info",
|
srvname.as_ref(),
|
||||||
RecordType::SRV,
|
RecordType::SRV,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::error::Error;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mut api = RadioBrowserAPI::new()?;
|
let mut api = RadioBrowserAPI::new()?;
|
||||||
let servers = RadioBrowserAPI::get_servers()?;
|
let servers = RadioBrowserAPI::get_default_servers()?;
|
||||||
println!("Servers: {:?}", servers);
|
println!("Servers: {:?}", servers);
|
||||||
let status = api.get_server_status()?;
|
let status = api.get_server_status()?;
|
||||||
println!("Status: {:#?}", status);
|
println!("Status: {:#?}", status);
|
||||||
|
|
|
@ -25,6 +25,16 @@ impl RadioBrowserAPI {
|
||||||
.map(|api| RadioBrowserAPI { api })
|
.map(|api| RadioBrowserAPI { api })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
|
||||||
|
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_a(dnsname).await })
|
||||||
|
.map(|api| RadioBrowserAPI { api })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, Box<dyn Error>> {
|
||||||
|
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_srv(srvname).await })
|
||||||
|
.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, Box<dyn Error>> {
|
||||||
task::block_on(async { self.api.get_server_status().await })
|
task::block_on(async { self.api.get_server_status().await })
|
||||||
}
|
}
|
||||||
|
@ -71,7 +81,7 @@ impl RadioBrowserAPI {
|
||||||
task::block_on(async { self.api.send(endpoint, mapjson).await })
|
task::block_on(async { self.api.send(endpoint, mapjson).await })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
pub fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
||||||
task::block_on(async { crate::RadioBrowserAPI::get_servers().await })
|
task::block_on(async { crate::RadioBrowserAPI::get_default_servers().await })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue