-
Notifications
You must be signed in to change notification settings - Fork 124
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
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9191013
change naming to _manual
marioortizmanero 6733e08
base implementations
marioortizmanero 4ef0fa8
docs improvements and more progress
marioortizmanero d9ffe17
manually implemented all the endpoints
marioortizmanero 8a95212
fix docs
marioortizmanero a68137b
fix tests & examples
marioortizmanero 60f5610
fix changelog
marioortizmanero b0a4bf7
add pagination_chunks
marioortizmanero 2e5da23
mention pagination_chunks in the changelog
marioortizmanero 42cabe7
use pagination_chunks in tests and fix generics
marioortizmanero 1382f84
fix pagination_chunks value
marioortizmanero 5d00edd
add PSA about pagination_chunks
marioortizmanero 364fa03
relax lifetimes
marioortizmanero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>, | ||
limit: Option<u32>, | ||
offset: Option<u32>, | ||
) -> ClientResult<Page<SimplifiedAlbum>> { | ||
|
@@ -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, | ||
|
@@ -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, | ||
|
@@ -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, | ||
|
@@ -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>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I was at it I made these parameters consistent; |
||
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(); | ||
|
@@ -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); | ||
|
@@ -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, | ||
|
@@ -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: | ||
|
@@ -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()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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