Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish the pagination implementation #201

Merged
merged 13 commits into from
Apr 22, 2021
155 changes: 131 additions & 24 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,31 @@ impl Spotify {
/// - limit - the number of albums to return
/// - offset - the index of the first album to return
///
/// See [`Spotify::artist_albums_manual`] for a manually paginated version
/// of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-an-artists-albums)
pub fn artist_albums<'a>(
&'a self,
artist_id: &'a ArtistId,
album_type: Option<AlbumType>,
market: Option<&'a Market>,
) -> impl Paginator<ClientResult<SimplifiedAlbum>> + 'a {
paginate(
move |limit, offset| {
self.artist_albums_manual(artist_id, album_type, market, Some(limit), Some(offset))
},
50,
)
}

/// The manually paginated version of [`Spotify::artist_albums`].
#[maybe_async]
pub async fn artist_albums(
pub async fn artist_albums_manual(
&self,
artist_id: &ArtistId,
album_type: Option<AlbumType>,
market: Option<Market>,
market: Option<&Market>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had to make some parameters a reference, like Market, because when using automatic pagination we iterate multiple times and would have to clone the parameter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense. As I mentioned in another PR, it might be a chance to pass parameters by reference

limit: Option<u32>,
offset: Option<u32>,
) -> ClientResult<Page<SimplifiedAlbum>> {
Expand Down Expand Up @@ -388,9 +406,23 @@ impl Spotify {
/// - limit - the number of items to return
/// - offset - the index of the first item to return
///
/// See [`Spotify::album_track_manual`] for a manually paginated version of
/// this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-an-albums-tracks)
pub fn album_track<'a, L: Into<Option<u32>>, O: Into<Option<u32>>>(
&'a self,
album_id: &'a AlbumId,
) -> impl Paginator<ClientResult<SimplifiedTrack>> + 'a {
paginate(
move |limit, offset| self.album_track_manual(album_id, Some(limit), Some(offset)),
50,
)
}

/// The manually paginated version of [`Spotify::album_track`].
#[maybe_async]
pub async fn album_track<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn album_track_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
album_id: &AlbumId,
limit: L,
Expand Down Expand Up @@ -452,9 +484,22 @@ impl Spotify {
/// - limit - the number of items to return
/// - offset - the index of the first item to return
///
/// See [`Spotify::current_user_playlists_manual`] for a manually paginated
/// version of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-a-list-of-current-users-playlists)
pub fn current_user_playlists<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
) -> impl Paginator<ClientResult<SimplifiedPlaylist>> + '_ {
paginate(
move |limit, offset| self.current_user_playlists_manual(Some(limit), Some(offset)),
50,
)
}

/// The manually paginated version of [`Spotify::current_user_playlists`].
#[maybe_async]
pub async fn current_user_playlists<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn current_user_playlists_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
limit: L,
offset: O,
Expand All @@ -476,9 +521,23 @@ impl Spotify {
/// - limit - the number of items to return
/// - offset - the index of the first item to return
///
/// See [`Spotify::user_playlists_manual`] for a manually paginated version
/// of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-list-users-playlists)
pub fn user_playlists<'a, L: Into<Option<u32>>, O: Into<Option<u32>>>(
&'a self,
user_id: &'a UserId,
) -> impl Paginator<ClientResult<SimplifiedPlaylist>> + 'a {
paginate(
move |limit, offset| self.user_playlists_manual(user_id, Some(limit), Some(offset)),
50,
)
}

/// The manually paginated version of [`Spotify::user_playlists`].
#[maybe_async]
pub async fn user_playlists<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn user_playlists_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
user_id: &UserId,
limit: L,
Expand Down Expand Up @@ -536,15 +595,33 @@ impl Spotify {
/// - offset - the index of the first track to return
/// - market - an ISO 3166-1 alpha-2 country code or the string from_token.
///
/// See [`Spotify::playlist_tracks`] for a manually paginated version of
/// this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-playlists-tracks)
pub fn playlist_tracks<'a, L: Into<Option<u32>>, O: Into<Option<u32>>>(
&'a self,
playlist_id: &'a PlaylistId,
fields: Option<&'a str>,
market: Option<&'a Market>,
) -> impl Paginator<ClientResult<PlaylistItem>> + 'a {
paginate(
move |limit, offset| {
self.playlist_tracks_manual(playlist_id, fields, market, Some(limit), Some(offset))
},
50,
)
}

