Skip to content

Get User's Saved Episodes #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 65 additions & 23 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,89 @@
name: "CodeQL"
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL Advanced"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: "38 5 * * 6"
- cron: '42 0 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
name: Analyze (${{ matrix.language }})
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners (GitHub.com only)
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
permissions:
# required for all workflows
security-events: write

# required to fetch internal or private CodeQL packs
packages: read

# only required for workflows in private repositories
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ java ]

include:
- language: java-kotlin
build-mode: none # This mode only analyzes Java. Set this to 'autobuild' or 'manual' to analyze Kotlin too.
# CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
# Use `c-cpp` to analyze code written in C, C++ or both
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
queries: +security-and-quality
build-mode: ${{ matrix.build-mode }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

- name: Autobuild
uses: github/codeql-action/autobuild@v2
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
# If the analyze step fails for one of the languages you are analyzing with
# "We were unable to automatically build your code", modify the matrix above
# to set the build mode to "manual" for that language. Then modify this step
# to build your code.
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
- if: matrix.build-mode == 'manual'
shell: bash
run: |
echo 'If you are using a "manual" build mode for one or more of the' \
'languages you are analyzing, replace this with the commands to build' \
'your code, for example:'
echo ' make bootstrap'
echo ' make release'
exit 1

- name: Perform CodeQL Analysis
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,12 @@ This is the most recent coverage in the repository. The marked endpoints may not
- - [x] Get Current User's Saved Albums
- - [x] Get User's Saved Shows
- - [x] Get Current User's Saved Tracks
- - [x] Get Current User's Saved Episodes
- - [x] Save Albums for Current User
- - [x] Save Shows for Current User
- - [x] Save Tracks for Current User
- [x] Markets
- - [x] Get Available Markets
- [x] Personalization
- - [x] Get User's Top Artists and Tracks
- [x] Player
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/spotify/api/impl/LibraryApiRetrofit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import spotify.exceptions.HttpRequestFailedException;
import spotify.factories.RetrofitHttpServiceFactory;
import spotify.models.albums.SavedAlbumFull;
import spotify.models.episodes.SavedEpisodeFull;
import spotify.models.paging.Paging;
import spotify.models.shows.SavedShowSimplified;
import spotify.models.tracks.SavedTrackFull;
Expand Down Expand Up @@ -127,6 +128,29 @@ public Paging<SavedAlbumFull> getSavedAlbums(Map<String, String> options) {
}
}

@Override
public Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options) {
options = ValidatorUtil.optionsValueCheck(options);

logger.trace("Constructing HTTP call to fetch current user's saved episodes");
Call<Paging<SavedEpisodeFull>> httpCall = libraryService.getSavedEpisodes("Bearer " + this.accessToken, options);

try {
logger.info("Executing HTTP call to fetch current user's saved episodes");
logger.debug("Fetching current user's saved episodes with the following values: {}.", options);
LoggingUtil.logHttpCall(logger, httpCall);
Response<Paging<SavedEpisodeFull>> response = httpCall.execute();

ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response, HttpStatusCode.OK);

logger.info("Saved episodes have been successfully fetched");
return response.body();
} catch (IOException ex) {
logger.error("HTTP request to fetch saved episodes has failed.");
throw new HttpRequestFailedException(ex.getMessage());
}
}

@Override
public Paging<SavedShowSimplified> getSavedShows(Map<String, String> options) {
options = ValidatorUtil.optionsValueCheck(options);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/spotify/api/interfaces/LibraryApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spotify.api.interfaces;

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

void deleteTracks(List<String> listOfTrackIds);

Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options);
}
6 changes: 6 additions & 0 deletions src/main/java/spotify/api/spotify/SpotifyApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import spotify.models.episodes.EpisodeFull;
import spotify.models.episodes.EpisodeFullCollection;
import spotify.models.episodes.EpisodeSimplified;
import spotify.models.episodes.SavedEpisodeFull;
import spotify.models.generic.Image;
import spotify.models.markets.MarketFull;
import spotify.models.paging.CursorBasedPaging;
Expand Down Expand Up @@ -264,6 +265,11 @@ public Paging<SavedAlbumFull> getSavedAlbums(Map<String, String> options) {
return libraryApi.getSavedAlbums(options);
}

public Paging<SavedEpisodeFull> getSavedEpisodes(Map<String, String> options) {
logger.info("Requesting to fetch current user's saved episodes");
return libraryApi.getSavedEpisodes(options);
}

