spotify_web_api/api/audiobooks/
check_user_saved_audiobooks.rs

1use crate::api::prelude::*;
2
3/// Check if one or more audiobooks are already saved in the current Spotify user's library.
4#[derive(Debug, Clone)]
5pub struct CheckUserSavedAudiobooks {
6    /// A list of [Spotify IDs](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the audiobooks.
7    pub ids: Vec<String>,
8}
9
10impl<T, I> From<I> for CheckUserSavedAudiobooks
11where
12    I: IntoIterator<Item = T>,
13    T: Into<String>,
14{
15    fn from(ids: I) -> Self {
16        Self {
17            ids: ids.into_iter().map(Into::into).collect(),
18        }
19    }
20}
21
22impl Endpoint for CheckUserSavedAudiobooks {
23    fn method(&self) -> Method {
24        Method::GET
25    }
26
27    fn endpoint(&self) -> Cow<'static, str> {
28        "me/audiobooks/contains".into()
29    }
30
31    fn parameters(&self) -> QueryParams<'_> {
32        let mut params = QueryParams::default();
33        params.push("ids", &self.ids.join(","));
34        params
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use crate::{
42        api::Query as _,
43        test::client::{ExpectedUrl, SingleTestClient},
44    };
45
46    #[test]
47    fn test_check_user_saved_audiobooks_endpoint() {
48        let endpoint = ExpectedUrl::builder()
49            .endpoint("me/audiobooks/contains")
50            .add_query_params(&[(
51                "ids",
52                "18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ,7iHfbu1YPACw6oZPAFJtqe",
53            )])
54            .build();
55
56        let expected_response = [false, false, false];
57
58        let client = SingleTestClient::new_json(endpoint, &expected_response);
59
60        let endpoint = CheckUserSavedAudiobooks::from([
61            "18yVqkdbdRvS24c0Ilj2ci",
62            "1HGw3J3NxZO1TP1BTtVhpZ",
63            "7iHfbu1YPACw6oZPAFJtqe",
64        ]);
65
66        let result: Vec<bool> = endpoint.query(&client).unwrap();
67
68        assert_eq!(result, expected_response);
69    }
70}