Skip to content

Commit cedab0c

Browse files
committed
feat: get current user's saved episodes.
1 parent 9689290 commit cedab0c

File tree

10 files changed

+128
-0
lines changed

10 files changed

+128
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,12 @@ This is the most recent coverage in the repository. The marked endpoints may not
215215
- - [x] Get Current User's Saved Albums
216216
- - [x] Get User's Saved Shows
217217
- - [x] Get Current User's Saved Tracks
218+
- - [x] Get Current User's Saved Episodes
218219
- - [x] Save Albums for Current User
219220
- - [x] Save Shows for Current User
220221
- - [x] Save Tracks for Current User
222+
- [x] Markets
223+
- - [x] Get Available Markets
221224
- [x] Personalization
222225
- - [x] Get User's Top Artists and Tracks
223226
- [x] Player

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import spotify.exceptions.HttpRequestFailedException;
1010
import spotify.factories.RetrofitHttpServiceFactory;
1111
import spotify.models.albums.SavedAlbumFull;
12+
import spotify.models.episodes.SavedEpisodeFull;
1213
import spotify.models.paging.Paging;
1314
import spotify.models.shows.SavedShowSimplified;
1415
import spotify.models.tracks.SavedTrackFull;
@@ -127,6 +128,29 @@ public Paging<SavedAlbumFull> getSavedAlbums(Map<String, String> options) {
127128
}
128129
}
129130

