@@ -69,6 +69,22 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
6969 })
7070 }
7171
72+
73+ /* *
74+ * Add a track to a user’s playlist.
75+ *
76+ * @param playlist The Spotify ID for the playlist.
77+ * @param tracks Spotify track id
78+ * @param position The position to insert the tracks, a zero-based index. For example, to insert the tracks in the
79+ * first position: position=0; to insert the tracks in the third position: position=2 . If omitted, the tracks will
80+ * be appended to the playlist. Tracks are added in the order they are listed in the query string or request body.
81+ *
82+ * @throws BadRequestException if any invalid track ids is provided or the playlist is not found
83+ */
84+
85+ fun addTrackToPlaylist (playlist : String ,track : String ,position : Int? =null)
86+ = addTracksToPlaylist(playlist,track,position = position)
87+
7288 /* *
7389 * Add one or more tracks to a user’s playlist.
7490 *
@@ -185,7 +201,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
185201 *
186202 * @throws BadRequestException if the playlist is not found or illegal filters are applied
187203 */
188- fun reorderTracks (
204+ fun reorderPlaylistTracks (
189205 playlist : String ,
190206 reorderRangeStart : Int ,
191207 reorderRangeLength : Int? = null,
@@ -273,26 +289,55 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
273289 })
274290 }
275291
276- fun removePlaylistTrack (
292+ /* *
293+ * Remove a track in the specified positions (zero-based) from the specified playlist.
294+ *
295+ * @param playlist the playlist id
296+ * @param track the track id
297+ * @param positions the positions at which the track is located in the playlist
298+ * @param snapshotId the playlist snapshot against which to apply the track removals. **recommended to have**
299+ */
300+ fun removeTrackFromPlaylist (
277301 playlist : String ,
278302 track : String ,
279303 positions : SpotifyTrackPositions ,
280304 snapshotId : String? = null
281- ) = removePlaylistTracks (playlist, track to positions, snapshotId = snapshotId)
305+ ) = removeTracksFromPlaylist (playlist, track to positions, snapshotId = snapshotId)
282306
283- fun removePlaylistTrack (
307+ /* *
308+ * Remove all occurrences of a track from the specified playlist.
309+ *
310+ * @param playlist the playlist id
311+ * @param track the track id
312+ * @param snapshotId the playlist snapshot against which to apply the track removals. **recommended to have**
313+ */
314+ fun removeTrackFromPlaylist (
284315 playlist : String ,
285316 track : String ,
286317 snapshotId : String? = null
287- ) = removePlaylistTracks (playlist, track, snapshotId = snapshotId)
318+ ) = removeTracksFromPlaylist (playlist, track, snapshotId = snapshotId)
288319
289- fun removePlaylistTracks (
320+ /* *
321+ * Remove all occurrences of the specified tracks from the given playlist.
322+ *
323+ * @param playlist the playlist id
324+ * @param tracks an array of track ids
325+ * @param snapshotId the playlist snapshot against which to apply the track removals. **recommended to have**
326+ */
327+ fun removeTracksFromPlaylist (
290328 playlist : String ,
291329 vararg tracks : String ,
292330 snapshotId : String? = null
293331 ) = removePlaylistTracksImpl(playlist, tracks.map { it to null }.toTypedArray(), snapshotId)
294332
295- fun removePlaylistTracks (
333+ /* *
334+ * Remove tracks (each with their own positions) from the given playlist.
335+ *
336+ * @param playlist the playlist id
337+ * @param tracks an array of [Pair]s of track ids *and* track positions (zero-based)
338+ * @param snapshotId the playlist snapshot against which to apply the track removals. **recommended to have**
339+ */
340+ fun removeTracksFromPlaylist (
296341 playlist : String ,
297342 vararg tracks : Pair <String , SpotifyTrackPositions >,
298343 snapshotId : String? = null
@@ -302,7 +347,7 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
302347 playlist : String ,
303348 tracks : Array <Pair <String , SpotifyTrackPositions ?>>,
304349 snapshotId : String?
305- ): SpotifyRestAction <String > {
350+ ): SpotifyRestAction <Snapshot > {
306351 return toAction(Supplier {
307352 if (tracks.isEmpty()) throw IllegalArgumentException (" You need to provide at least one track to remove" )
308353
@@ -311,29 +356,15 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
311356 tracks.map { (track, positions) ->
312357 JsonObject ().apply {
313358 this [" uri" ] = TrackURI (track).uri
314- if (positions?.positions?.isNotEmpty() == true ) this .put( " positions" , positions.positions)
315- }.also { if (positions?.positions?.isNotEmpty() == true ) it[" positions" ] = positions.positions }
359+ if (positions?.positions?.isNotEmpty() == true ) this [ " positions" ] = positions.positions
360+ }.also { if (positions?.positions?.isNotEmpty() == true ) it[" positions" ] = positions }
316361 }.let { json.put(" tracks" , JsonArray (it)) }
317362 delete(
318363 EndpointBuilder (" /playlists/${PlaylistURI (playlist).id} /tracks" ).toString(), body = json.toJsonString()
319- )
364+ ).toObject< Snapshot >(api)
320365 })
321366 }
322367
323- /*
324- fun removeAllOccurances(user: String, playlist: String, vararg tracks: String): SpotifyRestAction<Unit> {
325- if (tracks.isEmpty()) throw IllegalArgumentException("Tracks to remove must not be empty")
326- return toAction(Supplier {
327- val json = JSONObject()
328- json.put("tracks", tracks.map { JSONObject().put("uri", TrackURI(it).uri) })
329- println(json.toString())
330- val userURI = UserURI(user)
331- delete("https://api.spotify.com/v1/users/${userURI.id}/playlists/${userURI.PlaylistURI(playlist).id}/tracks",
332- body = json.toString(), contentType = "application/json")
333- Unit
334- })
335- }
336- */
337368 private fun encode (image : BufferedImage ): String {
338369 val bos = ByteArrayOutputStream ()
339370 ImageIO .write(image, " jpg" , bos)
@@ -344,4 +375,4 @@ class ClientPlaylistAPI(api: SpotifyAPI) : PlaylistsAPI(api) {
344375 data class Snapshot (val snapshot_id : String )
345376}
346377
347- class SpotifyTrackPositions (vararg val positions : Int )
378+ class SpotifyTrackPositions (vararg val positions : Int ) : ArrayList<Int>(positions.toList())
0 commit comments