Skip to content

A wrapper for Spotify Web API. It uses Retrofit to create Java interfaces from API endpoints

License

Notifications You must be signed in to change notification settings

ryandt/spotify-web-api-android

 
 

Repository files navigation

Build Status

Spotify Web API for Android

This project is a wrapper for the Spotify Web API. It uses Retrofit to create Java interfaces from API endpoints.

Building

This project is built using Gradle:

  1. Clone the repository: git clone https://github.com/kaaes/spotify-web-api-android.git
  2. Build: ./gradlew jar
  3. Grab the jar and put it in your project. It can be found in build/libs/spotify-web-api-android-0.1.0.jar

Dependencies

This project depends on Retrofit 1.9.0 and OkHttp 2.2.0. When you build it using the command above it creates a lean jar that doesn't contain Retrofit and OkHttp files. To make your app work you'll need to include these dependencies in your app:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'

It is also possible to build the project as a fat jar that contains all dependencies. To do this run:

./gradlew jarAll

the jar will be located in build/libs/spotify-web-api-android-all-0.1.0.jar

Usage

Out of the box it uses OkHttp HTTP client and a single thread executor.

SpotifyApi api = new SpotifyApi();

// Most (but not all) of the Spotify Web API endpoints require authorisation.
// If you know you'll only use the ones that don't require authorisation you can skip this step
api.setAccessToken("myAccessToken");

SpotifyService spotify = api.getService();

spotify.getAlbum("2dIGnmEIy1WZIcZCFSj6i8", new Callback<Album>() {
    @Override
    public void success(Album album, Response response) {
        Log.d("Album success", album.name);
    }

    @Override
    public void failure(RetrofitError error) {
        Log.d("Album failure", error.toString());
    }
});

It is also possible to construct the adapter with custom parameters.

final String accessToken = "myAccessToken";

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(SpotifyApi.SPOTIFY_WEB_API_ENDPOINT)
        .setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("Authorization", "Bearer " + accessToken);
            }
        })
        .build();

SpotifyService spotify = restAdapter.create(SpotifyService.class);

Error Handling

When using Retrofit, errors are returned as RetrofitError objects. These objects, among others, contain HTTP status codes and their descriptions, for example 400 - Bad Request. In many cases this will work well enough but in some cases Spotify Web API returns more detailed information, for example 400 - No search query.

To use the data returned in the response from the Web API SpotifyCallback object should be passed to the request method instead of regular Retrofit's Callback:

spotify.getMySavedTracks(new SpotifyCallback<Pager<SavedTrack>>;() {
    @Override
    public void success(Pager<SavedTrack> savedTrackPager, Response response) {
        // handle successful response
    }

    @Override
    public void failure(SpotifyError error) {
        // handle error
    }
});

For synchronous requests RetrofitError can be converted to SpotifyError if needed:

try {
    Pager<SavedTrack> mySavedTracks = spotify.getMySavedTracks();
} catch (RetrofitError error) {
    SpotifyError spotifyError = SpotifyError.fromRetrofitError(error);
    // handle error
}

Help

Bugs, Feature requests

Found a bug? Something that's missing? Feedback is an important part of improving the project, so please open an issue.

Code

Fork this project and start working on your own feature branch. When you're done, send a Pull Request to have your suggested changes merged into the master branch by the project's collaborators. Read more about the GitHub flow.

About

A wrapper for Spotify Web API. It uses Retrofit to create Java interfaces from API endpoints

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.4%
  • Shell 0.6%