Skip to content

Commit 5e064ca

Browse files
eleonora2687jzheng2017
authored andcommitted
feat: add files to introduce new functionality
1 parent 4372348 commit 5e064ca

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package spotify.api.impl;
2+
3+
import java.io.IOException;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import retrofit2.Call;
9+
import retrofit2.Response;
10+
import spotify.api.enums.HttpStatusCode;
11+
import spotify.api.interfaces.MarketApi;
12+
import spotify.exceptions.HttpRequestFailedException;
13+
import spotify.factories.RetrofitHttpServiceFactory;
14+
import spotify.models.markets.MarketFull;
15+
import spotify.retrofit.services.MarketService;
16+
import spotify.utils.LoggingUtil;
17+
import spotify.utils.ResponseChecker;
18+
19+
20+
public class MarketApiRetrofit implements MarketApi {
21+
22+
private final Logger logger = LoggerFactory.getLogger(MarketApiRetrofit.class);
23+
private final String accessToken;
24+
private final MarketService marketService;
25+
26+
27+
public MarketApiRetrofit(final String accessToken) {
28+
this(accessToken, RetrofitHttpServiceFactory.getMarketService());
29+
}
30+
31+
public MarketApiRetrofit(final String accessToken, final MarketService marketService) {
32+
this.accessToken = accessToken;
33+
this.marketService = marketService;
34+
}
35+
36+
37+
@Override
38+
public MarketFull getMarkets(){
39+
logger.trace("Constructing HTTP call to fetch markets.");
40+
Call<MarketFull> httpCall = marketService.getMarkets("Bearer " + this.accessToken);
41+
42+
try {
43+
44+
logger.info("Executing HTTP call to fetch markets");
45+
logger.debug("Fetching markets....");
46+
LoggingUtil.logHttpCall(logger, httpCall);
47+
Response<MarketFull> response = httpCall.execute();
48+
49+
ResponseChecker.throwIfRequestHasNotBeenFulfilledCorrectly(response, HttpStatusCode.OK);
50+
51+
logger.info("Markets fetched successfully.");
52+
return response.body();
53+
54+
} catch (IOException ex) {
55+
logger.error("HTTP request to fetch requested markets has failed.");
56+
throw new HttpRequestFailedException(ex.getMessage());
57+
}
58+
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package spotify.api.interfaces;
2+
3+
import spotify.models.markets.MarketFull;
4+
5+
public interface MarketApi {
6+
MarketFull getMarkets();
7+
8+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import spotify.models.episodes.EpisodeFullCollection;
2121
import spotify.models.episodes.EpisodeSimplified;
2222
import spotify.models.generic.Image;
23+
import spotify.models.markets.MarketFull;
2324
import spotify.models.paging.CursorBasedPaging;
2425
import spotify.models.paging.Paging;
2526
import spotify.models.players.CurrentlyPlayingObject;
@@ -62,6 +63,7 @@ public class SpotifyApi {
6263
private PersonalizationApi personalizationApi;
6364
private PlayerApi playerApi;
6465
private SearchApi searchApi;
66+
private MarketApi marketApi;
6567

6668
public SpotifyApi(final String accessToken) {
6769
this.setup(accessToken);
@@ -72,7 +74,6 @@ public void setApis(TrackApi trackApi, AlbumApi albumApi) {
7274
this.albumApi = albumApi;
7375
}
7476

75-
7677
public TrackFull getTrack(String trackId, Map<String, String> options) {
7778
logger.info("Requesting a track with id {}.", trackId);
7879
return trackApi.getTrack(trackId, options);
@@ -103,6 +104,11 @@ public AlbumFull getAlbum(String albumId, Map<String, String> options) {
103104
return albumApi.getAlbum(albumId, options);
104105
}
105106

107+
public MarketFull getMarket() {
108+
logger.info("Requesting all available markets");
109+
return marketApi.getMarkets();
110+
}
111+
106112
public AlbumFullCollection getAlbums(List<String> listOfAlbumIds, Map<String, String> options) {
107113
logger.info("Requesting multiple albums.");
108114
return albumApi.getAlbums(listOfAlbumIds, options);
@@ -460,5 +466,6 @@ private void setup(final String accessToken) {
460466
this.personalizationApi = new PersonalizationApiRetrofit(accessToken);
461467
this.playerApi = new PlayerApiRetrofit(accessToken);
462468
this.searchApi = new SearchApiRetrofit(accessToken);
469+
this.marketApi = new MarketApiRetrofit(accessToken);
463470
}
464471
}

src/main/java/spotify/factories/RetrofitHttpServiceFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ private static <T> T getRetrofitHttpService(final Class<T> serviceClassToBeCreat
8080

8181
return httpClient.create(serviceClassToBeCreatedFor);
8282
}
83+
84+
public static MarketService getMarketService() {
85+
return getRetrofitHttpService(MarketService.class, API_BASE_URL_HTTPS_WITH_VERSION);
86+
}
8387
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package spotify.models.markets;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class MarketFull {
7+
8+
private List<String> markets = new ArrayList<String>() ;
9+
public List<String> getAllMarkets() {
10+
return markets;
11+
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package spotify.retrofit.services;
2+
3+
import retrofit2.Call;
4+
import retrofit2.http.GET;
5+
import retrofit2.http.Header;
6+
import spotify.models.markets.MarketFull;
7+
8+
public interface MarketService {
9+
@GET("markets")
10+
Call<MarketFull> getMarkets(@Header("Authorization") String accessToken);
11+
12+
}

0 commit comments

Comments
 (0)