Skip to content

Commit 8b40792

Browse files
committed
change the verbose string-based endpoint building to EndpointBuilder
1 parent 0d8884c commit 8b40792

File tree

20 files changed

+225
-156
lines changed

20 files changed

+225
-156
lines changed

.circleci/config.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Java Gradle CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-java/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
- image: circleci/openjdk:8-jdk
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# - image: circleci/postgres:9.4
16+
17+
working_directory: ~/repo
18+
19+
environment:
20+
# Customize the JVM maximum heap limit
21+
JVM_OPTS: -Xmx3200m
22+
TERM: dumb
23+
24+
steps:
25+
- checkout
26+
27+
# Download and cache dependencies
28+
- restore_cache:
29+
keys:
30+
- v1-dependencies-{{ checksum "build.gradle" }}
31+
# fallback to using the latest cache if no exact match is found
32+
- v1-dependencies-
33+
34+
- run: gradle dependencies
35+
36+
- save_cache:
37+
paths:
38+
- ~/.gradle
39+
key: v1-dependencies-{{ checksum "build.gradle" }}
40+
41+
# run tests!
42+
- run: gradle test

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.adamratzman'
2-
version '1.0.2'
2+
version '1.0.3'
33

44
buildscript {
55
ext.kotlin_version = '1.2.31'

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/follow/ClientFollowAPI.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3030
*/
3131
fun isFollowingUsers(vararg userIds: String): SpotifyRestAction<List<Boolean>> {
3232
return toAction(Supplier {
33-
get("https://api.spotify.com/v1/me/following/contains?type=user&ids=${userIds.joinToString(",") { it.encode() }}").toObject<List<Boolean>>(api)
33+
get(EndpointBuilder("/me/following/contains").with("type", "user")
34+
.with("ids", userIds.joinToString(",") { it.encode() }).build()).toObject<List<Boolean>>(api)
3435
})
3536
}
3637

@@ -56,7 +57,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5657
*/
5758
fun isFollowingArtists(vararg artistIds: String): SpotifyRestAction<List<Boolean>> {
5859
return toAction(Supplier {
59-
get("https://api.spotify.com/v1/me/following/contains?type=artist&ids=${artistIds.joinToString(",") { it.encode() }}").toObject<List<Boolean>>(api)
60+
get(EndpointBuilder("/me/following/contains").with("type", "artist")
61+
.with("ids", artistIds.joinToString(",") { it.encode() }).build()).toObject<List<Boolean>>(api)
6062
})
6163
}
6264

@@ -66,14 +68,14 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
6668
* @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)
6769
* with full [Artist] objects
6870
*/
69-
fun getFollowedArtists(): SpotifyRestAction<CursorBasedPagingObject<Artist>> {
71+
fun getFollowedArtists(limit: Int? = null, after: String? = null): SpotifyRestAction<CursorBasedPagingObject<Artist>> {
7072
return toAction(Supplier {
71-
get("https://api.spotify.com/v1/me/following?type=artist").toCursorBasedPagingObject<Artist>("artists", api)
73+
get(EndpointBuilder("/me/following").with("type", "artist").with("limit", limit).with("after", after).build())
74+
.toCursorBasedPagingObject<Artist>("artists", this)
7275
})
7376
}
7477

75-
fun getFollowedUsers(): SpotifyRestAction<SpotifyPublicUser>
76-
= throw NotImplementedError("Though Spotify will implement this in the future, it is not currently supported.")
78+
fun getFollowedUsers(): SpotifyRestAction<SpotifyPublicUser> = throw NotImplementedError("Though Spotify will implement this in the future, it is not currently supported.")
7779

7880
/**
7981
* Add the current user as a follower of another user
@@ -93,7 +95,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
9395
*/
9496
fun followUsers(vararg userIds: String): SpotifyRestAction<Unit> {
9597
return toAction(Supplier {
96-
put("https://api.spotify.com/v1/me/following?type=user&ids=${userIds.joinToString(",") { it.encode() }}")
98+
put(EndpointBuilder("/me/following").with("type", "user")
99+
.with("ids", userIds.joinToString(",") { it.encode() }).build())
97100
Unit
98101
})
99102
}
@@ -116,7 +119,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
116119
*/
117120
fun followArtists(vararg artistIds: String): SpotifyRestAction<Unit> {
118121
return toAction(Supplier {
119-
put("https://api.spotify.com/v1/me/following?type=artist&ids=${artistIds.joinToString(",")}")
122+
put(EndpointBuilder("/me/following").with("type", "artist")
123+
.with("ids", artistIds.joinToString(",") { it.encode() }).build())
120124
Unit
121125
})
122126
}
@@ -134,7 +138,7 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
134138
*/
135139
fun followPlaylist(ownerId: String, playlistId: String, followPublicly: Boolean = true): SpotifyRestAction<Unit> {
136140
return toAction(Supplier {
137-
put("https://api.spotify.com/v1/users/$ownerId/playlists/$playlistId/followers", "{\"public\": $followPublicly}")
141+
put(EndpointBuilder("/users/$ownerId/playlists/$playlistId/followers").build(), "{\"public\": $followPublicly}")
138142
Unit
139143
})
140144

@@ -162,7 +166,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
162166
*/
163167
fun unfollowUsers(vararg userIds: String): SpotifyRestAction<Unit> {
164168
return toAction(Supplier {
165-
delete("https://api.spotify.com/v1/me/following?type=user&ids=${userIds.joinToString(",") { it.encode() }}")
169+
delete(EndpointBuilder("/me/following").with("type", "user")
170+
.with("ids", userIds.joinToString(",") { it.encode() }).build())
166171
Unit
167172
})
168173
}
@@ -189,7 +194,8 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
189194
*/
190195
fun unfollowArtists(vararg artistIds: String): SpotifyRestAction<Unit> {
191196
return toAction(Supplier {
192-
delete("https://api.spotify.com/v1/me/following?type=artist&ids=${artistIds.joinToString(",")}")
197+
delete(EndpointBuilder("/me/following").with("type", "artist")
198+
.with("ids", artistIds.joinToString(",") { it.encode() }).build())
193199
Unit
194200
})
195201
}
@@ -204,7 +210,7 @@ class ClientFollowAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
204210
*/
205211
fun unfollowPlaylist(ownerId: String, playlistId: String): SpotifyRestAction<Unit> {
206212
return toAction(Supplier {
207-
delete("https://api.spotify.com/v1/users/$ownerId/playlists/$playlistId/followers")
213+
delete(EndpointBuilder("/users/$ownerId/playlists/$playlistId/followers").build())
208214
Unit
209215
})
210216
}

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/library/ClientLibraryAPI.kt

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

33
import com.adamratzman.spotify.main.SpotifyAPI
44
import com.adamratzman.spotify.utils.*
5-
import com.sun.org.apache.xpath.internal.operations.Bool
65
import java.util.function.Supplier
76

87
/**
@@ -14,9 +13,9 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1413
*
1514
* @return Paging Object of [SavedTrack] ordered by position in library
1615
*/
17-
fun getSavedTracks(): SpotifyRestAction<PagingObject<SavedTrack>> {
16+
fun getSavedTracks(limit: Int = 20, offset: Int = 0, market: Market? = null): SpotifyRestAction<PagingObject<SavedTrack>> {
1817
return toAction(Supplier {
19-
get("https://api.spotify.com/v1/me/tracks").toPagingObject<SavedTrack>(api = api)
18+
get(EndpointBuilder("/me/tracks").build()).toPagingObject<SavedTrack>(endpoint = this)
2019
})
2120
}
2221

@@ -27,7 +26,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2726
*/
2827
fun getSavedAlbums(): SpotifyRestAction<PagingObject<SavedAlbum>> {
2928
return toAction(Supplier {
30-
get("https://api.spotify.com/v1/me/albums").toPagingObject<SavedAlbum>(api = api)
29+
get(EndpointBuilder("/me/albums").build()).toPagingObject<SavedAlbum>(endpoint = this)
3130
})
3231
}
3332

@@ -49,7 +48,8 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4948
*/
5049
fun doesLibraryContainTracks(vararg ids: String): SpotifyRestAction<List<Boolean>> {
5150
return toAction(Supplier {
52-
get("https://api.spotify.com/v1/me/tracks/contains?ids=${ids.joinToString(",") { it.encode() }}").toObject<List<Boolean>>(api)
51+
get(EndpointBuilder("/me/tracks/contains").with("ids", ids.joinToString(",") { it.encode() })
52+
.build()).toObject<List<Boolean>>(api)
5353
})
5454
}
5555

@@ -71,7 +71,8 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
7171
*/
7272
fun doesLibraryContainAlbums(vararg ids: String): SpotifyRestAction<List<Boolean>> {
7373
return toAction(Supplier {
74-
get("https://api.spotify.com/v1/me/albums/contains?ids=${ids.joinToString(",") { it.encode() }}").toObject<List<Boolean>>(api)
74+
get(EndpointBuilder("/me/albums/contains").with("ids", ids.joinToString(",") { it.encode() })
75+
.build()).toObject<List<Boolean>>(api)
7576
})
7677
}
7778

@@ -93,7 +94,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
9394
*/
9495
fun addTracksToLibrary(vararg ids: String): SpotifyRestAction<Unit> {
9596
return toAction(Supplier {
96-
put("https://api.spotify.com/v1/me/tracks?ids=${ids.joinToString(",") { it.encode() }}")
97+
put(EndpointBuilder("/me/tracks").with("ids", ids.joinToString(",") { it.encode() }).build())
9798
Unit
9899
})
99100
}
@@ -116,7 +117,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
116117
*/
117118
fun addAlbumsToLibrary(vararg ids: String): SpotifyRestAction<Unit> {
118119
return toAction(Supplier {
119-
put("https://api.spotify.com/v1/me/albums?ids=${ids.joinToString(",") { it.encode() }}")
120+
put(EndpointBuilder("/me/albums").with("ids", ids.joinToString(",") { it.encode() }).build())
120121
Unit
121122
})
122123
}
@@ -139,7 +140,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
139140
*/
140141
fun removeTracksFromLibrary(vararg ids: String): SpotifyRestAction<Unit> {
141142
return toAction(Supplier {
142-
delete("https://api.spotify.com/v1/me/tracks?ids=${ids.joinToString(",") { it.encode() }}")
143+
delete(EndpointBuilder("/me/tracks").with("ids", ids.joinToString(",") { it.encode() }).build())
143144
Unit
144145
})
145146
}
@@ -162,7 +163,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
162163
*/
163164
fun removeAlbumsFromLibrary(vararg ids: String): SpotifyRestAction<Unit> {
164165
return toAction(Supplier {
165-
delete("https://api.spotify.com/v1/me/albums?ids=${ids.joinToString(",") { it.encode() }}")
166+
delete(EndpointBuilder("/me/albums").with("ids", ids.joinToString(",") { it.encode() }).build())
166167
Unit
167168
})
168169
}

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/personalization/PersonalizationAPI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2222
*/
2323
fun getTopArtists(): SpotifyRestAction<PagingObject<Artist>> {
2424
return toAction(Supplier {
25-
get("https://api.spotify.com/v1/me/top/artists").toPagingObject<Artist>(api = api)
25+
get(EndpointBuilder("/me/top/artists").build()).toPagingObject<Artist>(endpoint = this)
2626
})
2727
}
2828

@@ -40,7 +40,7 @@ class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4040
*/
4141
fun getTopTracks(): SpotifyRestAction<PagingObject<Track>> {
4242
return toAction(Supplier {
43-
get("https://api.spotify.com/v1/me/top/tracks").toPagingObject<Track>(api = api)
43+
get(EndpointBuilder("/me/top/tracks").build()).toPagingObject<Track>(endpoint = this)
4444
})
4545
}
4646

src/main/kotlin/com/adamratzman/spotify/endpoints/priv/player/PlayerAPI.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,70 @@ import java.util.function.Supplier
1212
class PlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1313
fun getDevices(): SpotifyRestAction<List<Device>> {
1414
return toAction(Supplier {
15-
get("https://api.spotify.com/v1/me/player/devices").toInnerObject<Device>("devices", api)
15+
get(EndpointBuilder("/me/player/devices").build()).toInnerObject<Device>("devices", api)
1616
})
1717
}
1818

1919
fun getCurrentContext(): SpotifyRestAction<CurrentlyPlayingContext?> {
2020
return toAction(Supplier {
21-
val obj: CurrentlyPlayingContext? = get("https://api.spotify.com/v1/me/player").toObject<CurrentlyPlayingContext>(api)
21+
val obj: CurrentlyPlayingContext? = get(EndpointBuilder("/me/player").build()).toObject<CurrentlyPlayingContext>(api)
2222
if (obj?.timestamp == null) null else obj
2323
})
2424
}
2525

2626
fun getRecentlyPlayed(): SpotifyRestAction<CursorBasedPagingObject<PlayHistory>> {
2727
return toAction(Supplier {
28-
get("https://api.spotify.com/v1/me/player/recently-played").toCursorBasedPagingObject<PlayHistory>(api = api)
28+
get(EndpointBuilder("/me/player/recently-played").build()).toCursorBasedPagingObject<PlayHistory>(endpoint = this)
2929
})
3030
}
3131

3232
fun getCurrentlyPlaying(): SpotifyRestAction<CurrentlyPlayingObject?> {
3333
return toAction(Supplier {
34-
val obj: CurrentlyPlayingObject? = get("https://api.spotify.com/v1/me/player/currently-playing").toObject<CurrentlyPlayingObject>(api)
34+
val obj: CurrentlyPlayingObject? = get(EndpointBuilder("/me/player/currently-playing").build()).toObject<CurrentlyPlayingObject>(api)
3535
if (obj?.timestamp == null) null else obj
3636
})
3737
}
3838

3939
fun pausePlayback(deviceId: String? = null): SpotifyRestAction<Unit> {
4040
return toAction(Supplier {
41-
put("https://api.spotify.com/v1/me/player/pause${if (deviceId != null) "?device_id=${deviceId.encode()}" else ""}")
41+
put(EndpointBuilder("/me/player/pause").with("device_id", deviceId).build())
4242
Unit
4343
})
4444
}
4545

4646
fun seekPosition(positionMs: Long, deviceId: String? = null): SpotifyRestAction<Unit> {
4747
return toAction(Supplier {
4848
if (positionMs < 0) throw IllegalArgumentException("Position must not be negative!")
49-
put("https://api.spotify.com/v1/me/player/seek?position_ms=$positionMs${if (deviceId != null) "&device_id=${deviceId.encode()}" else ""}")
49+
put(EndpointBuilder("/me/player/seek").with("position_ms", positionMs).with("device_id", deviceId).build())
5050
Unit
5151
})
5252
}
5353

5454
fun setRepeatMode(state: PlayerRepeatState, deviceId: String? = null): SpotifyRestAction<Unit> {
5555
return toAction(Supplier {
56-
put("https://api.spotify.com/v1/me/player/repeat?state=${state.toString().toLowerCase()}${if (deviceId != null) "&device_id=${deviceId.encode()}" else ""}")
56+
put(EndpointBuilder("/me/player/repeat").with("state", state.toString().toLowerCase()).with("device_id", deviceId).build())
5757
Unit
5858
})
5959
}
6060

6161
fun setVolume(volume: Int, deviceId: String? = null): SpotifyRestAction<Unit> {
6262
if (volume !in 0..100) throw IllegalArgumentException("Volume must be within 0 to 100 inclusive. Provided: $volume")
6363
return toAction(Supplier {
64-
put("https://api.spotify.com/v1/me/player/volume?volume_percent=$volume${if (deviceId != null) "&device_id=${deviceId.encode()}" else ""}")
64+
put(EndpointBuilder("/me/player/volume").with("volume_percent", volume).with("device_id", deviceId).build())
6565
Unit
6666
})
6767
}
6868

6969
fun skipToNextTrack(deviceId: String? = null): SpotifyRestAction<Unit> {
7070
return toAction(Supplier {
71-
post("https://api.spotify.com/v1/me/player/next${if (deviceId != null) "?device_id=${deviceId.encode()}" else ""}")
71+
post(EndpointBuilder("/me/player/next").with("device_id", deviceId).build())
7272
Unit
7373
})
7474
}
7575

7676
fun rewindToLastTrack(deviceId: String? = null): SpotifyRestAction<Unit> {
7777
return toAction(Supplier {
78-
post("https://api.spotify.com/v1/me/player/previous${if (deviceId != null) "?device_id=${deviceId.encode()}" else ""}")
78+
post(EndpointBuilder("/me/player/previous").with("device_id", deviceId).build())
7979
Unit
8080
})
8181
}
@@ -103,7 +103,7 @@ class PlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
103103
fun startPlayback(albumId: String? = null, artistId: String? = null, playlist: PlaylistParams? = null,
104104
offsetNum: Int? = null, offsetTrackId: String? = null, deviceId: String? = null, vararg tracksToPlay: String): SpotifyRestAction<Unit> {
105105
return toAction(Supplier {
106-
val url = "https://api.spotify.com/v1/me/player/play${if (deviceId != null) "?device_id=${deviceId.encode()}" else ""}"
106+
val url = EndpointBuilder("/me/player/play").with("device_id", deviceId).build()
107107
val body = JSONObject()
108108
when {
109109
albumId != null -> body.put("context_uri", "spotify:album:$albumId")
@@ -130,15 +130,16 @@ class PlayerAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
130130

131131
fun shufflePlayback(shuffle: Boolean = true, deviceId: String? = null): SpotifyRestAction<Unit> {
132132
return toAction(Supplier {
133-
put("https://api.spotify.com/v1/me/player/shuffle?state=$shuffle${if (deviceId != null) "&device_id=${deviceId.encode()}" else ""}")
133+
put(EndpointBuilder("/me/player/shuffle").with("state", shuffle).with("device_id", deviceId).build())
134134
Unit
135135
})
136136
}
137137

138138
fun transferPlayback(vararg deviceId: String, play: Boolean = true): SpotifyRestAction<Unit> {
139139
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")
140140
return toAction(Supplier {
141-
put("https://api.spotify.com/v1/me/player?deviceId=${deviceId.map { it.encode() }.joinToString(",")}&play=$play")
141+
put(EndpointBuilder("/me/player").with("device_ids", deviceId.joinToString(",") {it.encode()})
142+
.with("play", play).build())
142143
Unit
143144
})
144145
}

0 commit comments

Comments
 (0)