Skip to content

Commit 55781ba

Browse files
committed
add clientplayerapi and clientplaylistapi samples
1 parent 20e5c04 commit 55781ba

File tree

5 files changed

+101
-9
lines changed

5 files changed

+101
-9
lines changed

samples/src/main/kotlin/private/ClientPlayerApiSample.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
package private
33

44
import com.adamratzman.spotify.SpotifyApi.Companion.spotifyClientApi
5+
import com.adamratzman.spotify.annotations.SpotifyExperimentalFunctionApi
6+
import com.adamratzman.spotify.endpoints.client.ClientPlayerApi.PlayerRepeatState.TRACK
57

8+
@SpotifyExperimentalFunctionApi
69
fun main() {
710
// instantiate api
811
val api = spotifyClientApi(
@@ -13,4 +16,46 @@ fun main() {
1316
tokenString = System.getenv("SPOTIFY_TOKEN_STRING")
1417
}
1518
}.build()
19+
20+
// print all devices connected to this Spotify account
21+
println(api.player.getDevices().complete())
22+
23+
// get the current context track position
24+
println(api.player.getCurrentContext().complete()?.progressMs)
25+
26+
// get the 20 most recently played (there is a small lag) tracks
27+
println(api.player.getRecentlyPlayed().complete().map { it.track.name })
28+
29+
// get the currenty played PlaybackActions
30+
println(api.player.getCurrentlyPlaying().complete()?.actions)
31+
32+
// pause playback on the current device
33+
api.player.pause().complete()
34+
35+
// seek the beginning of the track currently playing
36+
api.player.seek(0).complete()
37+
38+
// set repeat the current track
39+
api.player.setRepeatMode(TRACK).complete()
40+
41+
// set volume to 50%
42+
api.player.setVolume(50).complete()
43+
44+
// skip to the next track
45+
api.player.skipForward().complete()
46+
47+
// skip back to the last track that was in the user queue
48+
api.player.skipBehind().complete()
49+
50+
// resume playback
51+
api.player.resume().complete()
52+
53+
// play song "I'm Good" by the Mowgli's
54+
api.player.startPlayback(tracksToPlay = listOf(api.search.searchTrack("I'm Good the Mowgli's").complete()[0].uri.uri)).complete()
55+
56+
// toggle shuffling
57+
api.player.toggleShuffle(shuffle = true).complete()
58+
59+
// transfer playback
60+
api.player.transferPlayback("your_device_id").complete()
1661
}

samples/src/main/kotlin/private/ClientPlaylistApiSample.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,43 @@ fun main() {
1313
tokenString = System.getenv("SPOTIFY_TOKEN_STRING")
1414
}
1515
}.build()
16+
17+
// create and print a new playlist called "Test Playlist"
18+
val playlist = api.playlists.createClientPlaylist(
19+
"Test Playlist",
20+
"My test playlist description",
21+
true
22+
).complete()
23+
24+
println(playlist)
25+
26+
// add "Reflections" by MisterWives to the end of your newly-created playlist
27+
api.playlists.addTrackToClientPlaylist(playlist.id, "spotify:track:2PtBhfoPZ6VYtXkrE5FrCH").complete()
28+
29+
// change the playlist description to "My awesome playlist"
30+
api.playlists.changeClientPlaylistDetails(playlist.id, description = "My awesome playlist").complete()
31+
32+
// get all your client playlists
33+
println(api.playlists.getClientPlaylists(limit = 50).getAllItems().complete())
34+
35+
// get the number of tracks in a client playlist
36+
println(api.playlists.getClientPlaylist(playlist.id).complete()?.tracks?.total)
37+
38+
// see https://developer.spotify.com/documentation/web-api/reference/playlists/reorder-playlists-tracks/
39+
// for an example of re-ordering tracks
40+
41+
// replace the tracks in a client playlist with two songs by Lorde
42+
api.playlists.setClientPlaylistTracks(playlist.id, "spotify:track:6ie2Bw3xLj2JcGowOlcMhb", "").complete()
43+
44+
// remove the song we just added from our client playlist
45+
api.playlists.removeTrackFromClientPlaylist(playlist.id, "spotify:track:6ie2Bw3xLj2JcGowOlcMhb").complete()
46+
47+
// remove all playlist tracks
48+
api.playlists.removeAllClientPlaylistTracks(playlist.id)
49+
50+
// upload a client playlist cover
51+
api.playlists.uploadClientPlaylistCover(playlist.id, imageUrl = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png").complete()
52+
53+
// delete a client playlist
54+
api.playlists.deleteClientPlaylist(playlist.id).complete()
1655
}

