|
10 | 10 | import spotify.exceptions.HttpRequestFailedException; |
11 | 11 | import spotify.exceptions.ResponseChecker; |
12 | 12 | import spotify.factories.RetrofitClientFactory; |
| 13 | +import spotify.models.albums.AlbumSimplifiedPaging; |
13 | 14 | import spotify.models.categories.CategoryFull; |
14 | 15 | import spotify.models.categories.CategoryFullPaging; |
15 | 16 | import spotify.models.playlists.FeaturedPlaylistCollection; |
16 | 17 | import spotify.models.playlists.PlaylistSimplifiedPaging; |
| 18 | +import spotify.models.recommendations.RecommendationCollection; |
17 | 19 | import spotify.retrofit.services.BrowseService; |
18 | 20 | import spotify.utils.ValidatorUtil; |
19 | 21 |
|
20 | 22 | import java.io.IOException; |
| 23 | +import java.util.List; |
21 | 24 | import java.util.Map; |
22 | 25 |
|
23 | 26 | public class BrowseApiRetrofit implements BrowseApi { |
@@ -122,6 +125,72 @@ public FeaturedPlaylistCollection getFeaturedPlaylists(Map<String, String> optio |
122 | 125 | } |
123 | 126 | } |
124 | 127 |
|
| 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 | + |
125 | 194 | private void setup() { |
126 | 195 | logger.trace("Requesting Retrofit HTTP client."); |
127 | 196 | Retrofit httpClient = RetrofitClientFactory.getRetrofitClient(ApiUrl.API_URL_HTTPS + ApiUrl.VERSION); |
|
0 commit comments