A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: f9b9ad054863b48002ddb6992a5fad79e519803c (2120 bytes)[doc][blame][raw]
1
use serde::Deserialize;2
use std::fmt;4
use crate::error::Error;5
use crate::rest;6
use crate::Result;8
const API_URLS: [&str; 3] = [9
"https://de1.api.radio-browser.info",10
"https://fr1.api.radio-browser.info",11
"https://nl1.api.radio-browser.info",12
];14
#[derive(Debug, Default, Deserialize)]15
pub struct Search {16
pub name: String,17
pub page: Option<i64>,18
}20
impl fmt::Display for Search {21
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {22
write!(23
f,24
"name={}&offset={}&limit=10&order=votes&reverse=true&hidebroken=true",25
&self.name,26
&self.page.unwrap_or(0) * 10,27
)28
}29
}31
pub struct RadioBrowserApi;33
impl RadioBrowserApi {34
pub async fn search(search: Search) -> Result<Vec<rest::Station>> {35
let uri = format!("/json/stations/search?{}", &search);36
let mut response = RadioBrowserApi::fetch(&uri).await?;37
match response.body_json::<Vec<rest::Station>>().await {38
Ok(data) => Ok(data),39
Err(err) => Err(Error::new(&err.to_string())),40
}41
}43
pub async fn by_uuids(uuids: Vec<String>) -> Result<Vec<rest::Station>> {44
let uri = format!("/json/stations/byuuid?uuids={}", &uuids.join(","));45
let mut response = RadioBrowserApi::fetch(&uri).await?;46
match response.body_json::<Vec<rest::Station>>().await {47
Ok(data) => Ok(data),48
Err(err) => Err(Error::new(&err.to_string())),49
}50
}52
async fn fetch(uri: &str) -> Result<surf::Response> {53
let mut successful_response = None;54
for api_url in API_URLS {55
let url = format!("{}{}", &api_url, &uri,);56
match surf::get(&url).await {57
Ok(response) => {58
successful_response = Some(response);59
break;60
}61
Err(err) => println!("{}", Error::new(&err.to_string())),62
}63
}64
match successful_response {65
Some(response) => Ok(response),66
None => Err(Error::new("All request attempts failed!")),67
}68
}69
}