Skip to content

Commit e2fdc57

Browse files
committed
in endpoints returning PagingObjects, exchange SpotifyRestActions with SpotifyRestPagingActions
documentation isn't yet completed
1 parent 8313666 commit e2fdc57

25 files changed

+195
-159
lines changed

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientFollowingAPI.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.adamratzman.spotify.endpoints.public.FollowingAPI
44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyClientAPI
66
import com.adamratzman.spotify.main.SpotifyRestAction
7+
import com.adamratzman.spotify.main.SpotifyRestPagingAction
78
import com.adamratzman.spotify.utils.*
89
import java.util.function.Supplier
910

@@ -48,7 +49,7 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
4849
fun isFollowingUsers(vararg userIds: String): SpotifyRestAction<List<Boolean>> {
4950
return toAction(Supplier {
5051
get(EndpointBuilder("/me/following/contains").with("type", "user")
51-
.with("ids", userIds.joinToString(",") { it.encode() }).toString()).toObject<List<Boolean>>(api)
52+
.with("ids", userIds.joinToString(",") { it.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
5253
})
5354
}
5455

@@ -75,7 +76,7 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
7576
fun isFollowingArtists(vararg artistIds: String): SpotifyRestAction<List<Boolean>> {
7677
return toAction(Supplier {
7778
get(EndpointBuilder("/me/following/contains").with("type", "artist")
78-
.with("ids", artistIds.joinToString(",") { it.encode() }).toString()).toObject<List<Boolean>>(api)
79+
.with("ids", artistIds.joinToString(",") { it.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
7980
})
8081
}
8182

@@ -85,10 +86,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
8586
* @return [CursorBasedPagingObject] ([Information about them](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#the-benefits-of-linkedresults-pagingobjects-and-cursor-based-paging-objects)
8687
* with full [Artist] objects
8788
*/
88-
fun getFollowedArtists(limit: Int? = null, after: String? = null): SpotifyRestAction<CursorBasedPagingObject<Artist>> {
89-
return toAction(Supplier {
89+
fun getFollowedArtists(limit: Int? = null, after: String? = null): SpotifyRestPagingAction<Artist, CursorBasedPagingObject<Artist>> {
90+
return toPagingObjectAction(Supplier {
9091
get(EndpointBuilder("/me/following").with("type", "artist").with("limit", limit).with("after", after).toString())
91-
.toCursorBasedPagingObject<Artist>("artists", this)
92+
.toCursorBasedPagingObject("artists", this, Artist::class.java)
9293
})
9394
}
9495

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientLibraryAPI.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.adamratzman.spotify.endpoints.client
22

33
import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.main.SpotifyRestAction
5+
import com.adamratzman.spotify.main.SpotifyRestPagingAction
56
import com.adamratzman.spotify.utils.*
67
import java.util.function.Supplier
78

@@ -14,10 +15,10 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1415
*
1516
* @return Paging Object of [SavedTrack] ordered by position in library
1617
*/
17-
fun getSavedTracks(limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestAction<PagingObject<SavedTrack>> {
18-
return toAction(Supplier {
18+
fun getSavedTracks(limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestPagingAction<SavedTrack, PagingObject<SavedTrack>> {
19+
return toPagingObjectAction(Supplier {
1920
get(EndpointBuilder("/me/tracks").with("limit", limit).with("offset", offset).with("market", market?.code)
20-
.toString()).toPagingObject<SavedTrack>(endpoint = this)
21+
.toString()).toPagingObject(endpoint = this, tClazz = SavedTrack::class.java)
2122
})
2223
}
2324

@@ -26,10 +27,10 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2627
*
2728
* @return Paging Object of [SavedAlbum] ordered by position in library
2829
*/
29-
fun getSavedAlbums(limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestAction<PagingObject<SavedAlbum>> {
30-
return toAction(Supplier {
30+
fun getSavedAlbums(limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestPagingAction<SavedAlbum, PagingObject<SavedAlbum>> {
31+
return toPagingObjectAction(Supplier {
3132
get(EndpointBuilder("/me/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
32-
.toString()).toPagingObject<SavedAlbum>(endpoint = this)
33+
.toString()).toPagingObject(endpoint = this, tClazz = SavedAlbum::class.java)
3334
})
3435
}
3536

@@ -52,7 +53,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5253
fun contains(type: LibraryType, vararg ids: String): SpotifyRestAction<List<Boolean>> {
5354
return toAction(Supplier {
5455
get(EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { it.encode() })
55-
.toString()).toObject<List<Boolean>>(api)
56+
.toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
5657
})
5758
}
5859

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPersonalizationAPI.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.adamratzman.spotify.endpoints.client
22

33
import com.adamratzman.spotify.main.SpotifyAPI
4-
import com.adamratzman.spotify.main.SpotifyRestAction
4+
import com.adamratzman.spotify.main.SpotifyRestPagingAction
55
import com.adamratzman.spotify.utils.*
66
import java.util.function.Supplier
77

@@ -11,6 +11,7 @@ import java.util.function.Supplier
1111
class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1212
enum class TimeRange(val id: String) {
1313
LONG_TERM("long_term"), MEDIUM_TERM("medium_term"), SHORT_TERM("short_term");
14+
1415
override fun toString() = id
1516
}
1617

@@ -26,10 +27,10 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2627
*
2728
* @return [PagingObject] of full [Artist] objects sorted by affinity
2829
*/
29-
fun getTopArtists(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestAction<PagingObject<Artist>> {
30-
return toAction(Supplier {
30+
fun getTopArtists(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Artist, PagingObject<Artist>> {
31+
return toPagingObjectAction(Supplier {
3132
get(EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
32-
.with("time_range", timeRange).toString()).toPagingObject<Artist>(endpoint = this)
33+
.with("time_range", timeRange).toString()).toPagingObject(endpoint = this, tClazz = Artist::class.java)
3334
})
3435
}
3536

@@ -45,10 +46,10 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4546
*
4647
* @return [PagingObject] of full [Track] objects sorted by affinity
4748
*/
48-
fun getTopTracks(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestAction<PagingObject<Track>> {
49-
return toAction(Supplier {
49+
fun getTopTracks(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Track, PagingObject<Track>> {
50+
return toPagingObjectAction(Supplier {
5051
get(EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
51-
.with("time_range", timeRange).toString()).toPagingObject<Track>(endpoint = this)
52+
.with("time_range", timeRange).toString()).toPagingObject(endpoint = this, tClazz = Track::class.java)
5253
})
5354
}
5455

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPlayerAPI.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.adamratzman.spotify.endpoints.client
22

33
import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.main.SpotifyRestAction
5+
import com.adamratzman.spotify.main.SpotifyRestPagingAction
56
import com.adamratzman.spotify.utils.*
67
import org.json.JSONObject
78
import java.util.function.Supplier
@@ -13,26 +14,26 @@ import java.util.function.Supplier
1314
class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1415
fun getDevices(): SpotifyRestAction<List<Device>> {
1516
return toAction(Supplier {
16-
get(EndpointBuilder("/me/player/devices").toString()).toInnerObject<Device>("devices", api)
17+
get(EndpointBuilder("/me/player/devices").toString()).toInnerObject("devices", api, Device::class.java)
1718
})
1819
}
1920

2021
fun getCurrentContext(): SpotifyRestAction<CurrentlyPlayingContext?> {
2122
return toAction(Supplier {
22-
val obj: CurrentlyPlayingContext? = get(EndpointBuilder("/me/player").toString()).toObject<CurrentlyPlayingContext>(api)
23+
val obj: CurrentlyPlayingContext? = get(EndpointBuilder("/me/player").toString()).toObject(api, CurrentlyPlayingContext::class.java)
2324
if (obj?.timestamp == null) null else obj
2425
})
2526
}
2627

27-
fun getRecentlyPlayed(): SpotifyRestAction<CursorBasedPagingObject<PlayHistory>> {
28-
return toAction(Supplier {
29-
get(EndpointBuilder("/me/player/recently-played").toString()).toCursorBasedPagingObject<PlayHistory>(endpoint = this)
28+
fun getRecentlyPlayed(): SpotifyRestPagingAction<PlayHistory, CursorBasedPagingObject<PlayHistory>> {
29+
return toPagingObjectAction(Supplier {
30+
get(EndpointBuilder("/me/player/recently-played").toString()).toCursorBasedPagingObject(endpoint = this, tClazz = PlayHistory::class.java)
3031
})
3132
}
3233

3334
fun getCurrentlyPlaying(): SpotifyRestAction<CurrentlyPlayingObject?> {
3435
return toAction(Supplier {
35-
val obj: CurrentlyPlayingObject? = get(EndpointBuilder("/me/player/currently-playing").toString()).toObject<CurrentlyPlayingObject>(api)
36+
val obj: CurrentlyPlayingObject? = get(EndpointBuilder("/me/player/currently-playing").toString()).toObject(api, CurrentlyPlayingObject::class.java)
3637
if (obj?.timestamp == null) null else obj
3738
})
3839
}
@@ -116,8 +117,7 @@ class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
116117
if (offsetNum != null) body.put("offset", JSONObject().put("position", offsetNum))
117118
else if (offsetTrackId != null) body.put("offset", JSONObject().put("uri", "spotify:track:$offsetTrackId"))
118119
put(url, body.toString())
119-
}
120-
else put(url)
120+
} else put(url)
121121
Unit
122122
})
123123
}
@@ -139,7 +139,7 @@ class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
139139
fun transferPlayback(vararg deviceId: String, play: Boolean = true): SpotifyRestAction<Unit> {
140140
if (deviceId.size > 1) throw IllegalArgumentException("Although an array is accepted, only a single device_id is currently supported. Supplying more than one will 400 Bad Request")
141141
return toAction(Supplier {
142-
put(EndpointBuilder("/me/player").with("device_ids", deviceId.joinToString(",") {it.encode()})
142+
put(EndpointBuilder("/me/player").with("device_ids", deviceId.joinToString(",") { it.encode() })
143143
.with("play", play).toString())
144144
Unit
145145
})

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPlaylistAPI.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.adamratzman.spotify.endpoints.public.PlaylistsAPI
44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyClientAPI
66
import com.adamratzman.spotify.main.SpotifyRestAction
7+
import com.adamratzman.spotify.main.SpotifyRestPagingAction
78
import com.adamratzman.spotify.utils.*
89
import org.json.JSONObject
910
import java.awt.image.BufferedImage
@@ -43,7 +44,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
4344
if (description != null) json.put("description", description)
4445
if (public != null) json.put("public", public)
4546
if (collaborative != null) json.put("collaborative", collaborative)
46-
post(EndpointBuilder("/users/${userId.encode()}/playlists").toString(), json.toString()).toObject<Playlist>(api)
47+
post(EndpointBuilder("/users/${userId.encode()}/playlists").toString(), json.toString()).toObject(api, Playlist::class.java)
4748
})
4849
}
4950

@@ -102,12 +103,12 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
102103
*
103104
* @throws BadRequestException if the filters provided are illegal
104105
*/
105-
fun getClientPlaylists(limit: Int? = null, offset: Int? = null): SpotifyRestAction<PagingObject<SimplePlaylist>> {
106+
fun getClientPlaylists(limit: Int? = null, offset: Int? = null): SpotifyRestPagingAction<SimplePlaylist, PagingObject<SimplePlaylist>> {
106107
if (limit != null && limit !in 1..50) throw IllegalArgumentException("Limit must be between 1 and 50. Provided $limit")
107108
if (offset != null && offset !in 0..100000) throw IllegalArgumentException("Offset must be between 0 and 100,000. Provided $limit")
108-
return toAction(Supplier {
109+
return toPagingObjectAction(Supplier {
109110
get(EndpointBuilder("/me/playlists").with("limit", limit).with("offset", offset).toString())
110-
.toPagingObject<SimplePlaylist>(endpoint = this)
111+
.toPagingObject(endpoint = this, tClazz = SimplePlaylist::class.java)
111112
})
112113
}
113114

@@ -120,8 +121,8 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
120121
*/
121122
fun getClientPlaylist(id: String): SpotifyRestAction<SimplePlaylist?> {
122123
return toAction(Supplier {
123-
val playlists = getClientPlaylists().complete()
124-
playlists.items.find { it.id == id } ?: playlists.getAllItems<SimplePlaylist>().complete().find { it.id == id }
124+
val playlists = getClientPlaylists().completeWithPaging()
125+
playlists.items.find { it.id == id } ?: playlists.getAllItems().complete().find { it.id == id }
125126
})
126127
}
127128

@@ -165,7 +166,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
165166
if (reorderRangeLength != null) json.put("range_length", reorderRangeLength)
166167
if (snapshotId != null) json.put("snapshot_id", snapshotId)
167168
put(EndpointBuilder("/playlists/${playlistId.encode()}/tracks").toString(), json.toString())
168-
.toObject<Snapshot>(api)
169+
.toObject(api, Snapshot::class.java)
169170
})
170171
}
171172

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientUserAPI.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package com.adamratzman.spotify.endpoints.client
33
import com.adamratzman.spotify.endpoints.public.UserAPI
44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6-
import com.adamratzman.spotify.utils.*
6+
import com.adamratzman.spotify.utils.EndpointBuilder
7+
import com.adamratzman.spotify.utils.SpotifyUserInformation
8+
import com.adamratzman.spotify.utils.toObject
79
import java.util.function.Supplier
810

911
/**
@@ -21,7 +23,7 @@ class ClientUserAPI(api: SpotifyAPI) : UserAPI(api) {
2123
*/
2224
fun getUserProfile(): SpotifyRestAction<SpotifyUserInformation> {
2325
return toAction(Supplier {
24-
get(EndpointBuilder("/me").toString()).toObject<SpotifyUserInformation>(api)
26+
get(EndpointBuilder("/me").toString()).toObject(api, SpotifyUserInformation::class.java)
2527
})
2628
}
2729
}

src/main/kotlin/com/adamratzman/spotify/endpoints/public/AlbumAPI.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AlbumAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1919
fun getAlbum(albumId: String, market: Market? = null): SpotifyRestAction<Album?> {
2020
return toAction(Supplier {
2121
catch {
22-
get(EndpointBuilder("/albums/$albumId").with("market", market?.code).toString()).toObject<Album>(api)
22+
get(EndpointBuilder("/albums/$albumId").with("market", market?.code).toString()).toObject(api, Album::class.java)
2323
}
2424
})
2525
}
@@ -32,7 +32,7 @@ class AlbumAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3232
fun getAlbums(vararg albumIds: String, market: Market? = null): SpotifyRestAction<List<Album?>> {
3333
return toAction(Supplier {
3434
get(EndpointBuilder("/albums").with("ids", albumIds.joinToString(",") { it.encode() })
35-
.with("market", market?.code).toString()).toObject<AlbumsResponse>(api).albums
35+
.with("market", market?.code).toString()).toObject(api, AlbumsResponse::class.java).albums
3636
})
3737
}
3838

@@ -48,7 +48,7 @@ class AlbumAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4848
fun getAlbumTracks(albumId: String, limit: Int? = null, offset: Int? = null, market: Market? = null): SpotifyRestAction<LinkedResult<SimpleTrack>> {
4949
return toAction(Supplier {
5050
get(EndpointBuilder("/albums/${albumId.encode()}/tracks").with("limit", limit).with("offset", offset).with("market", market?.code)
51-
.toString()).toLinkedResult<SimpleTrack>(api)
51+
.toString()).toLinkedResult(api, SimpleTrack::class.java)
5252
})
5353
}
5454
}

src/main/kotlin/com/adamratzman/spotify/endpoints/public/ArtistsAPI.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1717
*/
1818
fun getArtist(artistId: String): SpotifyRestAction<Artist?> {
1919
return toAction(Supplier {
20-
catch { get(EndpointBuilder("/artists/${artistId.encode()}").toString()).toObject<Artist>(api) }
20+
catch { get(EndpointBuilder("/artists/${artistId.encode()}").toString()).toObject(api, Artist::class.java) }
2121
})
2222

2323
}
@@ -29,7 +29,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2929
fun getArtists(vararg artistIds: String): SpotifyRestAction<List<Artist?>> {
3030
return toAction(Supplier {
3131
get(EndpointBuilder("/artists").with("ids", artistIds.joinToString(",") { it.encode() }).toString())
32-
.toObject<ArtistList>(api).artists
32+
.toObject(api, ArtistList::class.java).artists
3333
})
3434
}
3535

@@ -46,7 +46,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4646
fun getArtistAlbums(artistId: String, limit: Int? = null, offset: Int? = null, market: Market? = null, include: List<AlbumInclusionStrategy> = listOf()): SpotifyRestAction<LinkedResult<SimpleAlbum>> {
4747
return toAction(Supplier {
4848
get(EndpointBuilder("/artists/${artistId.encode()}/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
49-
.with("include_groups", include.joinToString(",") { it.keyword }).toString()).toLinkedResult<SimpleAlbum>(api)
49+
.with("include_groups", include.joinToString(",") { it.keyword }).toString()).toLinkedResult(api, SimpleAlbum::class.java)
5050
})
5151
}
5252

@@ -66,7 +66,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
6666
fun getArtistTopTracks(artistId: String, market: Market = Market.US): SpotifyRestAction<List<Track>> {
6767
return toAction(Supplier {
6868
get(EndpointBuilder("/artists/${artistId.encode()}/top-tracks").with("country", market.code).toString())
69-
.toObject<TrackList>(api).tracks.map { it!! }
69+
.toObject(api, TrackList::class.java).tracks.map { it!! }
7070
})
7171
}
7272

@@ -81,7 +81,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
8181
*/
8282
fun getRelatedArtists(artistId: String): SpotifyRestAction<List<Artist>> {
8383
return toAction(Supplier {
84-
get(EndpointBuilder("/artists/${artistId.encode()}/related-artists").toString()).toObject<ArtistList>(api).artists.map { it!! }
84+
get(EndpointBuilder("/artists/${artistId.encode()}/related-artists").toString()).toObject(api, ArtistList::class.java).artists.map { it!! }
8585
})
8686
}
8787
}

0 commit comments

Comments
 (0)