/// The manually paginated version of [`Spotify::playlist_tracks`].
#[maybe_async]
pub async fn playlist_tracks<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn playlist_tracks_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
playlist_id: &PlaylistId,
fields: Option<&str>,
market: Option<&Market>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I was at it I made these parameters consistent; limit and offset always go last.

limit: L,
offset: O,
market: Option<Market>,
) -> ClientResult<Page<PlaylistItem>> {
let mut params = Query::with_capacity(2);
let limit = limit.into().unwrap_or(50).to_string();
Expand Down Expand Up @@ -911,14 +988,29 @@ impl Spotify {
/// - offset - the index of the first album to return
/// - market - Provide this parameter if you want to apply Track Relinking.
///
/// See [`Spotify::current_user_saved_albums`] for a manually paginated
/// version of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-saved-albums)
pub fn current_user_saved_albums<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
) -> impl Paginator<ClientResult<SavedAlbum>> + '_ {
paginate(
move |limit, offset| self.current_user_saved_albums_manual(Some(limit), Some(offset)),
50,
)
}

/// The manually paginated version of [`Spotify::current_user_saved_albums`].
#[maybe_async]
pub async fn current_user_saved_albums<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn current_user_saved_albums_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
limit: L,
offset: O,
) -> ClientResult<Page<SavedAlbum>> {
let mut params = Query::with_capacity(2);
// TODO: we should use the API's default value instead of
// `.unwrap_or(20)` and similars.
let limit = limit.into().unwrap_or(20).to_string();
let offset = offset.into().unwrap_or(0).to_string();
params.insert("limit", &limit);
Expand All @@ -935,12 +1027,21 @@ impl Spotify {
/// - offset - the index of the first track to return
/// - market - Provide this parameter if you want to apply Track Relinking.
///
/// See [`Spotify::current_user_saved_tracks_auto`] for an automatically
/// See [`Spotify::current_user_saved_tracks_manual`] for a manually
/// paginated version of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-saved-tracks)
pub fn current_user_saved_tracks(&self) -> impl Paginator<ClientResult<SavedTrack>> + '_ {
paginate(
move |limit, offset| self.current_user_saved_tracks_manual(limit, offset),
50,
)
}

/// The manually paginated version of
/// [`Spotify::current_user_saved_tracks`].
#[maybe_async]
pub async fn current_user_saved_tracks<L: Into<Option<u32>>, O: Into<Option<u32>>>(
pub async fn current_user_saved_tracks_manual<L: Into<Option<u32>>, O: Into<Option<u32>>>(
&self,
limit: L,
offset: O,
Expand All @@ -954,15 +1055,6 @@ impl Spotify {
self.convert_result(&result)
}

/// The automatically paginated version of
/// [`Spotify::current_user_saved_tracks`].
pub fn current_user_saved_tracks_auto(&self) -> impl Paginator<ClientResult<SavedTrack>> + '_ {
paginate(
move |limit, offset| self.current_user_saved_tracks(limit, offset),
50,
)
}

/// Gets a list of the artists followed by the current authorized user.
///
/// Parameters:
Expand Down Expand Up @@ -1047,22 +1139,37 @@ impl Spotify {
/// - offset - the index of the first entity to return
/// - time_range - Over what time frame are the affinities computed
///
/// See [`Spotify::current_user_top_artists_manual`] for a manually
/// paginated version of this.
///
/// [Reference](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-users-top-artists-and-tracks)
pub fn current_user_top_artists<'a>(
&'a self,
time_range: Option<&'a TimeRange>,
) -> impl Paginator<ClientResult<FullArtist>> + 'a {
paginate(
move |limit, offset| self.current_user_top_artists_manual(time_range, limit, offset),
50,
)
}

/// The manually paginated version of [`Spotify::current_user_top_artists`].
#[maybe_async]
pub async fn current_user_top_artists<
pub async fn current_user_top_artists_manual<
'a,
T: Into<Option<&'a TimeRange>>,
L: Into<Option<u32>>,
O: Into<Option<u32>>,
T: Into<Option<TimeRange>>,
>(
&self,
&'a self,
time_range: T,
limit: L,
offset: O,
time_range: T,
) -> ClientResult<Page<FullArtist>> {
let mut params = Query::with_capacity(3);
let limit = limit.into().unwrap_or(20).to_string();
let offset = offset.into().unwrap_or(0).to_string();
let time_range = time_range.into().unwrap_or(TimeRange::MediumTerm);
let time_range = time_range.into().unwrap_or(&TimeRange::MediumTerm);
params.insert("limit", &limit);
params.insert("offset", &offset);
params.insert("time_range", time_range.as_ref());
Expand Down