Skip to content

Commit fa6eb25

Browse files
Jiankai ZhengJiankai Zheng
authored andcommitted
feat(api): api call to fetch recommendations
1 parent f178eb5 commit fa6eb25

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
import spotify.exceptions.HttpRequestFailedException;
1111
import spotify.exceptions.ResponseChecker;
1212
import spotify.factories.RetrofitClientFactory;
13+
import spotify.models.albums.AlbumSimplifiedPaging;
1314
import spotify.models.categories.CategoryFull;
1415
import spotify.models.categories.CategoryFullPaging;
1516
import spotify.models.playlists.FeaturedPlaylistCollection;
1617
import spotify.models.playlists.PlaylistSimplifiedPaging;
18+
import spotify.models.recommendations.RecommendationCollection;
1719
import spotify.retrofit.services.BrowseService;
1820
import spotify.utils.ValidatorUtil;
1921

2022
import java.io.IOException;
23+
import java.util.List;
2124
import java.util.Map;
2225

2326
public class BrowseApiRetrofit implements BrowseApi {
@@ -122,6 +125,72 @@ public FeaturedPlaylistCollection getFeaturedPlaylists(Map<String, String> optio
122125
}
123126
}
124127

128+
@Override
129+
public AlbumSimplifiedPaging getNewReleases(Map<String, String> options) {
130+
options = ValidatorUtil.optionsValueCheck(options);
131+
132+
logger.trace("Constructing HTTP call to fetch new releases.");
133+
Call<AlbumSimplifiedPaging> httpCall = browseService.getNewReleases("Bearer " + this.accessToken, options);
134+
135+
try {
136+
logger.info("Executing HTTP call to fetch new releases.");
137+
logger.debug(String.format("Fetching new releases with following values: %s", options));
138+
logger.debug(String.format("%s / %s", httpCall.request().method(), httpCall.request().url().toString()));
139+
Response<AlbumSimplifiedPaging> response = httpCall.execute();
140+
141+
ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response.errorBody());
142+
143+
logger.info("New releases have been successfully fetched.");
144+
return response.body();
145+
} catch (IOException ex) {
146+
logger.error("HTTP request to fetch new releases has failed.");
147+
throw new HttpRequestFailedException(ex.getMessage());
148+
}
149+
}
150+
151+
@Override
152+
public RecommendationCollection getRecommendations(List<String> listOfSeedArtists, List<String> listOfSeedGenres, List<String> listOfSeedTracks, Map<String, String> options) {
153+
options = ValidatorUtil.optionsValueCheck(options);
154+
155+
mapSeedParameters(listOfSeedArtists, listOfSeedGenres, listOfSeedTracks, options);
156+
157+
logger.trace("Constructing HTTP call to fetch recommendations.");
158+
Call<RecommendationCollection> httpCall = browseService.getRecommendations("Bearer " + this.accessToken, options);
159+
160+
try {
161+
logger.info("Executing HTTP call to fetch recommendations.");
162+
logger.debug(String.format("Fetching recommendations with following values: %s", options));
163+
logger.debug(String.format("%s / %s", httpCall.request().method(), httpCall.request().url().toString()));
164+
Response<RecommendationCollection> response = httpCall.execute();
165+
166+
ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response.errorBody());
167+
168+
logger.info("Recommendations have been successfully fetched.");
169+
return response.body();
170+
} catch (IOException ex) {
171+
logger.error("HTTP request to fetch recommendations has failed.");
172+
throw new HttpRequestFailedException(ex.getMessage());
173+
}
174+
}
175+
176+
private void mapSeedParameters(List<String> listOfSeedArtists, List<String> listOfSeedGenres, List<String> listOfSeedTracks, Map<String, String> options) {
177+
final String artistSeedIds = String.join(",", listOfSeedArtists);
178+
final String genreSeedIds = String.join(",", listOfSeedGenres);
179+
final String trackSeedIds = String.join(",", listOfSeedTracks);
180+
181+
if (!artistSeedIds.isEmpty()) {
182+
options.put("seed_artists", artistSeedIds);
183+
}
184+
185+
if (!genreSeedIds.isEmpty()) {
186+
options.put("seed_genres", genreSeedIds);
187+
}
188+
189+
if (!trackSeedIds.isEmpty()) {
190+
options.put("seed_tracks", trackSeedIds);
191+
}
192+
}
193+
125194
private void setup() {
126195
logger.trace("Requesting Retrofit HTTP client.");
127196
Retrofit httpClient = RetrofitClientFactory.getRetrofitClient(ApiUrl.API_URL_HTTPS + ApiUrl.VERSION);

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

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

3+
import spotify.models.albums.AlbumSimplifiedPaging;
34
import spotify.models.categories.CategoryFull;
45
import spotify.models.categories.CategoryFullPaging;
56
import spotify.models.playlists.FeaturedPlaylistCollection;
67
import spotify.models.playlists.PlaylistSimplifiedPaging;
8+
import spotify.models.recommendations.RecommendationCollection;
79

10+
import java.util.List;
811
import java.util.Map;
912

1013
public interface BrowseApi {
@@ -15,4 +18,8 @@ public interface BrowseApi {
1518
CategoryFullPaging getCategories(Map<String, String> options);
1619

1720
FeaturedPlaylistCollection getFeaturedPlaylists(Map<String, String> options);
21+
22+
AlbumSimplifiedPaging getNewReleases(Map<String, String> options);
23+
24+
RecommendationCollection getRecommendations(List<String> listOfSeedArtists, List<String> listOfSeedGenres, List<String> listOfSeedTracks, Map<String, String> options);
1825
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import spotify.api.interfaces.*;
88
import spotify.models.albums.AlbumFull;
99
import spotify.models.albums.AlbumFullCollection;
10+
import spotify.models.albums.AlbumSimplifiedPaging;
1011
import spotify.models.artists.ArtistFull;
1112
import spotify.models.artists.ArtistFullCollection;
1213
import spotify.models.artists.ArtistSimplified;
@@ -21,6 +22,7 @@
2122
import spotify.models.paging.Paging;
2223
import spotify.models.playlists.FeaturedPlaylistCollection;
2324
import spotify.models.playlists.PlaylistSimplifiedPaging;
25+
import spotify.models.recommendations.RecommendationCollection;
2426
import spotify.models.shows.ShowFull;
2527
import spotify.models.shows.ShowSimplifiedCollection;
2628
import spotify.models.tracks.TrackFull;
@@ -172,6 +174,16 @@ public FeaturedPlaylistCollection getFeaturedPlaylists(Map<String, String> optio
172174
return browseApi.getFeaturedPlaylists(options);
173175
}
174176

177+
public AlbumSimplifiedPaging getNewReleases(Map<String, String> options) {
178+
logger.info("Requesting new releases");
179+
return browseApi.getNewReleases(options);
180+
}
181+
182+
public RecommendationCollection getRecommendations(List<String> listOfSeedArtists, List<String> listOfSeedGenres, List<String> listOfSeedTracks, Map<String, String> options) {
183+
logger.info("Requesting recommendations");
184+
return browseApi.getRecommendations(listOfSeedArtists, listOfSeedGenres, listOfSeedTracks, options);
185+
}
186+
175187
private void setup(String accessToken) {
176188
logger.trace("Constructing Retrofit APIs");
177189
this.trackApi = new TrackApiRetrofit(accessToken);

0 commit comments

Comments
 (0)