Skip to content

Commit 0e7e461

Browse files
Jiankai ZhengJiankai Zheng
authored andcommitted
feat(api): api call to remove items from playlist
1 parent 2ab3bc4 commit 0e7e461

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/main/java/spotify/api/impl/PlaylistApiRetrofit.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
import spotify.models.playlists.PlaylistSimplified;
1717
import spotify.models.playlists.PlaylistTrack;
1818
import spotify.models.playlists.Snapshot;
19-
import spotify.models.playlists.requests.AddItemPlaylistRequestBody;
20-
import spotify.models.playlists.requests.CreateUpdatePlaylistRequestBody;
21-
import spotify.models.playlists.requests.ReorderPlaylistItemsRequestBody;
22-
import spotify.models.playlists.requests.ReplacePlaylistItemsRequestBody;
19+
import spotify.models.playlists.requests.*;
2320
import spotify.retrofit.services.PlaylistService;
2421
import spotify.utils.LoggingUtil;
2522
import spotify.utils.ResponseChecker;
@@ -321,6 +318,39 @@ public void uploadCoverImageToPlaylist(String playlistId, String base64EncodedJp
321318
}
322319
}
323320

321+
@Override
322+
public Snapshot deleteItemsFromPlaylist(String playlistId, String snapshotId, DeleteItemsPlaylistRequestBody items) {
323+
if (playlistId == null || playlistId.isEmpty()) {
324+
final String errorMessage = "Playlist id is empty!";
325+
logger.error(errorMessage);
326+
throw new IllegalArgumentException(errorMessage);
327+
}
328+
329+
if (snapshotId != null && snapshotId.isEmpty()) {
330+
logger.warn("An empty snapshot id was passed in. The snapshot id has now been set to NULL.");
331+
snapshotId = null;
332+
}
333+
334+
logger.trace("Constructing HTTP call to remove items from a playlist.");
335+
Call<Snapshot> httpCall = playlistService.deleteItemsFromPlaylist("Bearer " + this.accessToken, playlistId, items);
336+
337+
try {
338+
logger.info("Executing HTTP call to remove items from a playlist.");
339+
logger.debug(String.format("Removing items from playlist %s with snapshot id %s", playlistId, snapshotId));
340+
logger.debug(String.format("Removing the following items %s", items));
341+
LoggingUtil.logHttpCall(logger, httpCall);
342+
Response<Snapshot> response = httpCall.execute();
343+
344+
ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response, HttpStatusCode.OK);
345+
346+
logger.info("Items have been successfully removed from the playlist");
347+
return response.body();
348+
} catch (IOException ex) {
349+
logger.error("HTTP request to remove items from playlist has failed");
350+
throw new HttpRequestFailedException(ex.getMessage());
351+
}
352+
}
353+
324354
private void validateParametersReorderFunction(String playlistId, ReorderPlaylistItemsRequestBody requestBody) {
325355
if (playlistId == null || playlistId.isEmpty()) {
326356
final String errorMessage = "Playlist id can not be empty!";

src/main/java/spotify/api/interfaces/PlaylistApi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import spotify.models.playlists.PlaylistTrack;
88
import spotify.models.playlists.Snapshot;
99
import spotify.models.playlists.requests.CreateUpdatePlaylistRequestBody;
10+
import spotify.models.playlists.requests.DeleteItemsPlaylistRequestBody;
1011
import spotify.models.playlists.requests.ReorderPlaylistItemsRequestBody;
1112

1213
import java.util.List;
@@ -34,4 +35,6 @@ public interface PlaylistApi {
3435
void replacePlaylistItems(String playlistId, List<String> listOfItemUris);
3536

3637
void uploadCoverImageToPlaylist(String playlistId, String base64EncodedJpegImage);
38+
39+
Snapshot deleteItemsFromPlaylist(String playlistId, String snapshotId, DeleteItemsPlaylistRequestBody items);
3740
}

src/main/java/spotify/api/spotify/SpotifyApi.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import spotify.models.paging.Paging;
2727
import spotify.models.playlists.*;
2828
import spotify.models.playlists.requests.CreateUpdatePlaylistRequestBody;
29+
import spotify.models.playlists.requests.DeleteItemsPlaylistRequestBody;
2930
import spotify.models.playlists.requests.ReorderPlaylistItemsRequestBody;
3031
import spotify.models.recommendations.RecommendationCollection;
3132
import spotify.models.shows.SavedShowSimplified;
@@ -345,6 +346,11 @@ public void uploadCoverImageToPlaylist(String playlistId, String base64EncodedJp
345346
playlistApi.uploadCoverImageToPlaylist(playlistId, base64EncodedJpegImage);
346347
}
347348

349+
public Snapshot deleteItemsFromPlaylist(String playlistId, String snapshotId, DeleteItemsPlaylistRequestBody items) {
350+
logger.info("Requesting to remove items from a playlist");
351+
return playlistApi.deleteItemsFromPlaylist(playlistId, snapshotId, items);
352+
}
353+
348354
private void setup(final String accessToken) {
349355
logger.trace("Constructing Retrofit APIs");
350356
this.trackApi = new TrackApiRetrofit(accessToken);

0 commit comments

Comments
 (0)