Skip to content

Commit ffa5d57

Browse files
authored
Merge pull request #40 from adamint/kotlinx.serialization
Kotlinx.serialization
2 parents b800bce + accc638 commit ffa5d57

30 files changed

+838
-402
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
.idea/**/gradle.xml
3030
.idea/**/libraries
3131

32-
# Gradle and Maven with auto-import
33-
# When using Gradle or Maven with auto-import, you should exclude module files,
32+
# Gradle and Maven with auto-# When using Gradle or Maven with auto-import, you should exclude module files,
3433
# since they will be recreated, and may cause churn. Uncomment if using
3534
# auto-import.
3635
.idea/modules.xml

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ dependencies {
3737
}
3838
```
3939

40+
#### Android
41+
This library should work out of the box on Android.
42+
```
43+
4044
## Creating a SpotifyAPI or SpotifyClientAPI object
4145
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.
4246

build.gradle

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
buildscript {
2+
ext.kotlin_version = '1.3.11'
3+
repositories { jcenter() }
4+
5+
dependencies {
6+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
7+
}
8+
}
9+
10+
111
plugins {
212
id "com.diffplug.gradle.spotless" version "3.16.0"
313
id "base"
414
id "io.codearte.nexus-staging" version "0.12.0"
515
id "com.bmuschko.nexus" version "2.3.1"
6-
id "org.jetbrains.kotlin.jvm" version "1.3.11"
716
}
817

18+
apply plugin: 'kotlin'
19+
920
group 'com.adamratzman'
1021
version '2.0.1'
1122

@@ -15,17 +26,19 @@ sourceCompatibility = 1.8
1526

1627
repositories {
1728
mavenCentral()
29+
jcenter()
1830
maven { url "https://dl.bintray.com/spekframework/spek" }
1931
}
2032

