so://phie

verbal

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

blob: 1c945c634f546d2882f9777500228585f9f8428c (1960 bytes)[doc][blame][raw]

1use serde::{Deserialize, Serialize};

2

3use crate::common::Station;

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

5use crate::adapter::{Adapter, StationAdapter};

6

7const API_URLS: [&str; 3] = [

8 "https://de1.api.radio-browser.info",

9 "https://fr1.api.radio-browser.info",

10 "https://nl1.api.radio-browser.info",

11];

12

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

14pub struct Search {

15 pub name: String,

16 pub page: Option<i64>,

17}

18

19impl Search {

20

21 pub fn into_query(&self) -> String {

22 format!(

23 "name={}&offset={}&limit=10&order=votes&reverse=true&hidebroken=true",

24 &self.name,

25 &self.page.unwrap_or(0) * 10,

26 )

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.into_query(),

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(

62 adapter.clone().populate(Station::default())

63 );

64 }

65 Ok(stations)

66 }

67

68}