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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Endpoint functions related to controlling what is playing on the current user's Spotify account. (Beta)
//!
//! All endpoints in here are in Beta, and so are more likely to break.
//!
//! The `device_id` parameter seen in this module is the device to perform the request on. If not
//! specified, it will default to the current user's currenttly active device.

use crate::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Get the current user's available devices (Beta).
///
/// Requires `user-read-playback-state`
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/get-a-users-available-devices/).
pub async fn get_devices(token: &AccessToken) -> Result<Vec<Device>, EndpointError<PlayerError>> {
    #[derive(Deserialize)]
    struct Devices {
        devices: Vec<Device>,
    }

    Ok(request!(
        token,
        GET "/v1/me/player/devices",
        ret = Devices
    )
    .devices)
}

/// Get information about the current user's current playback (Beta).
///
/// Requires `user-read-playback-state`. Returns None if nothing is currently playing.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/get-information-about-the-users-current-playback/).
pub async fn get_playback(
    token: &AccessToken,
    market: Option<Market>,
) -> Result<Option<CurrentPlayback>, EndpointError<PlayerError>> {
    Ok(Some(request!(
        token,
        GET "/v1/me/player",
        optional_query_params = {"market": market.map(|m| m.as_str())},
        ret = CurrentPlayback,
        or_else = None
    )))
}

/// Get current user's recently played tracks (Beta).
///
/// Note that a track needs to be played for >30seconds to be included in the play history.
/// Requires `user-read-recently-played`. Will return None if a private session is enabled.
///
/// `after` and `before` are Cursor values given the previous time this endpoint was called, to
/// move forward or back in time respectively. Both `after` and `before` must _not_ be Some.
/// `after` is a Unix milliseconds timestamp, and will return everything played after that
/// position, `before` is the same but returns everything before that position.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/get-recently-played/).
pub async fn get_recently_played(
    token: &AccessToken,
    limit: usize,
    after: Option<String>,
    before: Option<String>,
) -> Result<Option<TwoWayCursorPage<PlayHistory>>, EndpointError<PlayerError>> {
    Ok(Some(request!(
        token,
        GET "/v1/me/player/recently-played",
        query_params = {"limit": limit.to_string()},
        optional_query_params = {"after": after, "before": before},
        ret = TwoWayCursorPage<PlayHistory>,
        or_else = None
    )))
}

/// Get the current user's currently playing track (Beta).
///
/// Requires `user-read-currently-playing` and/or `user-read-playback-state`. Returns None if no
/// available devices are found, no tracks are playing, or a private session is enabled.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/get-the-users-currently-playing-track/).
pub async fn get_playing_track(
    token: &AccessToken,
    market: Option<Market>,
) -> Result<Option<CurrentlyPlaying>, EndpointError<PlayerError>> {
    Ok(Some(request!(
        token,
        GET "/v1/me/player/currently-playing",
        optional_query_params = {"market": market.map(|m| m.as_str())},
        ret = CurrentlyPlaying,
        or_else = None
    )))
}

