1use super::StreamingLinkList;
2use crate::file::FileIdentifier;
3
4#[derive(Debug, Default, serde::Serialize)]
6pub struct GetAudioLinkParams {
7 #[serde(rename = "abitrate", skip_serializing_if = "Option::is_none")]
9 pub audio_bit_rate: Option<u16>,
10
11 #[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 pub fn set_audio_bit_rate(&mut self, value: u16) {
27 self.audio_bit_rate = Some(value);
28 }
29
30 pub fn with_audio_bit_rate(mut self, value: u16) -> Self {
36 self.set_audio_bit_rate(value);
37 self
38 }
39
40 pub fn set_force_download(&mut self, value: bool) {
46 self.force_download = value;
47 }
48
49 pub fn with_force_download(mut self, value: bool) -> Self {
55 self.set_force_download(value);
56 self
57 }
58}
59
60#[derive(serde::Serialize)]
62struct Params<'a> {
63 #[serde(flatten)]
65 identifier: FileIdentifier<'a>,
66
67 #[serde(flatten)]
69 params: GetAudioLinkParams,
70}
71
72impl crate::Client {
73 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 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}