131+
@Override
132+
public Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options) {
133+
options = ValidatorUtil.optionsValueCheck(options);
134+
135+
logger.trace("Constructing HTTP call to fetch current user's saved episodes");
136+
Call<Paging<SavedEpisodeFull>> httpCall = libraryService.getSavedEpisodes("Bearer " + this.accessToken, options);
137+
138+
try {
139+
logger.info("Executing HTTP call to fetch current user's saved episodes");
140+
logger.debug("Fetching current user's saved episodes with the following values: {}.", options);
141+
LoggingUtil.logHttpCall(logger, httpCall);
142+
Response<Paging<SavedEpisodeFull>> response = httpCall.execute();
143+
144+
ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response, HttpStatusCode.OK);
145+
146+
logger.info("Saved episodes have been successfully fetched");
147+
return response.body();
148+
} catch (IOException ex) {
149+
logger.error("HTTP request to fetch saved episodes has failed.");
150+
throw new HttpRequestFailedException(ex.getMessage());
151+
}
152+
}
153+
130154
@Override
131155
public Paging<SavedShowSimplified> getSavedShows(Map<String, String> options) {
132156
options = ValidatorUtil.optionsValueCheck(options);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package spotify.api.interfaces;
22

33
import spotify.models.albums.SavedAlbumFull;
4+
import spotify.models.episodes.SavedEpisodeFull;
45
import spotify.models.paging.Paging;
56
import spotify.models.shows.SavedShowSimplified;
67
import spotify.models.tracks.SavedTrackFull;
@@ -32,4 +33,6 @@ public interface LibraryApi {
3233
void deleteShows(List<String> listOfShowIds, Map<String, String> options);
3334

3435
void deleteTracks(List<String> listOfTrackIds);
36+
37+
Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options);
3538
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import spotify.models.episodes.EpisodeFull;
2020
import spotify.models.episodes.EpisodeFullCollection;
2121
import spotify.models.episodes.EpisodeSimplified;
22+
import spotify.models.episodes.SavedEpisodeFull;
2223
import spotify.models.generic.Image;
2324
import spotify.models.markets.MarketFull;
2425
import spotify.models.paging.CursorBasedPaging;
@@ -264,6 +265,11 @@ public Paging<SavedAlbumFull> getSavedAlbums(Map<String, String> options) {
264265
return libraryApi.getSavedAlbums(options);
265266
}
266267

268+
public Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options) {
269+
logger.info("Requesting to fetch current user's saved episodes");
270+
return libraryApi.getSavedEpisodes(options);
271+
}
272+
267273
public Paging<SavedShowSimplified> getSavedShows(Map<String, String> options) {
268274
logger.info("Requesting to fetch current user's saved shows");
269275
return libraryApi.getSavedShows(options);

src/main/java/spotify/retrofit/services/LibraryService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import retrofit2.Call;
44
import retrofit2.http.*;
55
import spotify.models.albums.SavedAlbumFull;
6+
import spotify.models.episodes.SavedEpisodeFull;
67
import spotify.models.paging.Paging;
78
import spotify.models.shows.SavedShowSimplified;
89
import spotify.models.tracks.SavedTrackFull;
@@ -23,6 +24,9 @@ public interface LibraryService {
2324
@GET("me/albums")
2425
Call<Paging<SavedAlbumFull>> getSavedAlbums(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);
2526

27+
@GET("me/episodes")
28+
Call<Paging<SavedEpisodeFull>> getSavedEpisodes(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);
29+
2630
@GET("me/shows")
2731
Call<Paging<SavedShowSimplified>> getSavedShows(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);
2832

src/test/java/spotify/api/impl/LibraryApiRetrofitTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import spotify.exceptions.HttpRequestFailedException;
1414
import spotify.exceptions.SpotifyActionFailedException;
1515
import spotify.models.albums.SavedAlbumFull;
16+
import spotify.models.episodes.SavedEpisodeFull;
1617
import spotify.models.paging.Paging;
1718
import spotify.models.shows.SavedShowSimplified;
1819
import spotify.models.tracks.SavedTrackFull;
@@ -45,6 +46,8 @@ public class LibraryApiRetrofitTest extends AbstractApiRetrofitTest {
4546
@Mock
4647
private Call<Paging<SavedTrackFull>> mockedSavedTrackFullPagingCall;
4748
@Mock
49+
private Call<Paging<SavedEpisodeFull>> mockedSavedEpisodeFullPagingCall;
50+
@Mock
4851
private Call<Void> mockedVoidCall;
4952

5053
@BeforeEach
@@ -57,6 +60,7 @@ void setup() {
5760
when(mockedLibraryService.hasSavedShows(fakeAccessTokenWithBearer, fakeShowIds)).thenReturn(mockedListOfBooleanCall);
5861
when(mockedLibraryService.hasSavedTracks(fakeAccessTokenWithBearer, fakeTrackIds)).thenReturn(mockedListOfBooleanCall);
5962
when(mockedLibraryService.getSavedAlbums(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedAlbumFullPagingCall);
63+
when(mockedLibraryService.getSavedEpisodes(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedEpisodeFullPagingCall);
6064
when(mockedLibraryService.getSavedShows(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedAShowSimplifiedPagingCall);
6165
when(mockedLibraryService.getSavedTracks(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedTrackFullPagingCall);
6266
when(mockedLibraryService.saveAlbums(fakeAccessTokenWithBearer, fakeAlbumIds)).thenReturn(mockedVoidCall);
@@ -69,6 +73,7 @@ void setup() {
6973
when(mockedVoidCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
7074
when(mockedListOfBooleanCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
7175
when(mockedSavedAlbumFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
76+
when(mockedSavedEpisodeFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
7277
when(mockedSavedAShowSimplifiedPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
7378
when(mockedSavedTrackFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
7479
}
@@ -217,6 +222,15 @@ void getSavedAlbumsUsesCorrectValuesToCreateHttpCall() throws IOException {
217222
verify(mockedLibraryService).getSavedAlbums(fakeAccessTokenWithBearer, fakeOptionalParameters);
218223
}
219224

225+
@Test
226+
void getSavedEpisodesUsesCorrectValuesToCreateHttpCall() throws IOException {
227+
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
228+
229+
sut.getSavedEpisodes(fakeOptionalParameters);
230+
231+
verify(mockedLibraryService).getSavedEpisodes(fakeAccessTokenWithBearer, fakeOptionalParameters);
232+
}
233+
220234
@Test
221235
void getSavedAlbumsExecutesHttpCall() throws IOException {
222236
when(mockedSavedAlbumFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
@@ -226,6 +240,15 @@ void getSavedAlbumsExecutesHttpCall() throws IOException {
226240
verify(mockedSavedAlbumFullPagingCall).execute();
227241
}
228242

243+
@Test
244+
void getSavedEpisodesExecutesHttpCall() throws IOException {
245+
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
246+
247+
sut.getSavedEpisodes(fakeOptionalParameters);
248+
249+
verify(mockedSavedEpisodeFullPagingCall).execute();
250+
}
251+
229252
@Test
230253
void getSavedAlbumsThrowsSpotifyActionFailedExceptionWhenError() throws IOException {
231254
when(mockedSavedAlbumFullPagingCall.execute())
@@ -239,20 +262,47 @@ void getSavedAlbumsThrowsSpotifyActionFailedExceptionWhenError() throws IOExcept
239262
Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getSavedAlbums(fakeOptionalParameters));
240263
}
241264

265+
@Test
266+
void getSavedEpisodesThrowsSpotifyActionFailedExceptionWhenError() throws IOException {
267+
when(mockedSavedEpisodeFullPagingCall.execute())
268+
.thenReturn(
269+
Response.error(
270+
400,
271+
ResponseBody.create(MediaType.get("application/json"), getJson("error.json"))
272+
)
273+
);
274+
275+
Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getSavedEpisodes(fakeOptionalParameters));
276+
}
277+
242278
@Test
243279
void getSavedAlbumsThrowsHttpRequestFailedWhenHttpFails() throws IOException {
244280
when(mockedSavedAlbumFullPagingCall.execute()).thenThrow(IOException.class);
245281

246282
Assertions.assertThrows(HttpRequestFailedException.class, () -> sut.getSavedAlbums(fakeOptionalParameters));
247283
}
248284

285+
@Test
286+
void getSavedEpisodesThrowsHttpRequestFailedWhenHttpFails() throws IOException {
287+
when(mockedSavedEpisodeFullPagingCall.execute()).thenThrow(IOException.class);
288+
289+
Assertions.assertThrows(HttpRequestFailedException.class, () -> sut.getSavedEpisodes(fakeOptionalParameters));
290+
}
291+
249292
@Test
250293
void getSavedAlbumsReturnsSavedAlbumFullPagingWhenSuccessful() throws IOException {
251294
when(mockedSavedAlbumFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
252295

253296
Assertions.assertNotNull(sut.getSavedAlbums(fakeOptionalParameters));
254297
}
255298

299+
@Test
300+
void getSavedEpisodesReturnsSavedEpisodeFullPagingWhenSuccessful() throws IOException {
301+
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
302+
303+
Assertions.assertNotNull(sut.getSavedEpisodes(fakeOptionalParameters));
304+
}
305+
256306
@Test
257307
void getSavedShowsUsesCorrectValuesToCreateHttpCall() throws IOException {
258308
when(mockedSavedAShowSimplifiedPagingCall.execute()).thenReturn(Response.success(new Paging<>()));

0 commit comments

Comments
 (0)