From 681f1c67dcbf2db1d275a06210d82e4dd9aab9db Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 19 Apr 2022 01:55:26 +0200 Subject: [PATCH] added basic module --- .gitignore | 9 ++++++ Cargo.toml | 12 ++++++++ src/bin/test.rs | 11 +++++++ src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/bin/test.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ce42ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/target + + +# Added by cargo +# +# already existing elements were commented out + +#/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b314558 --- /dev/null +++ b/Cargo.toml @@ -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" \ No newline at end of file diff --git a/src/bin/test.rs b/src/bin/test.rs new file mode 100644 index 0000000..21880c6 --- /dev/null +++ b/src/bin/test.rs @@ -0,0 +1,11 @@ +use radiobrowser_lib_rust::ApiConfig; +use std::error::Error; + +#[async_std::main] +async fn main() -> Result<(), Box> { + let servers = radiobrowser_lib_rust::get_servers().await?; + println!("Servers: {:?}", servers); + let config: ApiConfig = radiobrowser_lib_rust::get_server_config().await?; + println!("{:#?}", config); + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..45ed6fc --- /dev/null +++ b/src/lib.rs @@ -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, + 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> { + let client = reqwest::Client::new(); + let res = client + .post("https://de1.api.radio-browser.info/json/config") + .send() + .await? + .json::() + .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, Box> { + // 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); + } +}