Skip to content

Latest commit

 

History

History
127 lines (99 loc) · 4.36 KB

README.md

File metadata and controls

127 lines (99 loc) · 4.36 KB

Spotify-API-Wrapper

An intuitive wrapper over the Spotify Developer API

Integrating with your App

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.

General Usage

The API-Wrapper consists of two basic parts; the Client and the Requests.

Client

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

Client Credentials Flow
SpotifyApiClient client = SpotifyApiClient
    .createClientCredentialsFlowClient(CLIENT_ID, CLIENT_SECRET);
Authorization Flow
SpotifyApiClient client = SpotifyApiClient
    .createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);

Requests

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();

Sending a Synchronous Request (full example)

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);

Sending an Asynchronous Request (full example)

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);
    });

Reauthorizing an Expired Client

SpotifyApiClient client = SpotifyApiClient
    .createAuthorizationFlowClient(CLIENT_ID, CLIENT_SECRET, CODE, REDIRECT_URL);
 
// one method call to reauthorize the client
client.reauthorize();
// or asynchronously
client.reauthorizeAsync();

Caching with ETags

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);