public Paging<SavedShowSimplified> getSavedShows(Map<String, String> options) {
logger.info("Requesting to fetch current user's saved shows");
return libraryApi.getSavedShows(options);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spotify/retrofit/services/LibraryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import retrofit2.Call;
import retrofit2.http.*;
import spotify.models.albums.SavedAlbumFull;
import spotify.models.episodes.SavedEpisodeFull;
import spotify.models.paging.Paging;
import spotify.models.shows.SavedShowSimplified;
import spotify.models.tracks.SavedTrackFull;
Expand All @@ -23,6 +24,9 @@ public interface LibraryService {
@GET("me/albums")
Call<Paging<SavedAlbumFull>> getSavedAlbums(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);

@GET("me/episodes")
Call<Paging<SavedEpisodeFull>> getSavedEpisodes(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);

@GET("me/shows")
Call<Paging<SavedShowSimplified>> getSavedShows(@Header("Authorization") String accessToken, @QueryMap Map<String, String> options);

Expand Down
50 changes: 50 additions & 0 deletions src/test/java/spotify/api/impl/LibraryApiRetrofitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import spotify.exceptions.HttpRequestFailedException;
import spotify.exceptions.SpotifyActionFailedException;
import spotify.models.albums.SavedAlbumFull;
import spotify.models.episodes.SavedEpisodeFull;
import spotify.models.paging.Paging;
import spotify.models.shows.SavedShowSimplified;
import spotify.models.tracks.SavedTrackFull;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class LibraryApiRetrofitTest extends AbstractApiRetrofitTest {
@Mock
private Call<Paging<SavedTrackFull>> mockedSavedTrackFullPagingCall;
@Mock
private Call<Paging<SavedEpisodeFull>> mockedSavedEpisodeFullPagingCall;
@Mock
private Call<Void> mockedVoidCall;

@BeforeEach
Expand All @@ -57,6 +60,7 @@ void setup() {
when(mockedLibraryService.hasSavedShows(fakeAccessTokenWithBearer, fakeShowIds)).thenReturn(mockedListOfBooleanCall);
when(mockedLibraryService.hasSavedTracks(fakeAccessTokenWithBearer, fakeTrackIds)).thenReturn(mockedListOfBooleanCall);
when(mockedLibraryService.getSavedAlbums(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedAlbumFullPagingCall);
when(mockedLibraryService.getSavedEpisodes(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedEpisodeFullPagingCall);
when(mockedLibraryService.getSavedShows(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedAShowSimplifiedPagingCall);
when(mockedLibraryService.getSavedTracks(fakeAccessTokenWithBearer, fakeOptionalParameters)).thenReturn(mockedSavedTrackFullPagingCall);
when(mockedLibraryService.saveAlbums(fakeAccessTokenWithBearer, fakeAlbumIds)).thenReturn(mockedVoidCall);
Expand All @@ -69,6 +73,7 @@ void setup() {
when(mockedVoidCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
when(mockedListOfBooleanCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
when(mockedSavedAlbumFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
when(mockedSavedEpisodeFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
when(mockedSavedAShowSimplifiedPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
when(mockedSavedTrackFullPagingCall.request()).thenReturn(new Request.Builder().url(fakeUrl).build());
}
Expand Down Expand Up @@ -217,6 +222,15 @@ void getSavedAlbumsUsesCorrectValuesToCreateHttpCall() throws IOException {
verify(mockedLibraryService).getSavedAlbums(fakeAccessTokenWithBearer, fakeOptionalParameters);
}

@Test
void getSavedEpisodesUsesCorrectValuesToCreateHttpCall() throws IOException {
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));

sut.getSavedEpisodes(fakeOptionalParameters);

verify(mockedLibraryService).getSavedEpisodes(fakeAccessTokenWithBearer, fakeOptionalParameters);
}

@Test
void getSavedAlbumsExecutesHttpCall() throws IOException {
when(mockedSavedAlbumFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
Expand All @@ -226,6 +240,15 @@ void getSavedAlbumsExecutesHttpCall() throws IOException {
verify(mockedSavedAlbumFullPagingCall).execute();
}

@Test
void getSavedEpisodesExecutesHttpCall() throws IOException {
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));

sut.getSavedEpisodes(fakeOptionalParameters);

verify(mockedSavedEpisodeFullPagingCall).execute();
}

@Test
void getSavedAlbumsThrowsSpotifyActionFailedExceptionWhenError() throws IOException {
when(mockedSavedAlbumFullPagingCall.execute())
Expand All @@ -239,20 +262,47 @@ void getSavedAlbumsThrowsSpotifyActionFailedExceptionWhenError() throws IOExcept
Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getSavedAlbums(fakeOptionalParameters));
}

@Test
void getSavedEpisodesThrowsSpotifyActionFailedExceptionWhenError() throws IOException {
when(mockedSavedEpisodeFullPagingCall.execute())
.thenReturn(
Response.error(
400,
ResponseBody.create(MediaType.get("application/json"), getJson("error.json"))
)
);

Assertions.assertThrows(SpotifyActionFailedException.class, () -> sut.getSavedEpisodes(fakeOptionalParameters));
}

@Test
void getSavedAlbumsThrowsHttpRequestFailedWhenHttpFails() throws IOException {
when(mockedSavedAlbumFullPagingCall.execute()).thenThrow(IOException.class);

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

@Test
void getSavedEpisodesThrowsHttpRequestFailedWhenHttpFails() throws IOException {
when(mockedSavedEpisodeFullPagingCall.execute()).thenThrow(IOException.class);

Assertions.assertThrows(HttpRequestFailedException.class, () -> sut.getSavedEpisodes(fakeOptionalParameters));
}

@Test
void getSavedAlbumsReturnsSavedAlbumFullPagingWhenSuccessful() throws IOException {
when(mockedSavedAlbumFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));

Assertions.assertNotNull(sut.getSavedAlbums(fakeOptionalParameters));
}

@Test
void getSavedEpisodesReturnsSavedEpisodeFullPagingWhenSuccessful() throws IOException {
when(mockedSavedEpisodeFullPagingCall.execute()).thenReturn(Response.success(new Paging<>()));

Assertions.assertNotNull(sut.getSavedEpisodes(fakeOptionalParameters));
}

@Test
void getSavedShowsUsesCorrectValuesToCreateHttpCall() throws IOException {
when(mockedSavedAShowSimplifiedPagingCall.execute()).thenReturn(Response.success(new Paging<>()));
Expand Down