mirror of
https://gitlab.com/hladislav/radiobrowser-lib-rust.git
synced 2025-04-29 23:34:12 +00:00
added basic module
This commit is contained in:
parent
e3d90d5d43
commit
681f1c67dc
4 changed files with 114 additions and 0 deletions
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
/target
|
||||
|
||||
|
||||
# Added by cargo
|
||||
#
|
||||
# already existing elements were commented out
|
||||
|
||||
#/target
|
||||
/Cargo.lock
|
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "radiobrowser-lib-rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
async-std = { version = "*", features = ["attributes", "tokio1"] }
|
||||
reqwest = { version = "0.11.10", features = ["json"] }
|
||||
serde = { version = "1.0.136", features = ["derive"] }
|
||||
async-std-resolver = "0.21.2"
|
11
src/bin/test.rs
Normal file
11
src/bin/test.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use radiobrowser_lib_rust::ApiConfig;
|
||||
use std::error::Error;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let servers = radiobrowser_lib_rust::get_servers().await?;
|
||||
println!("Servers: {:?}", servers);
|
||||
let config: ApiConfig = radiobrowser_lib_rust::get_server_config().await?;
|
||||
println!("{:#?}", config);
|
||||
Ok(())
|
||||
}
|
82
src/lib.rs
Normal file
82
src/lib.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
use reqwest;
|
||||
use serde::Deserialize;
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ApiConfig {
|
||||
pub check_enabled: bool,
|
||||
pub prometheus_exporter_enabled: bool,
|
||||
pub pull_servers: Vec<String>,
|
||||
pub tcp_timeout_seconds: u32,
|
||||
pub broken_stations_never_working_timeout_seconds: u32,
|
||||
pub broken_stations_timeout_seconds: u32,
|
||||
pub checks_timeout_seconds: u32,
|
||||
pub click_valid_timeout_seconds: u32,
|
||||
pub clicks_timeout_seconds: u32,
|
||||
pub mirror_pull_interval_seconds: u32,
|
||||
pub update_caches_interval_seconds: u32,
|
||||
pub server_name: String,
|
||||
pub server_location: String,
|
||||
pub server_country_code: String,
|
||||
pub check_retries: u32,
|
||||
pub check_batchsize: u32,
|
||||
pub check_pause_seconds: u32,
|
||||
pub api_threads: u32,
|
||||
pub cache_type: String,
|
||||
pub cache_ttl: u32,
|
||||
pub language_replace_filepath: String,
|
||||
pub language_to_code_filepath: String,
|
||||
}
|
||||
|
||||
pub async fn get_server_config() -> Result<ApiConfig, Box<dyn Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let res = client
|
||||
.post("https://de1.api.radio-browser.info/json/config")
|
||||
.send()
|
||||
.await?
|
||||
.json::<ApiConfig>()
|
||||
.await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
use async_std_resolver::proto::rr::RecordType;
|
||||
use async_std_resolver::proto::xfer::DnsRequestOptions;
|
||||
use async_std_resolver::{config, resolver};
|
||||
|
||||
pub async fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
||||
// Construct a new Resolver with default configuration options
|
||||
let resolver = resolver(
|
||||
config::ResolverConfig::default(),
|
||||
config::ResolverOpts::default(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Lookup the IP addresses associated with a name.
|
||||
// This returns a future that will lookup the IP addresses, it must be run in the Core to
|
||||
// to get the actual result.
|
||||
let response = resolver
|
||||
.lookup(
|
||||
"_api._tcp.radio-browser.info",
|
||||
RecordType::SRV,
|
||||
DnsRequestOptions::default(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// There can be many addresses associated with the name,
|
||||
// this can return IPv4 and/or IPv6 addresses
|
||||
let list = response
|
||||
.iter()
|
||||
.filter_map(|entry| entry.as_srv())
|
||||
.map(|entry| entry.target().to_string())
|
||||
.collect();
|
||||
Ok(list)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = 2 + 2;
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue