1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::fmt::Display;

use itertools::Itertools;
use serde::Deserialize;

use super::chunked_sequence;
use crate::{AudioAnalysis, AudioFeatures, Client, Error, Market, Response, Track};

/// Endpoint functions related to tracks and audio analysis.
#[derive(Debug, Clone, Copy)]
pub struct Tracks<'a>(pub &'a Client);

impl Tracks<'_> {
    /// Get audio analysis for a track.
    ///
    /// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-analysis/).
    pub async fn get_analysis(self, id: &str) -> Result<Response<AudioAnalysis>, Error> {
        self.0
            .send_json(self.0.client.get(endpoint!("/v1/audio-analysis/{}", id)))
            .await
    }

    /// Get audio features for a track.
    ///
    /// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-features/).
    pub async fn get_features_track(self, id: &str) -> Result<Response<AudioFeatures>, Error> {
        self.0
            .send_json(self.0.client.get(endpoint!("/v1/audio-features/{}", id)))
            .await
    }

    /// Get audio features for several tracks.
    ///
    /// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-audio-features/).
    pub async fn get_features_tracks<I: Iterator>(
        self,
        ids: impl IntoIterator<IntoIter = I, Item = I::Item>,
    ) -> Result<Response<Vec<AudioFeatures>>, Error>
    where
        I::Item: Display,
    {
        #[derive(Deserialize)]
        struct ManyAudioFeatures {
            audio_features: Vec<AudioFeatures>,
        }

        chunked_sequence(&ids.into_iter().chunks(100), |mut ids| async move {
            Ok(self
                .0
                .send_json::<ManyAudioFeatures>(
                    self.0
                        .client
                        .get(endpoint!("/v1/audio-features"))
                        .query(&(("ids", ids.join(",")),)),
                )
                .await?
                .map(|res| res.audio_features))
        })
        .await
    }

    /// Get information about several tracks.
    ///
    /// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-tracks/).
    pub async fn get_tracks<I: Iterator>(
        self,
        ids: impl IntoIterator<IntoIter = I, Item = I::Item>,
        market: Option<Market>,
    ) -> Result<Response<Vec<Track>>, Error>
    where
        I::Item: Display,
    {
        #[derive(Deserialize)]
        struct Tracks {
            tracks: Vec<Track>,
        };

        chunked_sequence(&ids.into_iter().chunks(50), |mut ids| async move {
            Ok(self
                .0
                .send_json::<Tracks>(
                    self.0
                        .client
                        .get(endpoint!("/v1/tracks"))
                        .query(&(("ids", ids.join(",")), market.map(Market::query))),
                )
                .await?
                .map(|res| res.tracks))
        })
        .await
    }

    /// Get information about a track.
    ///
    /// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-tracks/).
    pub async fn get_track(
        self,
        id: &str,
        market: Option<Market>,
    ) -> Result<Response<Track>, Error> {
        self.0
            .send_json(
                self.0
                    .client
                    .get(endpoint!("/v1/tracks/{}", id))
                    .query(&(market.map(Market::query),)),
            )
            .await
    }
}

#[cfg(test)]
mod tests {
    use isocountry::CountryCode;

    use crate::endpoints::client;
    use crate::{Market, Mode};

    #[tokio::test]
    async fn test_get_track() {
        // "Walk Like an Egyptian"
        let track = client()
            .tracks()
            .get_track("1Jwc3ODLQxtbnS8M9TflSP", None)
            .await
            .unwrap()
            .data;
        assert_eq!(track.id.unwrap(), "1Jwc3ODLQxtbnS8M9TflSP");
        assert_eq!(track.name, "Walk Like an Egyptian");
        assert_eq!(track.artists[0].name, "The Bangles");
    }

    #[tokio::test]
    async fn test_get_tracks() {
        // "Walk Like an Egyptian", "Play that Funky Music"
        let tracks = client()
            .tracks()
            .get_tracks(&["1Jwc3ODLQxtbnS8M9TflSP", "5uuJruktM9fMdN9Va0DUMl"], None)
            .await
            .unwrap()
            .data;
        assert_eq!(tracks.len(), 2);
        assert_eq!(tracks[0].name, "Walk Like an Egyptian");
        assert_eq!(tracks[1].name, "Play That Funky Music");
    }

    #[tokio::test]
    async fn test_relink() {
        // Test track relinking with "Heaven and Hell"
        let relinked = client()
            .tracks()
            .get_track(
                "6kLCHFM39wkFjOuyPGLGeQ",
                Some(Market::Country(CountryCode::USA)),
            )
            .await
            .unwrap()
            .data;
        assert_eq!(relinked.name, "Heaven and Hell");
        assert!(relinked.is_playable.unwrap());
        let from = relinked.linked_from.as_ref().unwrap();
        assert_eq!(from.id, "6kLCHFM39wkFjOuyPGLGeQ");
    }

    #[tokio::test]
    async fn test_analysis() {
        // Get analysis of "Walk Like an Egyptian"
        client()
            .tracks()
            .get_analysis("1Jwc3ODLQxtbnS8M9TflSP")
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_features() {
        // Get features of "Walk Like an Egyptian"
        let features = client()
            .tracks()
            .get_features_track("1Jwc3ODLQxtbnS8M9TflSP")
            .await
            .unwrap()
            .data;
        assert_eq!(features.id, "1Jwc3ODLQxtbnS8M9TflSP");
        assert_eq!(features.key, 11);
        assert_eq!(features.mode, Mode::Major);
        assert_eq!(features.tempo, 103.022);
    }

    #[tokio::test]
    async fn test_features_tracks() {
        // Get features of "Walk Like an Egyptian" and "Play that Funky Music"
        let features = client()
            .tracks()
            .get_features_tracks(&["1Jwc3ODLQxtbnS8M9TflSP", "5uuJruktM9fMdN9Va0DUMl"])
            .await
            .unwrap()
            .data;
        assert_eq!(features.len(), 2);
        assert_eq!(features[0].id, "1Jwc3ODLQxtbnS8M9TflSP");
        assert_eq!(features[1].id, "5uuJruktM9fMdN9Va0DUMl");
    }
}