I work with the Spotify API, and I hope that with the help of RxJava several paginated results are grouped. Spotify uses cursor-based breakdowns, so solutions like the one from @lopar will not work.
The answer from this call looks something like this (imagine there are 50 items):
{
"artists" : {
"items" : [ {
"id" : "6liAMWkVf5LH7YR9yfFy1Y",
"name" : "Portishead",
"type" : "artist"
}],
"next" : "https://api.spotify.com/v1/me/following?type=artist&after=6liAMWkVf5LH7YR9yfFy1Y&limit=50",
"total" : 119,
"cursors" : {
"after" : "6liAMWkVf5LH7YR9yfFy1Y"
},
"limit" : 50,
"href" : "https://api.spotify.com/v1/me/following?type=artist&limit=50"
}
}
Right now, I am getting the first 50 results like this using a modification:
public class CursorPager<T> {
public String href;
public List<T> items;
public int limit;
public String next;
public Cursor cursors;
public int total;
public CursorPager() {
}
}
public class ArtistsCursorPager {
public CursorPager<Artist> artists;
public ArtistsCursorPager() {
}
}
then
public interface SpotifyService {
@GET("/me/following?type=artist")
Observable<ArtistsCursorPager> getFollowedArtists(@Query("limit") int limit);
@GET("/me/following?type=artist")
Observable<ArtistsCursorPager> getFollowedArtists(@Query("limit") int limit, @Query("after") String spotifyId);
}
and
mSpotifyService.getFollowedArtists(50)
.flatMap(result -> Observable.from(result.artists.items))
.flatMap(this::responseToArtist)
.sorted()
.toList()
.subscribe(new Subscriber<List<Artist>>() {
@Override
public void onNext(List<Artist> artists) {
callback.onSuccess(artists);
}
});
I would like to return all (in this case 119) artists to callback.success(List<Artist>). I am new to RxJava, so I'm not sure if there is a way to do this.