Skip to content

Commit 17b8880

Browse files
committed
add missing parameters
1 parent 8b40792 commit 17b8880

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

.idea/modules/SpotifyKotlinWrapper.iml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/SpotifyKotlinWrapper_main.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/SpotifyKotlinWrapper_test.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import java.util.function.Supplier
88
* Endpoints for retrieving information about the user’s listening habits.
99
*/
1010
class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
11+
enum class TimeRange(val id: String) {
12+
LONG_TERM("long_term"), MEDIUM_TERM("medium_term"), SHORT_TERM("short_term");
13+
override fun toString() = id
14+
}
15+
1116
/**
1217
* Get the current user’s top artists based on calculated affinity.
1318
* Affinity is a measure of the expected preference a user has for a particular track or artist. It is based on user
@@ -20,9 +25,10 @@ class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2025
*
2126
* @return [PagingObject] of full [Artist] objects sorted by affinity
2227
*/
23-
fun getTopArtists(): SpotifyRestAction<PagingObject<Artist>> {
28+
fun getTopArtists(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestAction<PagingObject<Artist>> {
2429
return toAction(Supplier {
25-
get(EndpointBuilder("/me/top/artists").build()).toPagingObject<Artist>(endpoint = this)
30+
get(EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
31+
.with("time_range", timeRange).build()).toPagingObject<Artist>(endpoint = this)
2632
})
2733
}
2834

@@ -38,9 +44,10 @@ class PersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3844
*
3945
* @return [PagingObject] of full [Track] objects sorted by affinity
4046
*/
41-
fun getTopTracks(): SpotifyRestAction<PagingObject<Track>> {
47+
fun getTopTracks(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestAction<PagingObject<Track>> {
4248
return toAction(Supplier {
43-
get(EndpointBuilder("/me/top/tracks").build()).toPagingObject<Track>(endpoint = this)
49+
get(EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
50+
.with("time_range", timeRange).build()).toPagingObject<Track>(endpoint = this)
4451
})
4552
}
4653

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/artists/ArtistsAPI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4242
*
4343
* @throws BadRequestException if [artistId] is not found, or filter parameters are illegal
4444
*/
45-
fun getArtistAlbums(artistId: String, limit: Int = 20, offset: Int = 0, market: Market? = null, include: List<AlbumInclusionStrategy> = listOf()): SpotifyRestAction<LinkedResult<SimpleAlbum>> {
45+
fun getArtistAlbums(artistId: String, limit: Int? = null, offset: Int? = null, market: Market? = null, include: List<AlbumInclusionStrategy> = listOf()): SpotifyRestAction<LinkedResult<SimpleAlbum>> {
4646
return toAction(Supplier {
4747
get(EndpointBuilder("/artists/${artistId.encode()}/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
4848
.with("include_groups", include.joinToString(",") { it.keyword }).build()).toLinkedResult<SimpleAlbum>(api)
@@ -60,7 +60,7 @@ class ArtistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
6060
*
6161
* @throws BadRequestException if tracks are not available in the specified [Market] or the [artistId] is not found
6262
*/
63-
fun getArtistTopTracks(artistId: String, market: Market): SpotifyRestAction<List<Track>> {
63+
fun getArtistTopTracks(artistId: String, market: Market = Market.US): SpotifyRestAction<List<Track>> {
6464
return toAction(Supplier {
6565
get(EndpointBuilder("/artists/${artistId.encode()}/top-tracks").with("country", market.code).build())
6666
.toObject<TrackList>(api).tracks.map { it!! }

src/main/kotlin/com/adamratzman/spotify/utils/Helpers.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ abstract class SpotifyEndpoint(val api: SpotifyAPI) {
5757
}
5858

5959
internal class EndpointBuilder(val path: String) {
60-
private val builder = StringBuilder(base)
60+
val builder = StringBuilder(base)
61+
6162
init {
6263
builder.append(path)
6364
}
64-
fun with(key: String, value: Any?):EndpointBuilder {
65-
if (value != null && (value !is String || value.isNotEmpty()) ) {
65+
66+
fun with(key: String, value: Any?): EndpointBuilder {
67+
if (value != null && (value !is String || value.isNotEmpty())) {
6668
if (builder.toString() == base + path) builder.append("?")
6769
else builder.append("&")
6870
builder.append(key).append("=").append(value.toString())
@@ -74,11 +76,29 @@ internal class EndpointBuilder(val path: String) {
7476
}
7577

7678
data class CursorBasedPagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String?, val cursors: Cursor,
77-
val total: Int, val endpoint: SpotifyEndpoint)
79+
val total: Int, val endpoint: SpotifyEndpoint) {
80+
inline fun <reified T> getNext(): SpotifyRestAction<CursorBasedPagingObject<T>> = endpoint.toAction(
81+
Supplier {
82+
next?.let { endpoint.get(it).toCursorBasedPagingObject<T>(endpoint = endpoint) }
83+
?: throw IllegalStateException("PagingObject#next is null!")
84+
})
85+
}
7886

7987
data class Cursor(val after: String)
8088
data class PagingObject<out T>(val href: String, val items: List<T>, val limit: Int, val next: String? = null, val offset: Int = 0,
81-
val previous: String? = null, val total: Int, val endpoint: SpotifyEndpoint)
89+
val previous: String? = null, val total: Int, val endpoint: SpotifyEndpoint) {
90+
inline fun <reified T> getNext(): SpotifyRestAction<PagingObject<T>> = endpoint.toAction(
91+
Supplier {
92+
next?.let { endpoint.get(it).toPagingObject<T>(endpoint = endpoint) }
93+
?: throw IllegalStateException("PagingObject#next is null!")
94+
})
95+
96+
inline fun <reified T> getPrevious(): SpotifyRestAction<PagingObject<T>> = endpoint.toAction(
97+
Supplier {
98+
previous?.let { endpoint.get(it).toPagingObject<T>(endpoint = endpoint) }
99+
?: throw IllegalStateException("PagingObject#previous is null!")
100+
})
101+
}
82102

83103
data class LinkedResult<out T>(val href: String, val items: List<T>) {
84104
fun toPlaylistParams(): PlaylistParams {

src/test/kotlin/com/adamratzman/spotify/endpoints/pub/playlists/PlaylistsAPITest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.adamratzman.spotify.kotlin.endpoints.pub.playlists
22

33
import api
4+
import com.adamratzman.spotify.utils.SimplePlaylist
45
import org.junit.Test
56

67
internal class PlaylistsAPITest {
78
@Test
89
fun getPlaylists() {
9-
println(api.playlists.getPlaylists("wizzler").complete())
10+
println(api.playlists.getPlaylists("wizzler", limit = 3).complete().getNext<SimplePlaylist>().complete())
1011
}
1112

1213
@Test

0 commit comments

Comments
 (0)