An intuitive wrapper over the Spotify Developer API
Maven
<dependency>
<groupId>com.github.lajospolya</groupId>
<artifactId>spotify-api-wrapper</artifactId>
<version>3.0.RELEASE</version>
</dependency>
Gradle
implementation group: 'com.github.lajospolya', name: 'spotify-api-wrapper', version: '3.0.RELEASE'
Please note that only versions 3.0.RELEASE
and above are Android compatible.
For other Dependency Management systems please check Maven or Sonatype repositories.
The API-Wrapper consists of two basic parts; the Client and the Requests.
The Client is represented by the SpotifyApiClient
class. The client can be authenticated using the
Client Credentials Flow
or the
Authorization Code Flow
via two static factory methods.
To read more about Spotify Authorization, please checkout this link
SpotifyApiClient client = SpotifyApiClient
.createClientCredentialsFlowClient(CLIENT_ID, CLIENT_SECRET);
SpotifyApiClient client = SpotifyApiClient
.createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);
All requests extend AbstractSpotifyRequest<?>
Requests are built using the Builder Pattern. All parameters for the Builder
constructor are mandatory to the request. All appended parameters (ex, market) are optional parameters.
for example
String TRACK_ID = "1EaKU4dMbesXXd3BrLCtYG";
String MARKET = "CA";
GetTrack getTrackRequest = new GetTrack.Builder(TRACK_ID)
.market(MARKET).build();
The responses for all requests are typed.
SpotifyApiClient client = SpotifyApiClient
.createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);
String TRACK_ID = "1EaKU4dMbesXXd3BrLCtYG";
String MARKET = "CA";
GetTrack getTrackRequest = new GetTrack.Builder(TRACK_ID)
.market(MARKET).build();
Track track = client.sendRequest(getTrackRequest);
String SHOW_ID = "4xdoysfv0ztl97lrj8Sg4W";
String MARKET = "CA";
GetShowsEpisodes getShowsRequest = new GetShowsEpisodes.Builder(SHOW_ID)
.market(MARKET).build();
client.sendRequestAsync(getShowsRequest)
.success((episodes) -> {
System.out.println(episodes);
});
SpotifyApiClient client = SpotifyApiClient
.createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);
// one method call to reauthorize the client
client.reauthorize();
// or asynchronously
client.reauthorizeAsync();
Spotify has implemented Contitional Requests through the use of ETags.
Requests which support the use of ETags have an etag
setter method.
Responses which support the use of ETags have a getEtag
getter method.
If the ETag is set and the resource being requested hasn't changed then a null object will be returned.
SpotifyApiClient client = SpotifyApiClient
.createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);
GetMeTracks getUsersTracksRequest = new GetMeTracks.Builder()
.offset(0).limit(50).build();
// Fetch my saved Tracks
Paging<SavedTrack> tracks = client.sendRequest(getUsersTracksRequest);
String etag = tracks.getEtag();
GetMeTracks getUsersTracksCachedRequest = new GetMeTracks.Builder()
.etag(etag) // set the etag
.offset(0).limit(50).build();
// If I haven't saved any tracks then tracksCached will be null
Paging<SavedTrack> tracksCached = client.sendRequest(getUsersTracksCachedRequest);