Skip to content

Commit 8e8daaf

Browse files
committed
update tests to reflect new 404 on playlist follow
additionally, update documentation for that method and change klaxon dependency to compile
1 parent 18612ed commit 8e8daaf

File tree

8 files changed

+55
-76
lines changed

8 files changed

+55
-76
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ dependencies {
5454
This library should work out of the box on Android.
5555

5656
## Documentation
57-
The `spotify-web-api-kotlin` KDocs are hosted at https://adamint.github.io/spotify-web-api-kotlin
57+
The `spotify-web-api-kotlin` JavaDocs are hosted at https://adamint.github.io/spotify-web-api-kotlin
5858

5959
## Creating a SpotifyAPI or SpotifyClientAPI object
6060
In order to use the methods in this library, you must create either a `SpotifyAPI` or `SpotifyClientAPI` object using their respective exposed builders. Client-specific methods are unable to be accessed with the generic SpotifyAPI, rather you must create an instance of the Client API.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ repositories {
3636

3737
dependencies {
3838
// Actual library dependencies
39-
implementation 'com.beust:klaxon:5.0.5'
39+
compile 'com.beust:klaxon:5.0.5'
4040

4141
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
4242
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

src/main/kotlin/com/adamratzman/spotify/endpoints/public/FollowingAPI.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ package com.adamratzman.spotify.endpoints.public
33

44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6-
import com.adamratzman.spotify.utils.BadRequestException
7-
import com.adamratzman.spotify.utils.EndpointBuilder
8-
import com.adamratzman.spotify.utils.PlaylistURI
9-
import com.adamratzman.spotify.utils.SpotifyEndpoint
10-
import com.adamratzman.spotify.utils.UserURI
11-
import com.adamratzman.spotify.utils.encode
12-
import com.adamratzman.spotify.utils.toArray
6+
import com.adamratzman.spotify.utils.*
137
import java.util.function.Supplier
148

159
/**
@@ -25,7 +19,7 @@ open class FollowingAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
2519
*
2620
* @return List of Booleans representing whether the user follows the playlist. User IDs **not** found will return false
2721
*
28-
* @throws [BadRequestException] if the playlist is not found
22+
* @throws [BadRequestException] if the playlist is not found OR any user in the list does not exist
2923
*/
3024
fun areFollowingPlaylist(
3125
playlistOwner: String,
@@ -50,7 +44,7 @@ open class FollowingAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5044
*
5145
* @return booleans representing whether the user follows the playlist. User IDs **not** found will return false
5246
*
53-
* @throws [BadRequestException] if the playlist is not found
47+
* @throws [BadRequestException] if the playlist is not found or if the user does not exist
5448
*/
5549
fun isFollowingPlaylist(playlistOwner: String, playlist: String, user: String): SpotifyRestAction<Boolean> {
5650
return toAction(Supplier {

src/main/kotlin/com/adamratzman/spotify/endpoints/public/PlaylistsAPI.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,7 @@ package com.adamratzman.spotify.endpoints.public
44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
66
import com.adamratzman.spotify.main.SpotifyRestActionPaging
7-
import com.adamratzman.spotify.utils.BadRequestException
8-
import com.adamratzman.spotify.utils.EndpointBuilder
9-
import com.adamratzman.spotify.utils.Market
10-
import com.adamratzman.spotify.utils.PagingObject
11-
import com.adamratzman.spotify.utils.Playlist
12-
import com.adamratzman.spotify.utils.PlaylistTrack
13-
import com.adamratzman.spotify.utils.PlaylistURI
14-
import com.adamratzman.spotify.utils.SimplePlaylist
15-
import com.adamratzman.spotify.utils.SpotifyEndpoint
16-
import com.adamratzman.spotify.utils.SpotifyImage
17-
import com.adamratzman.spotify.utils.UserURI
18-
import com.adamratzman.spotify.utils.catch
19-
import com.adamratzman.spotify.utils.encode
20-
import com.adamratzman.spotify.utils.toArray
21-
import com.adamratzman.spotify.utils.toObject
22-
import com.adamratzman.spotify.utils.toPagingObject
7+
import com.adamratzman.spotify.utils.*
238
import java.util.function.Supplier
249

2510
/**
@@ -37,6 +22,8 @@ open class PlaylistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3722
* @return [PagingObject] of [SimplePlaylist]s **ONLY if** the user can be found. Otherwise, an empty paging object is returned.
3823
* This does not have the detail of full [Playlist] objects.
3924
*
25+
* @throws BadRequestException if the user is not found (404)
26+
*
4027
*/
4128
fun getPlaylists(
4229
user: String,

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.beust.klaxon.Json
77
import com.beust.klaxon.JsonBase
88
import com.beust.klaxon.Klaxon
99
import java.net.URLEncoder
10-
import java.util.Base64
10+
import java.util.*
1111

1212
/**
1313
* The cursor to use as key to find the next (or previous) page of items.
@@ -33,8 +33,8 @@ internal inline fun <reified T> String.toObjectNullable(o: SpotifyAPI?): T? = tr
3333
internal inline fun <reified T> String.toObject(o: SpotifyAPI?): T {
3434
val klaxon = o?.klaxon ?: Klaxon()
3535
val obj = klaxon.parse<T>(this) ?: throw SpotifyException(
36-
"Unable to parse $this",
37-
IllegalArgumentException("$this not found")
36+
"Unable to parse $this",
37+
IllegalArgumentException("$this not found")
3838
)
3939
o?.let {
4040
if (obj is Linkable) obj.api = o
@@ -54,46 +54,46 @@ internal inline fun <reified T> String.toArray(o: SpotifyAPI?): List<T> {
5454
}
5555
}
5656
} ?: throw SpotifyException(
57-
"Unable to parse $this",
58-
IllegalArgumentException("$this not found")
57+
"Unable to parse $this",
58+
IllegalArgumentException("$this not found")
5959
)
6060
}
6161

6262
internal inline fun <reified T> String.toPagingObject(
63-
innerObjectName: String? = null,
64-
endpoint: SpotifyEndpoint
63+
innerObjectName: String? = null,
64+
endpoint: SpotifyEndpoint
6565
): PagingObject<T> {
6666
val jsonObject = endpoint.api.klaxon.parseJsonObject(this.reader())
67-
.let { if (innerObjectName != null) it.obj(innerObjectName)!! else it }
67+
.let { if (innerObjectName != null) it.obj(innerObjectName)!! else it }
6868

6969
return PagingObject(
70-
jsonObject.string("href")!!,
71-
(jsonObject["items"] as JsonBase).toJsonString().toArray<T>(endpoint.api),
72-
jsonObject.int("limit")!!,
73-
jsonObject.string("next"),
74-
jsonObject.int("offset")!!,
75-
jsonObject.string("previous"),
76-
jsonObject.int("total")!!
70+
jsonObject.string("href")!!,
71+
(jsonObject["items"] as JsonBase).toJsonString().toArray<T>(endpoint.api),
72+
jsonObject.int("limit")!!,
73+
jsonObject.string("next"),
74+
jsonObject.int("offset")!!,
75+
jsonObject.string("previous"),
76+
jsonObject.int("total")!!
7777
).apply {
7878
this.endpoint = endpoint
7979
this.itemClazz = T::class.java
8080
}
8181
}
8282

8383
internal inline fun <reified T> String.toCursorBasedPagingObject(
84-
innerObjectName: String? = null,
85-
endpoint: SpotifyEndpoint
84+
innerObjectName: String? = null,
85+
endpoint: SpotifyEndpoint
8686
): CursorBasedPagingObject<T> {
8787
val jsonObject = endpoint.api.klaxon.parseJsonObject(this.reader())
88-
.let { if (innerObjectName != null) it.obj(innerObjectName)!! else it }
88+
.let { if (innerObjectName != null) it.obj(innerObjectName)!! else it }
8989

9090
return CursorBasedPagingObject(
91-
jsonObject.string("href")!!,
92-
(jsonObject["items"] as JsonBase).toJsonString().toArray<T>(endpoint.api),
93-
jsonObject.int("limit")!!,
94-
jsonObject.string("next"),
95-
endpoint.api.klaxon.parseFromJsonObject(jsonObject.obj("cursors")!!)!!,
96-
if (jsonObject.containsKey("total")) jsonObject.int("total")!! else -1
91+
jsonObject.string("href")!!,
92+
(jsonObject["items"] as JsonBase).toJsonString().toArray<T>(endpoint.api),
93+
jsonObject.int("limit")!!,
94+
jsonObject.string("next"),
95+
endpoint.api.klaxon.parseFromJsonObject(jsonObject.obj("cursors")!!)!!,
96+
if (jsonObject.containsKey("total")) jsonObject.int("total")!! else -1
9797
).apply {
9898
this.endpoint = endpoint
9999
this.itemClazz = T::class.java
@@ -104,14 +104,14 @@ internal inline fun <reified T> String.toInnerObject(innerName: String, api: Spo
104104
val jsonObject = api.klaxon.parseJsonObject(this.reader())
105105

106106
return jsonObject.obj(innerName)?.let { api.klaxon.parseFromJsonObject<T>(it) }
107-
?: throw SpotifyException("Unable to parse $this into $innerName (${T::class})", IllegalArgumentException())
107+
?: throw SpotifyException("Unable to parse $this into $innerName (${T::class})", IllegalArgumentException())
108108
}
109109

110110
internal inline fun <reified T> String.toInnerArray(innerName: String, api: SpotifyAPI): List<T> {
111111
val jsonObject = api.klaxon.parseJsonObject(this.reader())
112112

113113
return jsonObject.array(innerName)
114-
?: throw SpotifyException("Unable to parse $this into $innerName (${T::class})", IllegalArgumentException())
114+
?: throw SpotifyException("Unable to parse $this into $innerName (${T::class})", IllegalArgumentException())
115115
}
116116

117117
internal fun <T> catch(function: () -> T): T? {

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,20 @@ data class Track(
403403
* "restrictions" : {"reason" : "market"}
404404
*/
405405
data class SimpleAlbum(
406-
@Json(name = "album_type", ignored = false) private val _albumType: String,
407-
val artists: List<SimpleArtist>,
408-
@Json(name = "available_markets") val availableMarkets: List<String>? = null,
409-
@Json(name = "external_urls") val externalUrls: HashMap<String, String>,
410-
val href: String,
411-
val id: String,
412-
val images: List<SpotifyImage>,
413-
val name: String,
414-
val type: String,
415-
@Json(name = "uri", ignored = false) private val _uri: String,
416-
@Json(ignored = true) val uri: AlbumURI = AlbumURI(_uri),
417-
@Json(name = "release_date") val releaseDate: String,
418-
@Json(name = "release_date_precision") val releaseDatePrecision: String,
419-
@Json(name = "total_tracks") val totalTracks: Int? = null,
406+
@Json(name = "album_type", ignored = false) private val _albumType: String,//
407+
val artists: List<SimpleArtist>,//
408+
@Json(name = "available_markets") val availableMarkets: List<String>? = null,//
409+
@Json(name = "external_urls") val externalUrls: HashMap<String, String>,//
410+
val href: String,//
411+
val id: String,//
412+
val images: List<SpotifyImage>,//
413+
val name: String,//
414+
val type: String,//
415+
@Json(name = "uri", ignored = false) private val _uri: String,//
416+
@Json(ignored = true) val uri: AlbumURI = AlbumURI(_uri),//
417+
@Json(name = "release_date") val releaseDate: String,//
418+
@Json(name = "release_date_precision") val releaseDatePrecision: String,//
419+
@Json(name = "total_tracks") val totalTracks: Int? = null,//
420420
@Json(name = "album_group", ignored = false) private val albumGroupString: String? = null,
421421
val restrictions: Restrictions? = null,
422422
@Json(ignored = true) val albumGroup: AlbumResultType? = albumGroupString?.let { _ ->

src/test/kotlin/com/adamratzman/spotify/public/PublicFollowingAPITest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package com.adamratzman.spotify.public
44
import com.adamratzman.spotify.api
55
import com.adamratzman.spotify.utils.BadRequestException
66
import org.junit.jupiter.api.Assertions.assertEquals
7-
import org.junit.jupiter.api.Assertions.assertFalse
87
import org.junit.jupiter.api.assertThrows
98
import org.spekframework.spek2.Spek
109
import org.spekframework.spek2.style.specification.describe
@@ -14,7 +13,7 @@ class PublicFollowingAPITest : Spek({
1413
val f = api.following
1514
describe("do users follow playlist") {
1615
it("invalid users, valid playlist") {
17-
assertFalse(f.areFollowingPlaylist("spotify", "37i9dQZF1DXcBWIGoYBM5M", "udontexist89").complete()[0])
16+
assertThrows<BadRequestException> { f.areFollowingPlaylist("spotify", "37i9dQZF1DXcBWIGoYBM5M", "udontexist89").complete()[0] }
1817
}
1918
it("no users, valid playlist") {
2019
assertThrows<BadRequestException> {
@@ -31,8 +30,9 @@ class PublicFollowingAPITest : Spek({
3130
f.areFollowingPlaylist("spotify", "37i9dQZF1DXcBWIGoYBM5M", "adamratzman1", "adamratzman").complete())
3231
}
3332
it("mix of valid and invalid users, valid playlist") {
34-
assertEquals(listOf(false, true),
35-
f.areFollowingPlaylist("spotify", "37i9dQZF1DXcBWIGoYBM5M", "udontexist89", "adamratzman1").complete())
33+
assertThrows<BadRequestException> {
34+
f.areFollowingPlaylist("spotify", "37i9dQZF1DXcBWIGoYBM5M", "udontexist89", "adamratzman1").complete()
35+
}
3636
}
3737
}
3838
}

src/test/kotlin/com/adamratzman/spotify/public/PublicPlaylistsAPITest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package com.adamratzman.spotify.public
33

44
import com.adamratzman.spotify.api
55
import com.adamratzman.spotify.utils.BadRequestException
6-
import org.junit.jupiter.api.Assertions.assertEquals
7-
import org.junit.jupiter.api.Assertions.assertNull
8-
import org.junit.jupiter.api.Assertions.assertTrue
6+
import org.junit.jupiter.api.Assertions.*
97
import org.junit.jupiter.api.assertThrows
108
import org.spekframework.spek2.Spek
119
import org.spekframework.spek2.style.specification.describe
@@ -20,8 +18,8 @@ class PublicPlaylistsAPITest : Spek({
2018
assertTrue(p.getPlaylists("adamratzman1").complete().isNotEmpty())
2119
assertTrue(p.getPlaylists("adamratzman1").complete().isNotEmpty())
2220
}
23-
it("unknown user should throw exception but doesn't. if the id is valid, the list should be empty") {
24-
assertEquals(0, p.getPlaylists("non-existant-user").complete().size)
21+
it("unknown user should throw exception") {
22+
assertThrows<BadRequestException> { p.getPlaylists("non-existant-user").complete().size }
2523
}
2624
}
2725
describe("get playlist") {

0 commit comments

Comments
 (0)