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
//! Endpoint functions related to tracks and audio analysis.

use crate::*;
use serde::Deserialize;

/// Get audio analysis for a track.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-analysis/).
pub async fn get_audio_analysis_track(
    token: &AccessToken,
    id: &str,
) -> Result<AudioAnalysis, EndpointError<Error>> {
    Ok(request!(
        token,
        GET "/v1/audio-analysis/{}",
        path_params = [id],
        ret = AudioAnalysis
    ))
}

/// Get audio features for a track.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-features/).
pub async fn get_audio_features_track(
    token: &AccessToken,
    id: &str,
) -> Result<AudioFeatures, EndpointError<Error>> {
    Ok(request!(
        token,
        GET "/v1/audio-features/{}",
        path_params = [id],
        ret = AudioFeatures
    ))
}

/// Get audio features for several tracks.
///
/// Maximum 100 ids.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-audio-features/).
pub async fn get_audio_features_tracks(
    token: &AccessToken,
    ids: &[&str],
) -> Result<Vec<AudioFeatures>, EndpointError<Error>> {
    #[derive(Deserialize)]
    struct ManyAudioFeatures {
        audio_features: Vec<AudioFeatures>,
    }

    Ok(request!(
        token,
        GET "/v1/audio-features",
        query_params = {"ids": ids.join(",")},
        ret = ManyAudioFeatures
    )
    .audio_features)
}

/// Get information about several tracks.
///
/// Maximum 50 ids.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-tracks/).
pub async fn get_tracks(
    token: &AccessToken,
    ids: &[&str],
    market: Option<Market>,
) -> Result<Vec<Track>, EndpointError<Error>> {
    #[derive(Deserialize)]
    struct Tracks {
        tracks: Vec<Track>,
    };

    Ok(request!(
        token,
        GET "/v1/tracks",
        query_params = {"ids": ids.join(",")},
        optional_query_params = {"market": market.map(|m| m.as_str())},
        ret = Tracks
    )
    .tracks)
}

/// Get information about a track.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/tracks/get-several-tracks/).
pub async fn get_track(
    token: &AccessToken,
    id: &str,
    market: Option<Market>,
) -> Result<Track, EndpointError<Error>> {
    Ok(request!(
        token,
        GET "/v1/tracks/{}",
        path_params = [id],
        optional_query_params = {"market": market.map(|m| m.as_str())},
        ret = Track
    ))
}

#[cfg(test)]
mod tests {
    use crate::endpoints::token;
    use crate::*;

    #[tokio::test]
    async fn test_get_track() {
        // "Walk Like an Egyptian"
        let track = get_track(&token().await, "1Jwc3ODLQxtbnS8M9TflSP", None)
            .await
            .unwrap();
        assert_eq!(track.id, "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 = get_tracks(
            &token().await,
            &["1Jwc3ODLQxtbnS8M9TflSP", "5uuJruktM9fMdN9Va0DUMl"],
            None,
        )
        .await
        .unwrap();
        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 = get_track(
            &token().await,
            "6kLCHFM39wkFjOuyPGLGeQ",
            Some(Market::Country(CountryCode::USA)),
        )
        .await
        .unwrap();
        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"
        get_audio_analysis_track(&token().await, "1Jwc3ODLQxtbnS8M9TflSP")
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn test_features() {
        // Get features of "Walk Like an Egyptian"
        let features = get_audio_features_track(&token().await, "1Jwc3ODLQxtbnS8M9TflSP")
            .await
            .unwrap();
        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 = get_audio_features_tracks(
            &token().await,
            &["1Jwc3ODLQxtbnS8M9TflSP", "5uuJruktM9fMdN9Va0DUMl"],
        )
        .await
        .unwrap();
        assert_eq!(features.len(), 2);
        assert_eq!(features[0].id, "1Jwc3ODLQxtbnS8M9TflSP");
        assert_eq!(features[1].id, "5uuJruktM9fMdN9Va0DUMl");
    }
}