src/commonMain/kotlin/com.adamratzman.spotify/annotations/ExperimentalAnnotations.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ package com.adamratzman.spotify.annotations
55
@Retention(AnnotationRetention.BINARY)
66
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
77
annotation class SpotifyExperimentalHttpApi
8+
9+
@Experimental
10+
@Retention(AnnotationRetention.BINARY)
11+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
12+
annotation class SpotifyExperimentalFunctionApi

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientPlayerApi.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import com.adamratzman.spotify.SpotifyException.BadRequestException
66
import com.adamratzman.spotify.SpotifyRestAction
77
import com.adamratzman.spotify.SpotifyRestActionPaging
88
import com.adamratzman.spotify.SpotifyScope
9+
import com.adamratzman.spotify.annotations.SpotifyExperimentalFunctionApi
910
import com.adamratzman.spotify.http.EndpointBuilder
1011
import com.adamratzman.spotify.http.SpotifyEndpoint
11-
import com.adamratzman.spotify.http.encodeUrl
1212
import com.adamratzman.spotify.models.AlbumUri
1313
import com.adamratzman.spotify.models.ArtistUri
1414
import com.adamratzman.spotify.models.CurrentlyPlayingContext
@@ -153,7 +153,7 @@ class ClientPlayerApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
153153
}
154154

155155
/**
156-
* Set the repeat mode for the user’s playback. Options are repeat-track, repeat-context, and off.
156+
* Set the repeat mode for the user’s playback. Options are [PlayerRepeatState.TRACK], [PlayerRepeatState.CONTEXT], and [PlayerRepeatState.OFF].
157157
*
158158
* **Requires** the [SpotifyScope.USER_MODIFY_PLAYBACK_STATE] scope
159159
*
@@ -264,7 +264,7 @@ class ClientPlayerApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
264264
offsetNum: Int? = null,
265265
offsetTrackId: String? = null,
266266
deviceId: String? = null,
267-
vararg tracksToPlay: String
267+
tracksToPlay: List<String> = listOf()
268268
): SpotifyRestAction<Unit> {
269269
return toAction {
270270
val url = EndpointBuilder("/me/player/play").with("device_id", deviceId).toString()
@@ -330,13 +330,14 @@ class ClientPlayerApi(api: SpotifyApi<*, *>) : SpotifyEndpoint(api) {
330330
* @param deviceId The device to play on
331331
* @param play Whether to immediately start playback on the transferred device
332332
*/
333-
fun transferPlayback(vararg deviceId: String, play: Boolean = true): SpotifyRestAction<Unit> {
334-
require(deviceId.size <= 1) { "Although an array is accepted, only a single device_id is currently supported. Supplying more than one will 400 Bad Request" }
333+
@SpotifyExperimentalFunctionApi
334+
fun transferPlayback(deviceId: String, play: Boolean? = null): SpotifyRestAction<Unit> {
335+
// require(deviceId.size <= 1) { "Although an array is accepted, only a single device_id is currently supported. Supplying more than one will 400 Bad Request" }
335336
return toAction {
336-
put(
337-
EndpointBuilder("/me/player").with("device_ids", deviceId.joinToString(",") { it.encodeUrl() })
338-
.with("play", play).toString()
339-
)
337+
val json = jsonMap()
338+
play?.let { json += json { "play" to it } }
339+
json += json { "device_ids" to JsonArray(listOf(deviceId).map(::JsonPrimitive)) }
340+
put(EndpointBuilder("/me/player").toString(), json.toJson())
340341
Unit
341342
}
342343
}

src/commonTest/kotlin/com.adamratzman/spotify/utilities/RestActionTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2020; Original author: Adam Ratzman */
22
package com.adamratzman.spotify.utilities
33

4+
import com.adamratzman.spotify.annotations.SpotifyExperimentalHttpApi
45
import com.adamratzman.spotify.api
56
import com.adamratzman.spotify.endpoints.public.SearchApi.SearchType.TRACK
67
import com.adamratzman.spotify.utils.runBlocking
78
import kotlin.test.assertEquals
89
import org.spekframework.spek2.Spek
910
import org.spekframework.spek2.style.specification.describe
1011

12+
@SpotifyExperimentalHttpApi
1113
class RestActionTests : Spek({
1214
describe("Paging Object") {
1315
it("next test") {

0 commit comments

Comments
 (0)