Skip to content

Commit daeb01d

Browse files
committed
get [track, album, playlist] is now nullable
1 parent 9b366fb commit daeb01d

File tree

8 files changed

+19
-11
lines changed

8 files changed

+19
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This library is available via Maven Central.
1515
<dependency>
1616
<groupId>com.adamratzman</groupId>
1717
<artifactId>spotify-api-kotlin</artifactId>
18-
<version>1.0.1</version>
18+
<version>1.0.2</version>
1919
</dependency>
2020
```
2121

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.1'
2+
version '1.0.2'
33

44
buildscript {
55
ext.kotlin_version = '1.2.31'

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/album/AlbumAPI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class AlbumAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1616
*
1717
* @throws BadRequestException if the [albumId] is not found
1818
*/
19-
fun getAlbum(albumId: String, market: Market? = null): SpotifyRestAction<Album> {
19+
fun getAlbum(albumId: String, market: Market? = null): SpotifyRestAction<Album?> {
2020
return toAction(Supplier {
21-
get("https://api.spotify.com/v1/albums/$albumId${if (market != null) "?market=${market.code}" else ""}").toObject<Album>(api)
21+
catch { get("https://api.spotify.com/v1/albums/$albumId${if (market != null) "?market=${market.code}" else ""}").toObject<Album>(api) }
2222
})
2323
}
2424

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/playlists/PlaylistsAPI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class PlaylistsAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3434
*
3535
* @throws BadRequestException if the playlist is not found
3636
*/
37-
fun getPlaylist(userId: String, playlistId: String, market: Market? = null): SpotifyRestAction<Playlist> {
37+
fun getPlaylist(userId: String, playlistId: String, market: Market? = null): SpotifyRestAction<Playlist?> {
3838
return toAction(Supplier {
39-
get("https://api.spotify.com/v1/users/${userId.encode()}/playlists/${playlistId.encode()}${if (market != null) "?market=${market.code}" else ""}").toObject<Playlist>(api)
39+
catch {get ("https://api.spotify.com/v1/users/${userId.encode()}/playlists/${playlistId.encode()}${if (market != null) "?market=${market.code}" else ""}").toObject<Playlist>(api)}
4040
})
4141
}
4242

src/main/kotlin/com/adamratzman/spotify/endpoints/pub/tracks/TracksAPI.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class TracksAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
1717
*
1818
* @throws BadRequestException if [trackId] cannot be found
1919
*/
20-
fun getTrack(trackId: String, market: Market? = null): SpotifyRestAction<Track> {
20+
fun getTrack(trackId: String, market: Market? = null): SpotifyRestAction<Track?> {
2121
return toAction(Supplier {
22-
get("https://api.spotify.com/v1/tracks/${trackId.encode()}${if (market != null) "?market=${market.code}" else ""}").toObject<Track>(api)
22+
catch { get("https://api.spotify.com/v1/tracks/${trackId.encode()}${if (market != null) "?market=${market.code}" else ""}").toObject<Track>(api) }
2323
})
2424
}
2525

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,12 @@ inline fun <reified T> String.toLinkedResult(api: SpotifyAPI): LinkedResult<T> {
137137

138138
inline fun <reified T> String.toInnerObject(innerName: String, api: SpotifyAPI): List<T> {
139139
return JSONObject(this).getJSONArray(innerName).map { it.toString().toObject<T>(api) }
140+
}
141+
142+
fun <T> catch(function: () -> T):T? {
143+
return try{
144+
function()
145+
} catch (e:BadRequestException) {
146+
null
147+
}
140148
}

src/test/kotlin/com/adamratzman/spotify/endpoints/priv/playlists/ClientPlaylistAPITest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class ClientPlaylistAPITest {
3131
@Test
3232
fun uploadPlaylistCover() {
3333
println(clientApi.token.access_token)
34-
println(clientApi.playlists.getPlaylist("adamratzman1", "24LoWIwSxOTzaq44qLabiV").complete().name)
34+
println(clientApi.playlists.getPlaylist("adamratzman1", "24LoWIwSxOTzaq44qLabiV").complete()?.name)
3535
clientApi.clientPlaylists.uploadPlaylistCover("adamratzman1", "24LoWIwSxOTzaq44qLabiV",
3636
imageUrl = "https://upload.wikimedia.org/wikipedia/commons/3/3a/Cat03.jpg").complete()
3737
}
@@ -41,7 +41,7 @@ internal class ClientPlaylistAPITest {
4141
clientApi.clientPlaylists.addTracksToPlaylist("adamratzman1", "53Sr94VRrDgvLCh86lbMVx", "3ghmFXEXTP6DdVOyvuPHpr")
4242
.complete()
4343
clientApi.playlists.getPlaylist("adamratzman1", "53Sr94VRrDgvLCh86lbMVx").complete()
44-
.tracks.items.map { it.track.id }.let { println(it) }
44+
?.tracks?.items?.map { it.track.id }.let { println(it) }
4545
//clientApi.clientPlaylists.removeAllOccurances("adamratzman1", "53Sr94VRrDgvLCh86lbMVx",
4646
// "3ghmFXEXTP6DdVOyvuPHpr").complete()
4747
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class PlaylistsAPITest {
1111

1212
@Test
1313
fun getPlaylist() {
14-
println(api.playlists.getPlaylist("spotify", "59ZbFPES4DQwEjBpWHzrtC").complete())
14+
println(api.playlists.getPlaylist("spotify", "5dsaf9ZbFPES4DQwEjBpWHzrtC").complete())
1515
}
1616

1717
@Test

0 commit comments

Comments
 (0)