@@ -9,21 +9,45 @@ import java.util.function.Supplier
99 * Endpoints for retrieving information about a user’s playlists and for managing a user’s playlists.
1010 */
1111class UserPlaylistAPI (api : SpotifyAPI ) : SpotifyEndpoint(api) {
12- fun createPlaylist (userId : String , name : String , description : String , public : Boolean = true): SpotifyRestAction <Playlist > {
12+ /* *
13+ * Create a playlist for a Spotify user. (The playlist will be empty until you add tracks.)
14+ *
15+ * @param userId The user’s Spotify user ID.
16+ * @param name The name for the new playlist, for example "Your Coolest Playlist" . This name does not need to be
17+ * unique; a user may have several playlists with the same name.
18+ * @param description
19+ * @param public Defaults to true . If true the playlist will be public, if false it will be private.
20+ * To be able to create private playlists, the user must have granted the playlist-modify-private scope.
21+ * @param collaborative Defaults to false . If true the playlist will be collaborative. Note that to create a
22+ * collaborative playlist you must also set public to false . To create collaborative playlists you must have
23+ * granted playlist-modify-private and playlist-modify-public scopes.
24+ *
25+ * @return The created [Playlist] object with no tracks
26+ */
27+ fun createPlaylist (userId : String , name : String , description : String? = null, public : Boolean? = null, collaborative : Boolean? = null): SpotifyRestAction <Playlist > {
1328 return toAction(Supplier {
14- post( " https://api.spotify.com/v1/users/ ${userId.encode()} /playlists " ,
15- " { " +
16- " \" name \" : \" $name \" , " +
17- " \" description \" : \" $description \" , " +
18- " \" public \" : $public " +
19- " } " ).toObject<Playlist >(api)
29+ val json = JSONObject ()
30+ json.put( " name " , name)
31+ if (description != null ) json.put( " description " , description)
32+ if ( public != null ) json.put( " public " , public)
33+ if (collaborative != null ) json.put( " collaborative " , collaborative)
34+ post( " https://api.spotify.com/v1/users/ ${userId.encode()} /playlists " , json.toString() ).toObject<Playlist >(api)
2035 })
2136 }
2237
2338 /* *
24- * [uris] <b>MUST BE</b> Spotify URIs, unlike nearly every other endpoint.
39+ * Add one or more tracks to a user’s playlist.
40+ *
41+ * @param userId The user’s Spotify user ID.
42+ * @param playlistId The Spotify ID for the playlist.
43+ * @param uris Spotify track ids. A maximum of 100 tracks can be added in one request.
44+ * @param position The position to insert the tracks, a zero-based index. For example, to insert the tracks in the
45+ * first position: position=0; to insert the tracks in the third position: position=2 . If omitted, the tracks will
46+ * be appended to the playlist. Tracks are added in the order they are listed in the query string or request body.
47+ *
48+ * @throws BadRequestException if any invalid track ids is provided or the playlist is not found
2549 */
26- fun addTrackToPlaylist (userId : String , playlistId : String , vararg uris : String , position : Int? = null): SpotifyRestAction <Unit > {
50+ fun addTracksToPlaylist (userId : String , playlistId : String , vararg uris : String , position : Int? = null): SpotifyRestAction <Unit > {
2751 val json = JSONObject ().put(" uris" , uris.map { " spotify:track:${it.encode()} " })
2852 if (position != null ) json.put(" position" , position)
2953 return toAction(Supplier {
@@ -32,6 +56,18 @@ class UserPlaylistAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
3256 })
3357 }
3458
59+ /* *
60+ * Change a playlist’s name and public/private state. (The user must, of course, own the playlist.)
61+ *
62+ * @param userId The user’s Spotify user ID.
63+ * @param playlistId The Spotify ID for the playlist.
64+ * @param name Optional. The name to change the playlist to.
65+ * @param public Optional. Whether to make the playlist public or not.
66+ * @param collaborative Optional. Whether to make the playlist collaborative or not.
67+ * @param description Optional. Whether to change the description or not.
68+ *
69+ * @throws BadRequestException if the playlist is not found or parameters exceed the max length
70+ */
3571 fun changePlaylistDescription (userId : String , playlistId : String , name : String? = null, public : Boolean? = null, collaborative : Boolean? = null,
3672 description : String? = null): SpotifyRestAction <Unit > {
3773 val json = JSONObject ()
@@ -46,6 +82,14 @@ class UserPlaylistAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4682 })
4783 }
4884
85+ /* *
86+ * Get a list of the playlists owned or followed by a Spotify user.
87+ *
88+ * @param limit The maximum number of tracks to return. Default: 20. Minimum: 1. Maximum: 50.
89+ * @param offset The index of the first track to return. Default: 0 (the first object). Use with limit to get the next set of tracks.
90+ *
91+ * @throws BadRequestException if the filters provided are illegal
92+ */
4993 fun getClientPlaylists (limit : Int = 20, offset : Int = 0): SpotifyRestAction <PagingObject <SimplePlaylist >> {
5094 if (limit !in 1 .. 50 ) throw IllegalArgumentException (" Limit must be between 1 and 50. Provided $limit " )
5195 if (offset !in 0 .. 100000 ) throw IllegalArgumentException (" Offset must be between 0 and 100,000. Provided $limit " )
@@ -54,20 +98,50 @@ class UserPlaylistAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
5498 })
5599 }
56100
57- fun reorderTracks (userId : String , playlistId : String , reorderRangeStart : Int , reorderRangeLength : Int = 1, insertionPoint : Int , snapshotId : String? = null): SpotifyRestAction <Snapshot > {
101+ /* *
102+ * Reorder a track or a group of tracks in a playlist.
103+ *
104+ * When reordering tracks, the timestamp indicating when they were added and the user who added them will be kept
105+ * untouched. In addition, the users following the playlists won’t be notified about changes in the playlists
106+ * when the tracks are reordered.
107+ *
108+ * @param userId The user’s Spotify user ID.
109+ * @param playlistId The Spotify ID for the playlist.
110+ * @param reorderRangeStart The position of the first track to be reordered.
111+ * @param reorderRangeLength
112+ * @param insertionPoint
113+ * @param snapshotId
114+ *
115+ * @throws BadRequestException if the playlist is not found or illegal filters are applied
116+ */
117+ fun reorderTracks (userId : String , playlistId : String , reorderRangeStart : Int , reorderRangeLength : Int? = null, insertionPoint : Int , snapshotId : String? = null): SpotifyRestAction <Snapshot > {
58118 return toAction(Supplier {
59- put(" https://api.spotify.com/v1/users/${userId.encode()} /playlists/${playlistId.encode()} /tracks" , " {" +
60- " \" range_start\" : $reorderRangeStart ," +
61- " \" range_length\" : $reorderRangeLength ," +
62- " \" insert_before\" : $insertionPoint " +
63- if (snapshotId != null ) " ,\" snapshot_id\" : \" $snapshotId \" " else " " +
64- " }" ).toObject<Snapshot >(api)
119+ val json = JSONObject ()
120+ json.put(" range_start" , reorderRangeStart)
121+ json.put(" insert_before" , insertionPoint)
122+ if (reorderRangeLength != null ) json.put(" range_length" , reorderRangeLength)
123+ if (snapshotId != null ) json.put(" snapshot_id" , snapshotId)
124+ put(" https://api.spotify.com/v1/users/${userId.encode()} /playlists/${playlistId.encode()} /tracks" , json.toString())
125+ .toObject<Snapshot >(api)
65126 })
66127 }
67128
129+ /* *
130+ * Replace all the tracks in a playlist, overwriting its existing tracks. This powerful request can be useful
131+ * for replacing tracks, re-ordering existing tracks, or clearing the playlist.
132+ *
133+ * @param userId The user’s Spotify user ID.
134+ * @param playlistId The Spotify ID for the playlist.
135+ * @param trackIds The Spotify track ids.
136+ *
137+ * @throws BadRequestException if playlist is not found or illegal tracks are provided
138+ */
68139 fun replaceTracks (userId : String , playlistId : String , vararg trackIds : String ): SpotifyRestAction <Unit > {
69140 return toAction(Supplier {
70- put(" https://api.spotify.com/v1/users/${userId.encode()} /playlists/${playlistId.encode()} /tracks?uris=${trackIds.map { it.encode() }.joinToString(" ," ) { " spotify:track:$it " }} " )
141+ val json = JSONObject ()
142+ json.put(" uris" , trackIds.map { " spotify:track:${it.encode()} " })
143+ put(" https://api.spotify.com/v1/users/${userId.encode()} /playlists/${playlistId.encode()} /tracks" ,
144+ json.toString())
71145 Unit
72146 })
73147 }
0 commit comments