Skip to content

Commit 6889cbc

Browse files
committed
add getQueue function for player
1 parent 6f4dc31 commit 6889cbc

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.adamratzman.spotify.SpotifyException.BadRequestException
88
import com.adamratzman.spotify.SpotifyScope
99
import com.adamratzman.spotify.http.SpotifyEndpoint
1010
import com.adamratzman.spotify.models.ContextUri
11+
import com.adamratzman.spotify.models.CurrentUserQueue
1112
import com.adamratzman.spotify.models.CurrentlyPlayingContext
1213
import com.adamratzman.spotify.models.CurrentlyPlayingObject
1314
import com.adamratzman.spotify.models.CurrentlyPlayingType
@@ -93,6 +94,21 @@ public class ClientPlayerApi(api: GenericSpotifyApi) : SpotifyEndpoint(api) {
9394
return if (obj?.timestamp == null) null else obj
9495
}
9596

97+
/**
98+
* Get the list of objects that make up the user's queue.
99+
*
100+
* **Requires** the [SpotifyScope.USER_READ_CURRENTLY_PLAYING] scope
101+
*
102+
* Note that queue length may, in some cases, contain tracks that will play after the user-specified queue.
103+
*
104+
* **[Api Reference](https://developer.spotify.com/documentation/web-api/reference/#/operations/get-queue)**
105+
*/
106+
public suspend fun getUserQueue(): CurrentUserQueue {
107+
return get(
108+
endpointBuilder("/me/player/queue").toString()
109+
).toObject(CurrentUserQueue.serializer(), api = api, json = json)
110+
}
111+
96112
/**
97113
* Get tracks from the current user’s recently played tracks. Note: Currently doesn't support podcast episodes.
98114
*

src/commonMain/kotlin/com.adamratzman.spotify/models/Player.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ public data class PlaybackActions(
168168
}
169169
}
170170

171+
@Serializable
172+
public data class CurrentUserQueue(
173+
@SerialName("currently_playing") val currentlyPlaying: Playable? = null,
174+
@SerialName("queue") val queue: List<Playable>
175+
)
176+
171177
/**
172178
* Maps a playback action to whether the user is disallowed from doing it
173179
*

src/commonMain/kotlin/com.adamratzman.spotify/models/SpotifyUris.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ private fun String.add(type: String, allowColon: Boolean): String {
3838
}
3939

4040
private fun String.remove(type: String, allowColon: Boolean): String {
41-
println(type)
4241
if (type == UserCollectionUriType && matchesUserCollectionUri()) {
4342
return "collection"
4443
} else {

src/commonTest/kotlin/com.adamratzman/spotify/priv/ClientPlayerApiTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.adamratzman.spotify.models.SpotifyContextType
1313
import com.adamratzman.spotify.models.SpotifyTrackUri
1414
import com.adamratzman.spotify.models.toAlbumUri
1515
import com.adamratzman.spotify.models.toArtistUri
16+
import com.adamratzman.spotify.models.toEpisodeUri
1617
import com.adamratzman.spotify.models.toPlaylistUri
1718
import com.adamratzman.spotify.models.toShowUri
1819
import com.adamratzman.spotify.models.toTrackUri
@@ -271,4 +272,40 @@ class ClientPlayerApiTest : AbstractTest<SpotifyClientApi>() {
271272

272273
assertEquals(toDevice.id, api.player.getCurrentContext()!!.device.id)
273274
}
275+
276+
@Test
277+
fun testGetCurrentQueue(): TestResult = runTestOnDefaultDispatcher {
278+
buildApi<SpotifyClientApi>(::testGetCurrentQueue.name)
279+
if (!arePlayerTestsEnabled()) return@runTestOnDefaultDispatcher
280+
if (!isApiInitialized()) return@runTestOnDefaultDispatcher
281+
282+
val device = api.player.getDevices().first()
283+
284+
val trackId = "1VsVY1ySdH3nVSWnLT5vCf"
285+
api.player.startPlayback(
286+
playableUrisToPlay = listOf(PlayableUri("spotify:track:$trackId")),
287+
deviceId = device.id
288+
)
289+
delay(playbackRelatedDelayMs)
290+
291+
api.player.getUserQueue().let { playedSingleTrackNoQueueQueueResponse ->
292+
assertEquals(trackId, playedSingleTrackNoQueueQueueResponse.currentlyPlaying?.asTrack?.id)
293+
}
294+
295+
api.player.skipForward(deviceId = device.id)
296+
delay(playbackRelatedDelayMs)
297+
298+
val episodeId = "4gQNhlqd3jg5QTh7umdRXT"
299+
api.player.startPlayback(
300+
playableUrisToPlay = listOf(episodeId.toEpisodeUri(), trackId.toTrackUri()),
301+
deviceId = device.id
302+
)
303+
delay(playbackRelatedDelayMs)
304+
305+
api.player.getUserQueue().let { playingWithQueueResponse ->
306+
assertEquals(episodeId, playingWithQueueResponse.currentlyPlaying?.id)
307+
assertEquals(trackId, playingWithQueueResponse.queue[0].id)
308+
}
309+
310+
}
274311
}

0 commit comments

Comments
 (0)