Skip to content

Commit e4eed05

Browse files
committed
Use inline uri classes
* Added use of uri classes
1 parent 6f52e64 commit e4eed05

File tree

14 files changed

+232
-277
lines changed

14 files changed

+232
-277
lines changed

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
1919
*
2020
* @throws BadRequestException if [userId] is a non-existing id
2121
*/
22-
fun isFollowingUser(userId: String): SpotifyRestAction<Boolean> {
22+
fun isFollowingUser(user: String): SpotifyRestAction<Boolean> {
2323
return toAction(Supplier {
24-
isFollowingUsers(userId).complete()[0]
24+
isFollowingUsers(user).complete()[0]
2525
})
2626
}
2727

@@ -46,10 +46,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
4646
*
4747
* @throws BadRequestException if [userIds] contains a non-existing id
4848
*/
49-
fun isFollowingUsers(vararg userIds: String): SpotifyRestAction<List<Boolean>> {
49+
fun isFollowingUsers(vararg users: String): SpotifyRestAction<List<Boolean>> {
5050
return toAction(Supplier {
5151
get(EndpointBuilder("/me/following/contains").with("type", "user")
52-
.with("ids", userIds.joinToString(",") { it.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
52+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
5353
})
5454
}
5555

@@ -60,9 +60,9 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
6060
*
6161
* @throws BadRequestException if [artistId] is a non-existing id
6262
*/
63-
fun isFollowingArtist(artistId: String): SpotifyRestAction<Boolean> {
63+
fun isFollowingArtist(artist: String): SpotifyRestAction<Boolean> {
6464
return toAction(Supplier {
65-
isFollowingArtists(artistId).complete()[0]
65+
isFollowingArtists(artist).complete()[0]
6666
})
6767
}
6868

@@ -73,10 +73,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
7373
*
7474
* @throws BadRequestException if [artistIds] contains a non-existing id
7575
*/
76-
fun isFollowingArtists(vararg artistIds: String): SpotifyRestAction<List<Boolean>> {
76+
fun isFollowingArtists(vararg artists: String): SpotifyRestAction<List<Boolean>> {
7777
return toAction(Supplier {
7878
get(EndpointBuilder("/me/following/contains").with("type", "artist")
79-
.with("ids", artistIds.joinToString(",") { it.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
79+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
8080
})
8181
}
8282

@@ -100,9 +100,9 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
100100
*
101101
* @throws BadRequestException if an invalid id is provided
102102
*/
103-
fun followUser(userId: String): SpotifyRestAction<Unit> {
103+
fun followUser(user: String): SpotifyRestAction<Unit> {
104104
return toAction(Supplier {
105-
followUsers(userId).complete()
105+
followUsers(user).complete()
106106
})
107107
}
108108

@@ -111,10 +111,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
111111
*
112112
* @throws BadRequestException if an invalid id is provided
113113
*/
114-
fun followUsers(vararg userIds: String): SpotifyRestAction<Unit> {
114+
fun followUsers(vararg users: String): SpotifyRestAction<Unit> {
115115
return toAction(Supplier {
116116
put(EndpointBuilder("/me/following").with("type", "user")
117-
.with("ids", userIds.joinToString(",") { it.encode() }).toString())
117+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString())
118118
Unit
119119
})
120120
}
@@ -135,28 +135,27 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
135135
*
136136
* @throws BadRequestException if an invalid id is provided
137137
*/
138-
fun followArtists(vararg artistIds: String): SpotifyRestAction<Unit> {
138+
fun followArtists(vararg artists: String): SpotifyRestAction<Unit> {
139139
return toAction(Supplier {
140140
put(EndpointBuilder("/me/following").with("type", "artist")
141-
.with("ids", artistIds.joinToString(",") { it.encode() }).toString())
141+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString())
142142
Unit
143143
})
144144
}
145145

146146
/**
147147
* Add the current user as a follower of a playlist.
148148
*
149-
* @param ownerId The Spotify user ID of the person who owns the playlist.
150-
* @param playlistId The Spotify ID of the playlist. Any playlist can be followed, regardless of its
149+
* @param playlist The Spotify ID of the playlist. Any playlist can be followed, regardless of its
151150
* public/private status, as long as you know its playlist ID.
152151
* @param followPublicly Defaults to true. If true the playlist will be included in user’s public playlists,
153152
* if false it will remain private. To be able to follow playlists privately, the user must have granted the playlist-modify-private scope.
154153
*
155154
* @throws BadRequestException if the playlist is not found
156155
*/
157-
fun followPlaylist(playlistId: String, followPublicly: Boolean = true): SpotifyRestAction<Unit> {
156+
fun followPlaylist(playlist: String, followPublicly: Boolean = true): SpotifyRestAction<Unit> {
158157
return toAction(Supplier {
159-
put(EndpointBuilder("/playlists/$playlistId/followers").toString(), "{\"public\": $followPublicly}")
158+
put(EndpointBuilder("/playlists/${PlaylistURI(playlist).id}/followers").toString(), "{\"public\": $followPublicly}")
160159
Unit
161160
})
162161

@@ -169,9 +168,9 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
169168
*
170169
* @throws BadRequestException if [userId] is not found
171170
*/
172-
fun unfollowUser(userId: String): SpotifyRestAction<Unit> {
171+
fun unfollowUser(user: String): SpotifyRestAction<Unit> {
173172
return toAction(Supplier {
174-
unfollowUsers(userId).complete()
173+
unfollowUsers(user).complete()
175174
})
176175
}
177176

@@ -182,10 +181,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
182181
*
183182
* @throws BadRequestException if an invalid id is provided
184183
*/
185-
fun unfollowUsers(vararg userIds: String): SpotifyRestAction<Unit> {
184+
fun unfollowUsers(vararg users: String): SpotifyRestAction<Unit> {
186185
return toAction(Supplier {
187186
delete(EndpointBuilder("/me/following").with("type", "user")
188-
.with("ids", userIds.joinToString(",") { it.encode() }).toString())
187+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString())
189188
Unit
190189
})
191190
}
@@ -197,9 +196,9 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
197196
*
198197
* @throws BadRequestException if an invalid id is provided
199198
*/
200-
fun unfollowArtist(artistId: String): SpotifyRestAction<Unit> {
199+
fun unfollowArtist(artist: String): SpotifyRestAction<Unit> {
201200
return toAction(Supplier {
202-
unfollowArtists(artistId).complete()
201+
unfollowArtists(artist).complete()
203202
})
204203
}
205204

@@ -210,26 +209,25 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
210209
*
211210
* @throws BadRequestException if an invalid id is provided
212211
*/
213-
fun unfollowArtists(vararg artistIds: String): SpotifyRestAction<Unit> {
212+
fun unfollowArtists(vararg artists: String): SpotifyRestAction<Unit> {
214213
return toAction(Supplier {
215214
delete(EndpointBuilder("/me/following").with("type", "artist")
216-
.with("ids", artistIds.joinToString(",") { it.encode() }).toString())
215+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString())
217216
Unit
218217
})
219218
}
220219

221220
/**
222221
* Remove the current user as a follower of a playlist.
223222
*
224-
* @param ownerId The Spotify user ID of the person who owns the playlist.
225223
* @param playlistId The Spotify ID of the playlist that is to be no longer followed.
226224
*
227225
* @throws BadRequestException if the playlist is not found
228226
*/
229-
fun unfollowPlaylist(playlistId: String): SpotifyRestAction<Unit> {
227+
fun unfollowPlaylist(playlist: String): SpotifyRestAction<Unit> {
230228
return toAction(Supplier {
231-
delete(EndpointBuilder("/playlists/$playlistId/followers").toString())
229+
delete(EndpointBuilder("/playlists/${PlaylistURI(playlist).id}/followers").toString())
232230
Unit
233231
})
234232
}
235-
}
233+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3737
/**
3838
* Check if the [LibraryType] with id [id] is already saved in the current Spotify user’s ‘Your Music’ library.
3939
*
40-
* @throws BadRequestException if [id] is not found
40+
* @throws BadRequestException if [track] is not found
4141
*/
4242
fun contains(type: LibraryType, id: String): SpotifyRestAction<Boolean> {
4343
return toAction(Supplier {
@@ -52,7 +52,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5252
*/
5353
fun contains(type: LibraryType, vararg ids: String): SpotifyRestAction<List<Boolean>> {
5454
return toAction(Supplier {
55-
get(EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { it.encode() })
55+
get(EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { type.id(it).encode() })
5656
.toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
5757
})
5858
}
@@ -75,7 +75,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
7575
*/
7676
fun add(type: LibraryType, vararg ids: String): SpotifyRestAction<Unit> {
7777
return toAction(Supplier {
78-
put(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { it.encode() }).toString())
78+
put(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { type.id(it).encode() }).toString())
7979
Unit
8080
})
8181
}
@@ -98,14 +98,14 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
9898
*/
9999
fun remove(type: LibraryType, vararg ids: String): SpotifyRestAction<Unit> {
100100
return toAction(Supplier {
101-
delete(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { it.encode() }).toString())
101+
delete(EndpointBuilder("/me/$type").with("ids", ids.joinToString(",") { type.id(it).encode() }).toString())
102102
Unit
103103
})
104104
}
105105
}
106106

107-
enum class LibraryType(private val value: String) {
108-
TRACK("tracks"), ALBUM("albums");
107+
enum class LibraryType(private val value: String, internal val id: (String) -> String) {
108+
TRACK("tracks", { TrackURI(it).id }), ALBUM("albums", { AlbumURI(it).id });
109109

110110
override fun toString() = value
111-
}
111+
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
102102
*
103103
* @throws BadRequestException if more than one type of play type is specified or the offset is illegal.
104104
*/
105-
fun startPlayback(albumId: String? = null, artistId: String? = null, playlist: PlaylistParams? = null,
105+
fun startPlayback(album: String? = null, artist: String? = null, playlist: PlaylistURI? = null,
106106
offsetNum: Int? = null, offsetTrackId: String? = null, deviceId: String? = null, vararg tracksToPlay: String): SpotifyRestAction<Unit> {
107107
return toAction(Supplier {
108108
val url = EndpointBuilder("/me/player/play").with("device_id", deviceId).toString()
109109
val body = JSONObject()
110110
when {
111-
albumId != null -> body.put("context_uri", "spotify:album:$albumId")
112-
artistId != null -> body.put("context_uri", "spotify:artist:$artistId")
113-
playlist != null -> body.put("context_uri", "spotify:user:${playlist.author}:playlist:${playlist.id}")
114-
tracksToPlay.isNotEmpty() -> body.put("uris", tracksToPlay.map { "spotify:track:$it" })
111+
album != null -> body.put("context_uri", AlbumURI(album).uri)
112+
artist != null -> body.put("context_uri", ArtistURI(artist).uri)
113+
playlist != null -> body.put("context_uri", playlist.uri)
114+
tracksToPlay.isNotEmpty() -> body.put("uris", tracksToPlay.map { TrackURI(it).uri })
115115
}
116116
if (body.keySet().isNotEmpty()) {
117117
if (offsetNum != null) body.put("offset", JSONObject().put("position", offsetNum))
118-
else if (offsetTrackId != null) body.put("offset", JSONObject().put("uri", "spotify:track:$offsetTrackId"))
118+
else if (offsetTrackId != null) body.put("offset", JSONObject().put("uri", TrackURI(offsetTrackId).uri))
119119
put(url, body.toString())
120120
} else put(url)
121121
Unit
@@ -146,4 +146,4 @@ class ClientPlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
146146
}
147147

148148
enum class PlayerRepeatState { TRACK, CONTEXT, OFF }
149-
}
149+
}

0 commit comments

Comments
 (0)