2133
dependencies {
2234
// Actual library dependencies
2335
implementation(group: 'org.json', name: 'json', version: '20180130')
24-
implementation("com.google.code.gson:gson:2.8.1")
2536

26-
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.11"
27-
compile "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
37+
implementation 'com.beust:klaxon:5.0.1'
2838

39+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
40+
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
41+
2942
// Spek testing requirements
3043
testImplementation('org.spekframework.spek2:spek-dsl-jvm:2.0.0-rc.1') {
3144
exclude group: 'org.jetbrains.kotlin'
@@ -37,7 +50,7 @@ dependencies {
3750

3851
testImplementation('org.junit.jupiter:junit-jupiter-api:5.2.0')
3952
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
40-
testRuntimeOnly "org.jetbrains.kotlin:kotlin-reflect:1.3.11"
53+
testRuntimeOnly "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
4154
}
4255

4356
spotless {

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientFollowingAPI.kt

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.adamratzman.spotify.endpoints.public.FollowingAPI
55
import com.adamratzman.spotify.main.SpotifyAPI
66
import com.adamratzman.spotify.main.SpotifyClientAPI
77
import com.adamratzman.spotify.main.SpotifyRestAction
8-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
8+
import com.adamratzman.spotify.main.SpotifyRestActionPaging
99
import com.adamratzman.spotify.utils.Artist
1010
import com.adamratzman.spotify.utils.ArtistURI
1111
import com.adamratzman.spotify.utils.CursorBasedPagingObject
@@ -14,8 +14,8 @@ import com.adamratzman.spotify.utils.PlaylistURI
1414
import com.adamratzman.spotify.utils.SpotifyPublicUser
1515
import com.adamratzman.spotify.utils.UserURI
1616
import com.adamratzman.spotify.utils.encode
17+
import com.adamratzman.spotify.utils.toArray
1718
import com.adamratzman.spotify.utils.toCursorBasedPagingObject
18-
import com.adamratzman.spotify.utils.toObject
1919
import java.util.function.Supplier
2020

2121
/**
@@ -46,7 +46,13 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
4646
* @throws [BadRequestException] if the playlist is not found
4747
*/
4848
fun isFollowingPlaylist(playlistOwner: String, playlistId: String): SpotifyRestAction<Boolean> {
49-
return toAction(Supplier { isFollowingPlaylist(playlistOwner, playlistId, (api as SpotifyClientAPI).userId).complete() })
49+
return toAction(Supplier {
50+
isFollowingPlaylist(
51+
playlistOwner,
52+
playlistId,
53+
(api as SpotifyClientAPI).userId
54+
).complete()
55+
})
5056
}
5157

5258
/**
@@ -58,8 +64,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
5864
*/
5965
fun isFollowingUsers(vararg users: String): SpotifyRestAction<List<Boolean>> {
6066
return toAction(Supplier {
61-
get(EndpointBuilder("/me/following/contains").with("type", "user")
62-
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
67+
get(
68+
EndpointBuilder("/me/following/contains").with("type", "user")
69+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()
70+
).toArray<Boolean>(api)
6371
})
6472
}
6573

@@ -85,8 +93,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
8593
*/
8694
fun isFollowingArtists(vararg artists: String): SpotifyRestAction<List<Boolean>> {
8795
return toAction(Supplier {
88-
get(EndpointBuilder("/me/following/contains").with("type", "artist")
89-
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()).toObject(api, mutableListOf<Boolean>().javaClass).toList()
96+
get(
97+
EndpointBuilder("/me/following/contains").with("type", "artist")
98+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()
99+
).toArray<Boolean>(api)
90100
})
91101
}
92102

@@ -96,14 +106,22 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
96106
* @return [CursorBasedPagingObject] ([Information about them](https://github.com/adamint/spotify-web-api-kotlin/blob/master/README.md#the-benefits-of-linkedresults-pagingobjects-and-cursor-based-paging-objects)
97107
* with full [Artist] objects
98108
*/
99-
fun getFollowedArtists(limit: Int? = null, after: String? = null): SpotifyRestPagingAction<Artist, CursorBasedPagingObject<Artist>> {
100-
return toPagingObjectAction(Supplier {
101-
get(EndpointBuilder("/me/following").with("type", "artist").with("limit", limit).with("after", after).toString())
102-
.toCursorBasedPagingObject("artists", this, Artist::class.java)
109+
fun getFollowedArtists(
110+
limit: Int? = null,
111+
after: String? = null
112+
): SpotifyRestActionPaging<Artist,CursorBasedPagingObject<Artist>> {
113+
return toActionPaging(Supplier {
114+
get(
115+
EndpointBuilder("/me/following").with("type", "artist").with("limit", limit).with(
116+
"after",
117+
after
118+
).toString()
119+
).toCursorBasedPagingObject<Artist>("artists", this)
103120
})
104121
}
105122

106-
fun getFollowedUsers(): SpotifyRestAction<List<SpotifyPublicUser>> = throw NotImplementedError("Though Spotify will implement this in the future, it is not currently supported.")
123+
fun getFollowedUsers(): SpotifyRestAction<List<SpotifyPublicUser>> =
124+
throw NotImplementedError("Though Spotify will implement this in the future, it is not currently supported.")
107125

108126
/**
109127
* Add the current user as a follower of another user
@@ -123,8 +141,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
123141
*/
124142
fun followUsers(vararg users: String): SpotifyRestAction<Unit> {
125143
return toAction(Supplier {
126-
put(EndpointBuilder("/me/following").with("type", "user")
127-
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString())
144+
put(
145+
EndpointBuilder("/me/following").with("type", "user")
146+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()
147+
)
128148
Unit
129149
})
130150
}
@@ -147,8 +167,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
147167
*/
148168
fun followArtists(vararg artists: String): SpotifyRestAction<Unit> {
149169
return toAction(Supplier {
150-
put(EndpointBuilder("/me/following").with("type", "artist")
151-
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString())
170+
put(
171+
EndpointBuilder("/me/following").with("type", "artist")
172+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()
173+
)
152174
Unit
153175
})
154176
}
@@ -165,7 +187,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
165187
*/
166188
fun followPlaylist(playlist: String, followPublicly: Boolean = true): SpotifyRestAction<Unit> {
167189
return toAction(Supplier {
168-
put(EndpointBuilder("/playlists/${PlaylistURI(playlist).id}/followers").toString(), "{\"public\": $followPublicly}")
190+
put(
191+
EndpointBuilder("/playlists/${PlaylistURI(playlist).id}/followers").toString(),
192+
"{\"public\": $followPublicly}"
193+
)
169194
Unit
170195
})
171196
}
@@ -192,8 +217,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
192217
*/
193218
fun unfollowUsers(vararg users: String): SpotifyRestAction<Unit> {
194219
return toAction(Supplier {
195-
delete(EndpointBuilder("/me/following").with("type", "user")
196-
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString())
220+
delete(
221+
EndpointBuilder("/me/following").with("type", "user")
222+
.with("ids", users.joinToString(",") { UserURI(it).id.encode() }).toString()
223+
)
197224
Unit
198225
})
199226
}
@@ -220,8 +247,10 @@ class ClientFollowingAPI(api: SpotifyAPI) : FollowingAPI(api) {
220247
*/
221248
fun unfollowArtists(vararg artists: String): SpotifyRestAction<Unit> {
222249
return toAction(Supplier {
223-
delete(EndpointBuilder("/me/following").with("type", "artist")
224-
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString())
250+
delete(
251+
EndpointBuilder("/me/following").with("type", "artist")
252+
.with("ids", artists.joinToString(",") { ArtistURI(it).id.encode() }).toString()
253+
)
225254
Unit
226255
})
227256
}

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientLibraryAPI.kt

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

44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
6+
import com.adamratzman.spotify.main.SpotifyRestActionPaging
77
import com.adamratzman.spotify.utils.AlbumURI
88
import com.adamratzman.spotify.utils.EndpointBuilder
99
import com.adamratzman.spotify.utils.Market
@@ -13,7 +13,7 @@ import com.adamratzman.spotify.utils.SavedTrack
1313
import com.adamratzman.spotify.utils.SpotifyEndpoint
1414
import com.adamratzman.spotify.utils.TrackURI
1515
import com.adamratzman.spotify.utils.encode
16-
import com.adamratzman.spotify.utils.toObject
16+
import com.adamratzman.spotify.utils.toArray
1717
import com.adamratzman.spotify.utils.toPagingObject
1818
import java.util.function.Supplier
1919

@@ -30,12 +30,12 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3030
limit: Int? = null,
3131
offset: Int? = null,
3232
market: Market? = null
33-
): SpotifyRestPagingAction<SavedTrack, PagingObject<SavedTrack>> {
34-
return toPagingObjectAction(Supplier {
33+
): SpotifyRestActionPaging<SavedTrack,PagingObject<SavedTrack>> {
34+
return toActionPaging(Supplier {
3535
get(
3636
EndpointBuilder("/me/tracks").with("limit", limit).with("offset", offset).with("market", market?.code)
3737
.toString()
38-
).toPagingObject(endpoint = this, tClazz = SavedTrack::class.java)
38+
).toPagingObject<SavedTrack>(endpoint = this)
3939
})
4040
}
4141

@@ -48,12 +48,12 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4848
limit: Int? = null,
4949
offset: Int? = null,
5050
market: Market? = null
51-
): SpotifyRestPagingAction<SavedAlbum, PagingObject<SavedAlbum>> {
52-
return toPagingObjectAction(Supplier {
51+
): SpotifyRestActionPaging<SavedAlbum,PagingObject<SavedAlbum>> {
52+
return toActionPaging(Supplier {
5353
get(
5454
EndpointBuilder("/me/albums").with("limit", limit).with("offset", offset).with("market", market?.code)
5555
.toString()
56-
).toPagingObject(endpoint = this, tClazz = SavedAlbum::class.java)
56+
).toPagingObject<SavedAlbum>(endpoint = this)
5757
})
5858
}
5959

@@ -78,7 +78,7 @@ class ClientLibraryAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
7878
get(
7979
EndpointBuilder("/me/$type/contains").with("ids", ids.joinToString(",") { type.id(it).encode() })
8080
.toString()
81-
).toObject(api, mutableListOf<Boolean>().javaClass).toList()
81+
).toArray<Boolean>(api)
8282
})
8383
}
8484

src/main/kotlin/com/adamratzman/spotify/endpoints/client/ClientPersonalizationAPI.kt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package com.adamratzman.spotify.endpoints.client
33

44
import com.adamratzman.spotify.main.SpotifyAPI
5-
import com.adamratzman.spotify.main.SpotifyRestPagingAction
5+
import com.adamratzman.spotify.main.SpotifyRestActionPaging
66
import com.adamratzman.spotify.utils.Artist
77
import com.adamratzman.spotify.utils.EndpointBuilder
88
import com.adamratzman.spotify.utils.PagingObject
@@ -33,10 +33,17 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3333
*
3434
* @return [PagingObject] of full [Artist] objects sorted by affinity
3535
*/
36-
fun getTopArtists(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Artist, PagingObject<Artist>> {
37-
return toPagingObjectAction(Supplier {
38-
get(EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
39-
.with("time_range", timeRange).toString()).toPagingObject(endpoint = this, tClazz = Artist::class.java)
36+
fun getTopArtists(
37+
limit: Int? = null,
38+
offset: Int? = null,
39+
timeRange: TimeRange? = null
40+
): SpotifyRestActionPaging<Artist, PagingObject<Artist>> {
41+
return toActionPaging(Supplier {
42+
get(
43+
EndpointBuilder("/me/top/artists").with("limit", limit).with("offset", offset)
44+
.with("time_range", timeRange).toString()
45+
)
46+
.toPagingObject<Artist>(endpoint = this)
4047
})
4148
}
4249

@@ -52,10 +59,17 @@ class ClientPersonalizationAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5259
*
5360
* @return [PagingObject] of full [Track] objects sorted by affinity
5461
*/
55-
fun getTopTracks(limit: Int? = null, offset: Int? = null, timeRange: TimeRange? = null): SpotifyRestPagingAction<Track, PagingObject<Track>> {
56-
return toPagingObjectAction(Supplier {
57-
get(EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
58-
.with("time_range", timeRange).toString()).toPagingObject(endpoint = this, tClazz = Track::class.java)
62+
fun getTopTracks(
63+
limit: Int? = null,
64+
offset: Int? = null,
65+
timeRange: TimeRange? = null
66+
): SpotifyRestActionPaging<Track,PagingObject<Track>> {
67+
return toActionPaging(Supplier {
68+
get(
69+
EndpointBuilder("/me/top/tracks").with("limit", limit).with("offset", offset)
70+
.with("time_range", timeRange).toString()
71+
)
72+
.toPagingObject<Track>(endpoint = this)
5973
})
6074
}
6175
}

0 commit comments

Comments
 (0)