Skip to main content

opensubsonic/api/
internet_radio.rs

1//! Internet Radio API endpoints.
2
3use crate::Client;
4use crate::data::InternetRadioStation;
5use crate::error::Error;
6
7impl Client {
8    /// Get all internet radio stations.
9    ///
10    /// See <https://opensubsonic.netlify.app/docs/endpoints/getinternetradiostations/>
11    pub async fn get_internet_radio_stations(&self) -> Result<Vec<InternetRadioStation>, Error> {
12        let data = self.get_response("getInternetRadioStations", &[]).await?;
13        let stations = data
14            .get("internetRadioStations")
15            .and_then(|v| v.get("internetRadioStation"))
16            .cloned()
17            .unwrap_or_else(|| serde_json::Value::Array(vec![]));
18        Ok(serde_json::from_value(stations)?)
19    }
20
21    /// Create a new internet radio station.
22    ///
23    /// See <https://opensubsonic.netlify.app/docs/endpoints/createinternetradiostation/>
24    pub async fn create_internet_radio_station(
25        &self,
26        stream_url: &str,
27        name: &str,
28        home_page_url: Option<&str>,
29    ) -> Result<(), Error> {
30        let mut params = vec![("streamUrl", stream_url), ("name", name)];
31        if let Some(hp) = home_page_url {
32            params.push(("homepageUrl", hp));
33        }
34        self.get_response("createInternetRadioStation", &params)
35            .await?;
36        Ok(())
37    }
38
39    /// Update an existing internet radio station.
40    ///
41    /// See <https://opensubsonic.netlify.app/docs/endpoints/updateinternetradiostation/>
42    pub async fn update_internet_radio_station(
43        &self,
44        id: &str,
45        stream_url: &str,
46        name: &str,
47        home_page_url: Option<&str>,
48    ) -> Result<(), Error> {
49        let mut params = vec![("id", id), ("streamUrl", stream_url), ("name", name)];
50        if let Some(hp) = home_page_url {
51            params.push(("homepageUrl", hp));
52        }
53        self.get_response("updateInternetRadioStation", &params)
54            .await?;
55        Ok(())
56    }
57
58    /// Delete an internet radio station.
59    ///
60    /// See <https://opensubsonic.netlify.app/docs/endpoints/deleteinternetradiostation/>
61    pub async fn delete_internet_radio_station(&self, id: &str) -> Result<(), Error> {
62        self.get_response("deleteInternetRadioStation", &[("id", id)])
63            .await?;
64        Ok(())
65    }
66}