spotify_web_api/api/albums/
check_user_saved_albums.rs

1use crate::api::prelude::*;
2
3/// Check if one or more albums is already saved in the current Spotify user's 'Your Music' library.
4#[derive(Debug, Clone)]
5pub struct CheckUserSavedAlbums {
6    /// A list of [Spotify IDs](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids) for the albums.
7    pub ids: Vec<String>,
8}
9
10impl<T, I> From<I> for CheckUserSavedAlbums
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 CheckUserSavedAlbums {
23    fn method(&self) -> Method {
24        Method::GET
25    }
26
27    fn endpoint(&self) -> Cow<'static, str> {
28        "me/albums/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_albums_endpoint() {
48        let endpoint = ExpectedUrl::builder()
49            .endpoint("me/albums/contains")
50            .add_query_params(&[(
51                "ids",
52                "382ObEPsp2rxGrnsizN5TX,1A2GTWGtFfWp7KSQTwWOyo,2noRn2Aes5aoNVsU6iWThc",
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 = CheckUserSavedAlbums::from([
61            "382ObEPsp2rxGrnsizN5TX",
62            "1A2GTWGtFfWp7KSQTwWOyo",
63            "2noRn2Aes5aoNVsU6iWThc",
64        ]);
65
66        let result: Vec<bool> = endpoint.query(&client).unwrap();
67
68        assert_eq!(result, expected_response);
69    }
70}