pcloud/stream/
audio.rs

1use super::StreamingLinkList;
2use crate::file::FileIdentifier;
3
4/// Parameters for retrieving an audio file link, including options for controlling the audio bit rate and download behavior.
5#[derive(Debug, Default, serde::Serialize)]
6pub struct GetAudioLinkParams {
7    /// Audio bit rate in kilobits per second (from 16 to 320).
8    #[serde(rename = "abitrate", skip_serializing_if = "Option::is_none")]
9    pub audio_bit_rate: Option<u16>,
10
11    /// Flag to force the audio file to be downloaded rather than streamed.
12    #[serde(
13        rename = "forcedownload",
14        skip_serializing_if = "crate::request::is_false",
15        serialize_with = "crate::request::serialize_bool"
16    )]
17    force_download: bool,
18}
19
20impl GetAudioLinkParams {
21    /// Sets the audio bit rate for the link.
22    ///
23    /// # Arguments
24    ///
25    /// * `value` - Audio bit rate in kilobits per second (must be between 16 and 320).
26    pub fn set_audio_bit_rate(&mut self, value: u16) {
27        self.audio_bit_rate = Some(value);
28    }
29
30    /// Sets the audio bit rate and returns the updated `GetAudioLinkParams` object.
31    ///
32    /// # Arguments
33    ///
34    /// * `value` - Audio bit rate in kilobits per second (must be between 16 and 320).
35    pub fn with_audio_bit_rate(mut self, value: u16) -> Self {
36        self.set_audio_bit_rate(value);
37        self
38    }
39
40    /// Sets the `force_download` flag.
41    ///
42    /// # Arguments
43    ///
44    /// * `value` - Boolean indicating whether the file should be forced to download.
45    pub fn set_force_download(&mut self, value: bool) {
46        self.force_download = value;
47    }
48
49    /// Sets the `force_download` flag and returns the updated `GetAudioLinkParams` object.
50    ///
51    /// # Arguments
52    ///
53    /// * `value` - Boolean indicating whether the file should be forced to download.
54    pub fn with_force_download(mut self, value: bool) -> Self {
55        self.set_force_download(value);
56        self
57    }
58}
59
60/// Struct representing the parameters used when making a request to get an audio link.
61#[derive(serde::Serialize)]
62struct Params<'a> {
63    /// The identifier for the file being requested.
64    #[serde(flatten)]
65    identifier: FileIdentifier<'a>,
66
67    /// The parameters controlling how the audio link should be generated.
68    #[serde(flatten)]
69    params: GetAudioLinkParams,
70}
71
72impl crate::Client {
73    /// Gets an audio link using only the file identifier, without additional parameters.
74    ///
75    /// # Arguments
76    ///
77    /// * `identifier` - The identifier of the file.
78    ///
79    /// # Returns
80    ///
81    /// A result containing the `StreamingLinkList` with the generated audio links.
82    pub async fn get_audio_link(
83        &self,
84        identifier: impl Into<FileIdentifier<'_>>,
85    ) -> crate::Result<StreamingLinkList> {
86        self.get_request::<StreamingLinkList, _>("getaudiolink", identifier.into())
87            .await
88    }
89
90    /// Gets an audio link using both the file identifier and additional parameters.
91    ///
92    /// # Arguments
93    ///
94    /// * `identifier` - The identifier of the file.
95    /// * `params` - The parameters to customize the audio link (e.g., audio bit rate, force download).
96    ///
97    /// # Returns
98    ///
99    /// A result containing the `StreamingLinkList` with the generated audio links.
100    pub async fn get_audio_link_with_params(
101        &self,
102        identifier: impl Into<FileIdentifier<'_>>,
103        params: GetAudioLinkParams,
104    ) -> crate::Result<StreamingLinkList> {
105        self.get_request::<StreamingLinkList, _>(
106            "getaudiolink",
107            Params {
108                identifier: identifier.into(),
109                params,
110            },
111        )
112        .await
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use crate::{Client, Credentials};
119    use mockito::Matcher;
120
121    #[tokio::test]
122    async fn success() {
123        let mut server = mockito::Server::new_async().await;
124        let m = server.mock("GET", "/getaudiolink")
125            .match_query(Matcher::AllOf(vec![
126                Matcher::UrlEncoded("access_token".into(), "access-token".into()),
127                Matcher::UrlEncoded("fileid".into(), "42".into()),
128            ]))
129            .with_status(200)
130            .with_body(r#"{
131        "result": 0,
132        "dwltag": "yvkNr0TqT6HFAWlVpdnHs5",
133        "hash": 17869736033964340520,
134        "size": 10485760,
135        "expires": "Sat, 24 Jul 2021 03:18:31 +0000",
136        "path": "\/DLZCAt2vXZejNfL5ZruLVZZTk2ev7Z2ZZNR5ZZdoz6ZXZQZZErw4bH0PfzBQt3LlgXMliXVtietX\/SAkdyBjkA7mQABbT.bin",
137        "hosts": [
138                "edef2.pcloud.com",
139                "eu3.pcloud.com"
140        ]
141}"#)
142.create();
143        let client = Client::new(server.url(), Credentials::access_token("access-token")).unwrap();
144        let result = client.get_audio_link(42).await.unwrap();
145        let mut iter = result.links();
146        assert_eq!(iter.next().unwrap().to_string(), "https://edef2.pcloud.com/DLZCAt2vXZejNfL5ZruLVZZTk2ev7Z2ZZNR5ZZdoz6ZXZQZZErw4bH0PfzBQt3LlgXMliXVtietX/SAkdyBjkA7mQABbT.bin");
147        m.assert();
148    }
149}