so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 4ec5f21d5e7c6d434d8fba113b6ef53ff3237b85 (1936 bytes)[doc][blame][raw]

1use std::fmt;

2use serde::{Deserialize, Serialize};

3

4use crate::error::{Result, Error, ErrorKind};

5use crate::models::Station;

6use crate::adapter::StationAdapter;

7

8const 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];

13

14#[derive(Debug, Default, Deserialize, Serialize)]

15pub struct Search {

16 pub name: String,

17 pub page: Option<i64>,

18}

19

20impl 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}

30

31pub struct RadioBrowserApi;

32

33impl RadioBrowserApi {

34

35 pub async fn search(search: Search) -> Result<Vec<Station>> {

36 let mut successful_response = None;

37 for api_url in API_URLS {

38 let url = format!(

39 "{}/json/stations/search?{}",

40 &api_url,

41 &search,

42 );

43 match surf::get(&url).await {

44 Ok(response) => {

45 successful_response = Some(response);

46 break;

47 },

48 Err(err) => println!("{}", Error::new(ErrorKind::Fetch, &err.to_string())),

49 }

50 }

51 let mut response = match successful_response {

52 Some(response) => response,

53 None => return Err(Error::new(ErrorKind::Fetch, "All request attempts failed!")),

54 };

55 let adapters = match response.body_json::<Vec<StationAdapter>>().await {

56 Ok(adapters) => adapters,

57 Err(err) => return Err(Error::new(ErrorKind::Parse, &err.to_string())),

58 };

59 let mut stations = vec![];

60 for adapter in adapters {

61 stations.push(adapter.into());

62 }

63 Ok(stations)

64 }

65

66}