@@ -17,11 +17,11 @@ import com.adamratzman.spotify.models.PlaylistURI
17
17
import com.adamratzman.spotify.models.SimplePlaylist
18
18
import com.adamratzman.spotify.models.TrackURI
19
19
import com.adamratzman.spotify.models.UserURI
20
+ import com.adamratzman.spotify.models.serialization.toJson
20
21
import com.adamratzman.spotify.models.serialization.toObject
21
22
import com.adamratzman.spotify.models.serialization.toPagingObject
22
- import com.beust.klaxon.Json
23
- import com.beust.klaxon.JsonArray
24
- import com.beust.klaxon.JsonObject
23
+ import com.adamratzman.spotify.utils.jsonMap
24
+ import com.squareup.moshi.Json
25
25
import java.awt.image.BufferedImage
26
26
import java.io.ByteArrayOutputStream
27
27
import java.io.File
@@ -54,22 +54,22 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
54
54
* @return The created [Playlist] object with no tracks
55
55
*/
56
56
fun createPlaylist (
57
- name : String ,
58
- description : String? = null,
59
- public : Boolean? = null,
60
- collaborative : Boolean? = null,
61
- user : String = (api as SpotifyClientAPI ).userId
57
+ name : String ,
58
+ description : String? = null,
59
+ public : Boolean? = null,
60
+ collaborative : Boolean? = null,
61
+ user : String = (api as SpotifyClientAPI ).userId
62
62
): SpotifyRestAction <Playlist > {
63
63
if (name.isEmpty()) throw BadRequestException (ErrorObject (400 , " Name cannot be empty" ))
64
64
return toAction(Supplier {
65
- val json = JsonObject ()
65
+ val json = jsonMap ()
66
66
json[" name" ] = name
67
67
if (description != null ) json[" description" ] = description
68
68
if (public != null ) json[" public" ] = public
69
69
if (collaborative != null ) json[" collaborative" ] = collaborative
70
70
post(
71
71
EndpointBuilder (" /users/${UserURI (user).id.encode()} /playlists" ).toString(),
72
- json.toJsonString ()
72
+ json.toJson ()
73
73
).toObject<Playlist >(api)
74
74
})
75
75
}
@@ -107,12 +107,12 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
107
107
* @throws BadRequestException if any invalid track ids is provided or the playlist is not found
108
108
*/
109
109
fun addTracksToPlaylist (playlist : String , vararg tracks : String , position : Int? = null): SpotifyRestAction <Unit > {
110
- val json = JsonObject (). apply { this [ " uris" ] = tracks.map { TrackURI (TrackURI (it).id.encode()).uri } }
110
+ val json = mutableMapOf< String , Any >( " uris" to tracks.map { TrackURI (TrackURI (it).id.encode()).uri })
111
111
if (position != null ) json[" position" ] = position
112
112
return toAction(Supplier {
113
113
post(
114
114
EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
115
- json.toJsonString ()
115
+ json.toJson ()
116
116
)
117
117
Unit
118
118
})
@@ -133,20 +133,20 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
133
133
* @throws BadRequestException if the playlist is not found or parameters exceed the max length
134
134
*/
135
135
fun changePlaylistDetails (
136
- playlist : String ,
137
- name : String? = null,
138
- public : Boolean? = null,
139
- collaborative : Boolean? = null,
140
- description : String? = null
136
+ playlist : String ,
137
+ name : String? = null,
138
+ public : Boolean? = null,
139
+ collaborative : Boolean? = null,
140
+ description : String? = null
141
141
): SpotifyRestAction <Unit > {
142
- val json = JsonObject ()
142
+ val json = jsonMap ()
143
143
if (name != null ) json[" name" ] = name
144
144
if (public != null ) json[" public" ] = public
145
145
if (collaborative != null ) json[" collaborative" ] = collaborative
146
146
if (description != null ) json[" description" ] = description
147
147
if (json.isEmpty()) throw IllegalArgumentException (" At least one option must not be null" )
148
148
return toAction(Supplier {
149
- put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} " ).toString(), json.toJsonString ())
149
+ put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} " ).toString(), json.toJson ())
150
150
Unit
151
151
})
152
152
}
@@ -166,8 +166,8 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
166
166
* @throws BadRequestException if the filters provided are illegal
167
167
*/
168
168
fun getPlaylists (
169
- limit : Int? = null,
170
- offset : Int? = null
169
+ limit : Int? = null,
170
+ offset : Int? = null
171
171
): SpotifyRestActionPaging <SimplePlaylist , PagingObject <SimplePlaylist >> {
172
172
if (limit != null && limit !in 1 .. 50 ) throw IllegalArgumentException (" Limit must be between 1 and 50. Provided $limit " )
173
173
if (offset != null && offset !in 0 .. 100000 ) throw IllegalArgumentException (" Offset must be between 0 and 100,000. Provided $limit " )
@@ -229,21 +229,21 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
229
229
* @throws BadRequestException if the playlist is not found or illegal filters are applied
230
230
*/
231
231
fun reorderPlaylistTracks (
232
- playlist : String ,
233
- reorderRangeStart : Int ,
234
- reorderRangeLength : Int? = null,
235
- insertionPoint : Int ,
236
- snapshotId : String? = null
232
+ playlist : String ,
233
+ reorderRangeStart : Int ,
234
+ reorderRangeLength : Int? = null,
235
+ insertionPoint : Int ,
236
+ snapshotId : String? = null
237
237
): SpotifyRestAction <Snapshot > {
238
238
return toAction(Supplier {
239
- val json = JsonObject ()
239
+ val json = jsonMap ()
240
240
json[" range_start" ] = reorderRangeStart
241
241
json[" insert_before" ] = insertionPoint
242
242
if (reorderRangeLength != null ) json[" range_length" ] = reorderRangeLength
243
243
if (snapshotId != null ) json[" snapshot_id" ] = snapshotId
244
244
put(
245
245
EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
246
- json.toJsonString ()
246
+ json.toJson ()
247
247
).toObject<Snapshot >(api)
248
248
})
249
249
}
@@ -262,11 +262,11 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
262
262
*/
263
263
fun setPlaylistTracks (playlist : String , vararg tracks : String ): SpotifyRestAction <Unit > {
264
264
return toAction(Supplier {
265
- val json = JsonObject ()
265
+ val json = jsonMap ()
266
266
json[" uris" ] = tracks.map { TrackURI (TrackURI (it).id.encode()).uri }
267
267
put(
268
268
EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(),
269
- json.toJsonString ()
269
+ json.toJson ()
270
270
)
271
271
Unit
272
272
})
@@ -316,12 +316,12 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
316
316
* @throws BadRequestException if invalid data is provided
317
317
*/
318
318
fun uploadPlaylistCover (
319
- playlist : String ,
320
- imagePath : String? = null,
321
- imageFile : File ? = null,
322
- image : BufferedImage ? = null,
323
- imageData : String? = null,
324
- imageUrl : String? = null
319
+ playlist : String ,
320
+ imagePath : String? = null,
321
+ imageFile : File ? = null,
322
+ image : BufferedImage ? = null,
323
+ imageData : String? = null,
324
+ imageUrl : String? = null
325
325
): SpotifyRestAction <Unit > {
326
326
return toAction(Supplier {
327
327
val data = imageData ? : when {
@@ -351,10 +351,10 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
351
351
* @param snapshotId The playlist snapshot against which to apply this action. **recommended to have**
352
352
*/
353
353
fun removeTrackFromPlaylist (
354
- playlist : String ,
355
- track : String ,
356
- positions : SpotifyTrackPositions ,
357
- snapshotId : String? = null
354
+ playlist : String ,
355
+ track : String ,
356
+ positions : SpotifyTrackPositions ,
357
+ snapshotId : String? = null
358
358
) = removeTracksFromPlaylist(playlist, track to positions, snapshotId = snapshotId)
359
359
360
360
/* *
@@ -368,9 +368,9 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
368
368
* @param snapshotId The playlist snapshot against which to apply this action. **recommended to have**
369
369
*/
370
370
fun removeTrackFromPlaylist (
371
- playlist : String ,
372
- track : String ,
373
- snapshotId : String? = null
371
+ playlist : String ,
372
+ track : String ,
373
+ snapshotId : String? = null
374
374
) = removeTracksFromPlaylist(playlist, track, snapshotId = snapshotId)
375
375
376
376
/* *
@@ -384,9 +384,9 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
384
384
* @param snapshotId The playlist snapshot against which to apply this action. **recommended to have**
385
385
*/
386
386
fun removeTracksFromPlaylist (
387
- playlist : String ,
388
- vararg tracks : String ,
389
- snapshotId : String? = null
387
+ playlist : String ,
388
+ vararg tracks : String ,
389
+ snapshotId : String? = null
390
390
) = removePlaylistTracksImpl(playlist, tracks.map { it to null }.toTypedArray(), snapshotId)
391
391
392
392
/* *
@@ -400,29 +400,29 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistAPI(api) {
400
400
* @param snapshotId The playlist snapshot against which to apply this action. **recommended to have**
401
401
*/
402
402
fun removeTracksFromPlaylist (
403
- playlist : String ,
404
- vararg tracks : Pair <String , SpotifyTrackPositions >,
405
- snapshotId : String? = null
403
+ playlist : String ,
404
+ vararg tracks : Pair <String , SpotifyTrackPositions >,
405
+ snapshotId : String? = null
406
406
) = removePlaylistTracksImpl(playlist, tracks.toList().toTypedArray(), snapshotId)
407
407
408
408
private fun removePlaylistTracksImpl (
409
- playlist : String ,
410
- tracks : Array <Pair <String , SpotifyTrackPositions ?>>,
411
- snapshotId : String?
409
+ playlist : String ,
410
+ tracks : Array <Pair <String , SpotifyTrackPositions ?>>,
411
+ snapshotId : String?
412
412
): SpotifyRestAction <Snapshot > {
413
413
return toAction(Supplier {
414
414
if (tracks.isEmpty()) throw IllegalArgumentException (" You need to provide at least one track to remove" )
415
415
416
- val json = JsonObject ().apply { if (snapshotId != null ) this [" snapshot_id" ] = snapshotId }
416
+ val json = jsonMap ().apply { if (snapshotId != null ) this [" snapshot_id" ] = snapshotId }
417
417
418
418
tracks.map { (track, positions) ->
419
- JsonObject ().apply {
419
+ jsonMap ().apply {
420
420
this [" uri" ] = TrackURI (track).uri
421
421
if (positions?.positions?.isNotEmpty() == true ) this [" positions" ] = positions.positions
422
422
}.also { if (positions?.positions?.isNotEmpty() == true ) it[" positions" ] = positions }
423
- }.let { json.put(" tracks" , JsonArray (it) ) }
423
+ }.let { json.put(" tracks" , it ) }
424
424
delete(
425
- EndpointBuilder (" /playlists/${PlaylistURI (playlist).id} /tracks" ).toString(), body = json.toJsonString ()
425
+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id} /tracks" ).toString(), body = json.toJson ()
426
426
).toObject<Snapshot >(api)
427
427
})
428
428
}
0 commit comments