/// Pause the current user's playback (Beta).
///
/// Requires `user-modify-playback-state`. This action completes asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/pause-a-users-playback/).
pub async fn pause(
    token: &AccessToken,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player/pause",
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Seek to position in currently playing track (Beta).
///
/// Requires `user-modify-playback-state`. This action completes asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/seek-to-position-in-currently-playing-track/).
pub async fn seek(
    token: &AccessToken,
    position: Duration,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player/seek",
        query_params = {"position_ms": position.as_millis().to_string()},
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Set repeat mode on current playback (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/set-repeat-mode-on-users-playback/).
pub async fn set_repeat(
    token: &AccessToken,
    state: RepeatState,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player/repeat",
        query_params = {"state": state.as_str()},
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Set volume on current playback (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// `volume_percent` is the volume as a percentage, from 0 to 100 inclusive.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/set-volume-for-users-playback/).
pub async fn set_volume(
    token: &AccessToken,
    volume_percent: i32,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player/volume",
        query_params = {"volume_percent": volume_percent.to_string()},
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Skip to next track (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// After a successful skip operation, playback will automatically start.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/skip-users-playback-to-next-track/).
pub async fn skip_next(
    token: &AccessToken,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        POST "/v1/me/player/next",
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Skip to previous track (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// After a successful skip operation, playback will automatically start. This action will always
/// skip to the previous track, regardless of the current track's progress; to go to the start of
/// the track, use `seek`.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/skip-users-playback-to-previous-track/).
pub async fn skip_prev(
    token: &AccessToken,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        POST "/v1/me/player/previous",
        optional_query_params = {"device_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Request to play something.
///
/// It either plays from a context (playlist, album or artist and track is not allowed) with a
/// specified 0-indexed offset to start playing at, or plays a list of tracks.
#[derive(Debug, Clone)]
pub enum Play<'s, 'i> {
    Context(ItemType, &'i str, usize),
    Tracks(&'s [&'s str]),
}

/// Start or resume playback (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// `play`, when set, controls what to play, and what offset in the context to start playing at.
/// `position` controls how far into the current track to play; if it is longer than the current
/// track, then the next track will play.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/start-a-users-playback/).
pub async fn play(
    token: &AccessToken,
    play: Option<Play<'_, '_>>,
    position: Option<Duration>,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    #[derive(Serialize)]
    struct Offset {
        position: usize,
    }

    #[derive(Serialize)]
    struct Body {
        context_uri: Option<String>,
        offset: Option<Offset>,
        uris: Option<Vec<String>>,
        position_ms: Option<u128>,
    }

    let mut body = Body {
        context_uri: None,
        offset: None,
        uris: None,
        position_ms: position.map(|duration| duration.as_millis()),
    };

    if let Some(play) = play {
        match play {
            Play::Context(context_type, id, position) => {
                body.context_uri = Some(format!("spotify:{}:{}", context_type.as_str(), id));
                body.offset = Some(Offset { position });
            }
            Play::Tracks(ids) => {
                body.uris = Some(ids.iter().map(|s| format!("spotify:track:{}", s)).collect());
            }
        }
    }

    request!(
        token,
        PUT "/v1/me/player/play",
        optional_query_params = {"device_id": device_id},
        body = serde_json::to_string(&body)?
    );
    Ok(())
}

/// Enable or disable shuffle (Beta).
///
/// Requires `user-modify-playback-state`. This action complete asynchronously, meaning you will
/// not know if it succeeded unless you check.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/toggle-shuffle-for-users-playback/).
pub async fn set_shuffle(
    token: &AccessToken,
    shuffle: bool,
    device_id: Option<&str>,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player/shuffle",
        query_params = {"state": if shuffle {"true"} else {"false"}},
        optional_query_params = {"deivce_id": device_id},
        body = "{}"
    );
    Ok(())
}

/// Transfer playback to another device (Beta).
///
/// Requires `user-modify-playback-state`. When `play == true`, playback will happen on the new
/// device. When `play == false`, playback will continue in its current state.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/player/transfer-a-users-playback/).
pub async fn transfer(
    token: &AccessToken,
    id: &str,
    play: bool,
) -> Result<(), EndpointError<PlayerError>> {
    request!(
        token,
        PUT "/v1/me/player",
        body = format!(r#"{{"device_ids":["{}"],"play":{}}}"#, id, play)
    );
    Ok(())
}

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

    #[tokio::test]
    async fn test() {
        let token = token().await;

        let mut devices = get_devices(&token).await.unwrap().into_iter();
        let device = loop {
            let device = devices
                .next()
                .expect("You must have at least one usable device for this test to work.");
            if !device.is_restricted && device.id.is_some() && !device.is_private_session {
                break device;
            }
        };
        let id = &device.id.as_ref().unwrap();
        if !device.is_active {
            println!("Transferring device to {}...", device.name);
            transfer(&token, id, false).await.unwrap();
        }

        // Time to wait to assume that the operation has completed
        let wait_time = Duration::from_millis(300);

        // Play 10 seconds into the 3rd track from RELAXER
        play(
            &token,
            Some(Play::Context(ItemType::Album, "3lBPyXvg1hhoJ1REnw80fZ", 2)),
            Some(Duration::from_secs(10)),
            None,
        )
        .await
        .unwrap();
        time::delay_for(wait_time).await;
        let playback = get_playback(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playback.device.id, device.id);
        assert_eq!(playback.device.name, device.name);
        assert_eq!(playback.device.device_type, device.device_type);
        assert_eq!(playback.device.volume_percent, device.volume_percent);
        let context = playback.currently_playing.context.unwrap();
        assert_eq!(context.context_type, ItemType::Album);
        assert_eq!(context.id, "3lBPyXvg1hhoJ1REnw80fZ");
        assert!(playback.currently_playing.progress.unwrap() >= Duration::from_secs(10));
        assert!(playback.currently_playing.is_playing);
        let track = &playback.currently_playing.item.unwrap();
        assert_eq!(track.album.id, "3lBPyXvg1hhoJ1REnw80fZ");
        assert_eq!(track.track_number, 3);
        assert_eq!(
            playback.currently_playing.currently_playing_type,
            TrackType::Track
        );

        // Play "I am a Paleontologist" and "Ten Tonne Skeleton"
        play(
            &token,
            Some(Play::Tracks(&[
                "2Wbz0QcXCVYmuBgOwUV6KU",
                "0vjYxBDAcflD0358arIVZG",
            ])),
            None,
            None,
        )
        .await
        .unwrap();
        time::delay_for(wait_time).await;
        let playing = get_playing_track(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert!(playing.progress.unwrap() < Duration::from_secs(2));
        assert!(playing.is_playing);
        let track = &playing.item.unwrap();
        assert_eq!(track.id, "2Wbz0QcXCVYmuBgOwUV6KU");
        assert_eq!(playing.currently_playing_type, TrackType::Track);

        // Seek to 2ms before end
        seek(&token, Duration::from_millis(152106 - 2), None)
            .await
            .unwrap();
        time::delay_for(wait_time).await;
        let playing = get_playing_track(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playing.item.unwrap().id, "0vjYxBDAcflD0358arIVZG");

        // Repeat, shuffle, volume
        set_repeat(&token, RepeatState::Track, None).await.unwrap();
        set_shuffle(&token, true, None).await.unwrap();
        set_volume(&token, 17, None).await.unwrap();
        time::delay_for(wait_time).await;
        let playback = get_playback(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playback.repeat_state, RepeatState::Track);
        assert_eq!(playback.shuffle_state, true);
        assert_eq!(playback.device.volume_percent.unwrap(), 17);
        set_repeat(&token, RepeatState::Context, None)
            .await
            .unwrap();
        set_shuffle(&token, false, None).await.unwrap();
        set_volume(&token, 73, None).await.unwrap();
        time::delay_for(wait_time).await;
        let playback = get_playback(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playback.repeat_state, RepeatState::Context);
        assert_eq!(playback.shuffle_state, false);
        assert_eq!(playback.device.volume_percent.unwrap(), 73);

        // Skip previous
        skip_prev(&token, None).await.unwrap();
        time::delay_for(wait_time).await;
        let playing = get_playing_track(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playing.item.unwrap().id, "2Wbz0QcXCVYmuBgOwUV6KU");

        // Skip next
        skip_next(&token, None).await.unwrap();
        time::delay_for(wait_time).await;
        let playing = get_playing_track(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert_eq!(playing.item.unwrap().id, "0vjYxBDAcflD0358arIVZG");

        // Pause
        pause(&token, None).await.unwrap();
        time::delay_for(wait_time).await;
        let playback = get_playback(&token, Some(Market::FromToken))
            .await
            .unwrap()
            .unwrap();
        assert!(!playback.currently_playing.is_playing);
    }

    #[tokio::test]
    async fn test_recent() {
        get_recently_played(&token().await, 3, None, None)
            .await
            .unwrap();
    }
}