1+ /* Created by Adam Ratzman (2018) */
12package com.adamratzman.spotify.endpoints.client
23
34import com.adamratzman.spotify.endpoints.public.PlaylistsAPI
45import com.adamratzman.spotify.main.SpotifyAPI
56import com.adamratzman.spotify.main.SpotifyClientAPI
67import com.adamratzman.spotify.main.SpotifyRestAction
78import com.adamratzman.spotify.main.SpotifyRestPagingAction
8- import com.adamratzman.spotify.utils.*
9+ import com.adamratzman.spotify.utils.BadRequestException
10+ import com.adamratzman.spotify.utils.EndpointBuilder
11+ import com.adamratzman.spotify.utils.ErrorObject
12+ import com.adamratzman.spotify.utils.PagingObject
13+ import com.adamratzman.spotify.utils.Playlist
14+ import com.adamratzman.spotify.utils.PlaylistURI
15+ import com.adamratzman.spotify.utils.SimplePlaylist
16+ import com.adamratzman.spotify.utils.TrackURI
17+ import com.adamratzman.spotify.utils.UserURI
18+ import com.adamratzman.spotify.utils.encode
19+ import com.adamratzman.spotify.utils.toObject
20+ import com.adamratzman.spotify.utils.toPagingObject
921import org.json.JSONObject
1022import java.awt.image.BufferedImage
1123import java.io.ByteArrayOutputStream
@@ -16,7 +28,6 @@ import javax.imageio.IIOException
1628import javax.imageio.ImageIO
1729import javax.xml.bind.DatatypeConverter
1830
19-
2031/* *
2132 * Endpoints for retrieving information about a user’s playlists and for managing a user’s playlists.
2233 */
@@ -36,15 +47,24 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
3647 *
3748 * @return The created [Playlist] object with no tracks
3849 */
39- fun createPlaylist (name : String , description : String? = null, public : Boolean? = null, collaborative : Boolean? = null, user : String = (api as SpotifyClientAPI ).userId): SpotifyRestAction <Playlist > {
50+ fun createPlaylist (
51+ name : String ,
52+ description : String? = null,
53+ public : Boolean? = null,
54+ collaborative : Boolean? = null,
55+ user : String = (api as SpotifyClientAPI ).userId
56+ ): SpotifyRestAction <Playlist > {
4057 if (name.isEmpty()) throw BadRequestException (ErrorObject (400 , " Name cannot be empty" ))
4158 return toAction(Supplier {
4259 val json = JSONObject ()
4360 json.put(" name" , name)
4461 if (description != null ) json.put(" description" , description)
4562 if (public != null ) json.put(" public" , public)
4663 if (collaborative != null ) json.put(" collaborative" , collaborative)
47- post(EndpointBuilder (" /users/${UserURI (user).id.encode()} /playlists" ).toString(), json.toString()).toObject(api, Playlist ::class .java)
64+ post(EndpointBuilder (" /users/${UserURI (user).id.encode()} /playlists" ).toString(), json.toString()).toObject(
65+ api,
66+ Playlist ::class .java
67+ )
4868 })
4969 }
5070
@@ -81,8 +101,13 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
81101 *
82102 * @throws BadRequestException if the playlist is not found or parameters exceed the max length
83103 */
84- fun changePlaylistDescription (playlist : String , name : String? = null, public : Boolean? = null, collaborative : Boolean? = null,
85- description : String? = null): SpotifyRestAction <Unit > {
104+ fun changePlaylistDescription (
105+ playlist : String ,
106+ name : String? = null,
107+ public : Boolean? = null,
108+ collaborative : Boolean? = null,
109+ description : String? = null
110+ ): SpotifyRestAction <Unit > {
86111 val json = JSONObject ()
87112 if (name != null ) json.put(" name" , name)
88113 if (public != null ) json.put(" public" , public)
@@ -103,12 +128,15 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
103128 *
104129 * @throws BadRequestException if the filters provided are illegal
105130 */
106- fun getClientPlaylists (limit : Int? = null, offset : Int? = null): SpotifyRestPagingAction <SimplePlaylist , PagingObject <SimplePlaylist >> {
131+ fun getClientPlaylists (
132+ limit : Int? = null,
133+ offset : Int? = null
134+ ): SpotifyRestPagingAction <SimplePlaylist , PagingObject <SimplePlaylist >> {
107135 if (limit != null && limit !in 1 .. 50 ) throw IllegalArgumentException (" Limit must be between 1 and 50. Provided $limit " )
108136 if (offset != null && offset !in 0 .. 100000 ) throw IllegalArgumentException (" Offset must be between 0 and 100,000. Provided $limit " )
109137 return toPagingObjectAction(Supplier {
110138 get(EndpointBuilder (" /me/playlists" ).with (" limit" , limit).with (" offset" , offset).toString())
111- .toPagingObject(endpoint = this , tClazz = SimplePlaylist ::class .java)
139+ .toPagingObject(endpoint = this , tClazz = SimplePlaylist ::class .java)
112140 })
113141 }
114142
@@ -156,16 +184,21 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
156184 *
157185 * @throws BadRequestException if the playlist is not found or illegal filters are applied
158186 */
159- fun reorderTracks (playlist : String , reorderRangeStart : Int , reorderRangeLength : Int? = null, insertionPoint : Int ,
160- snapshotId : String? = null): SpotifyRestAction <Snapshot > {
187+ fun reorderTracks (
188+ playlist : String ,
189+ reorderRangeStart : Int ,
190+ reorderRangeLength : Int? = null,
191+ insertionPoint : Int ,
192+ snapshotId : String? = null
193+ ): SpotifyRestAction <Snapshot > {
161194 return toAction(Supplier {
162195 val json = JSONObject ()
163196 json.put(" range_start" , reorderRangeStart)
164197 json.put(" insert_before" , insertionPoint)
165198 if (reorderRangeLength != null ) json.put(" range_length" , reorderRangeLength)
166199 if (snapshotId != null ) json.put(" snapshot_id" , snapshotId)
167200 put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /tracks" ).toString(), json.toString())
168- .toObject(api, Snapshot ::class .java)
201+ .toObject(api, Snapshot ::class .java)
169202 })
170203 }
171204
@@ -213,9 +246,14 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
213246 * @throws IIOException if the image is not found
214247 * @throws BadRequestException if invalid data is provided
215248 */
216- fun uploadPlaylistCover (playlist : String , imagePath : String? = null,
217- imageFile : File ? = null, image : BufferedImage ? = null, imageData : String? = null,
218- imageUrl : String? = null): SpotifyRestAction <Unit > {
249+ fun uploadPlaylistCover (
250+ playlist : String ,
251+ imagePath : String? = null,
252+ imageFile : File ? = null,
253+ image : BufferedImage ? = null,
254+ imageData : String? = null,
255+ imageUrl : String? = null
256+ ): SpotifyRestAction <Unit > {
219257 return toAction(Supplier {
220258 val data = imageData ? : when {
221259 image != null -> encode(image)
@@ -224,8 +262,10 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
224262 imagePath != null -> encode(ImageIO .read(URL (" file:///$imagePath " )))
225263 else -> throw IllegalArgumentException (" No cover image was specified" )
226264 }
227- put(EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /images" ).toString(),
228- data, contentType = " image/jpeg" )
265+ put(
266+ EndpointBuilder (" /playlists/${PlaylistURI (playlist).id.encode()} /images" ).toString(),
267+ data, contentType = " image/jpeg"
268+ )
229269 Unit
230270 })
231271 }
0 commit comments