Skip to content

Commit

Permalink
Fix okhttp3 upgrade again
Browse files Browse the repository at this point in the history
  • Loading branch information
geecko86 committed Feb 11, 2017
1 parent 22483ac commit 1e4341c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 95 deletions.
7 changes: 4 additions & 3 deletions SpotifyApi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ android {
}

dependencies {
compile 'com.android.support:support-annotations:21.0.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.android.support:support-annotations:25.1.1'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
Expand Down Expand Up @@ -224,7 +225,13 @@ private class WebApiAuthenticator implements Interceptor {
public okhttp3.Response intercept(Chain chain) {
AccessToken token = mTokenStore.read();
if (token != null) {
chain.request().header("Authorization:"+ token.tokenType + " " + token.accessToken);
try {
Request request = chain.request().newBuilder()
.addHeader("Authorization", token.tokenType + " " + token.accessToken).build();
return chain.proceed(request);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
Expand Down
112 changes: 56 additions & 56 deletions SpotifyApi/src/main/java/com/drivemode/spotify/SpotifyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,205 +40,205 @@ public interface SpotifyService {
* Profiles *
*/

@GET("/me")
@GET("me")
public Call<User> getMe();

@GET("/user/{id}")
@GET("user/{id}")
public Call<User> getUser(@Path("id") String userId);

/**
* Playlists *
*/

@GET("/users/{id}/playlists")
@GET("users/{id}/playlists")
public Call<Pager<Playlist>> getPlaylists(@Path("id") String userId, @Query("offset") int offset, @Query("limit") int limit);

@GET("/users/{id}/playlists")
@GET("users/{id}/playlists")
public Call<Pager<Playlist>> getPlaylists(@Path("id") String userId);

@GET("/users/{user_id}/playlists/{playlist_id}")
@GET("users/{user_id}/playlists/{playlist_id}")
public Call<Playlist> getPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId);

@GET("/users/{user_id}/playlists/{playlist_id}/tracks")
@GET("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<Pager<PlaylistTrack>> getPlaylistTracks(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("offset") int offset, @Query("limit") int limit);

@GET("/users/{user_id}/playlists/{playlist_id}/tracks")
@GET("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<Pager<PlaylistTrack>> getPlaylistTracks(@Path("user_id") String userId, @Path("playlist_id") String playlistId);

@POST("/users/{user_id}/playlists")
@POST("users/{user_id}/playlists")
public Call<Playlist> createPlaylist(@Path("user_id") String userId, @Query("name") String name);

@POST("/users/{user_id}/playlists")
@POST("users/{user_id}/playlists")
public Call<Playlist> createPlaylist(@Path("user_id") String userId, @Query("name") String name, @Query("public") boolean is_public);

@POST("/users/{user_id}/playlists/{playlist_id}/tracks")
@POST("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<SnapshotId> addTracksToPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("uris") String trackUris);

@POST("/users/{user_id}/playlists/{playlist_id}/tracks")
@POST("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<SnapshotId> addTracksToPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("uris") String trackUris, @Query("position") int position);

@DELETEWITHBODY("/users/{user_id}/playlists/{playlist_id}/tracks")
@DELETEWITHBODY("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<SnapshotId> removeTracksFromPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Body TracksToRemove tracksToRemove);

@DELETEWITHBODY("/users/{user_id}/playlists/{playlist_id}/tracks")
@DELETEWITHBODY("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<SnapshotId> removeTracksFromPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Body TracksToRemoveWithPosition tracksToRemoveWithPosition);

// todo: process status code and return boolean
@PUT("/users/{user_id}/playlists/{playlist_id}/tracks")
@PUT("users/{user_id}/playlists/{playlist_id}/tracks")
public Call<Boolean> replaceTracksInPlaylist(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("uris") String trackUris);

// todo: process status code and return boolean
@PUT("/users/{user_id}/playlists/{playlist_id}")
@PUT("users/{user_id}/playlists/{playlist_id}")
public Call<Boolean> changePlaylistDetails(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("name") String name);

// todo: process status code and return boolean
@PUT("/users/{user_id}/playlists/{playlist_id}")
@PUT("users/{user_id}/playlists/{playlist_id}")
public Call<Boolean> changePlaylistDetails(@Path("user_id") String userId, @Path("playlist_id") String playlistId, @Query("public") boolean is_public);

/**
* Albums *
*/

@GET("/albums/{id}")
@GET("albums/{id}")
public Call<Album> getAlbum(@Path("id") String albumId);

@GET("/albums")
@GET("albums")
public Call<Albums> getAlbums(@Query("ids") String albumIds);

@GET("/albums/{id}/tracks")
@GET("albums/{id}/tracks")
public Call<Pager<Track>> getAlbumTracks(@Path("id") String albumId);

@GET("/albums/{id}/tracks")
@GET("albums/{id}/tracks")
public Call<Pager<Track>> getAlbumTracks(@Path("id") String albumId, @Query("offset") int offset, @Query("limit") int limit);

/**
* Artists *
*/

@GET("/artists/{id}/albums")
@GET("artists/{id}/albums")
public Call<Pager<Album>> getArtistAlbums(@Path("id") String artistId, @Query("offset") int offset, @Query("limit") int limit);

@GET("/artists/{id}/albums")
@GET("artists/{id}/albums")
public Call<Pager<Album>> getArtistAlbums(@Path("id") String artistId);

@GET("/artists/{id}/top-tracks")
@GET("artists/{id}/top-tracks")
public Call<Pager<Track>> getArtistTopTrack(@Path("id") String artistId, @Query("offset") int offset, @Query("limit") int limit);

@GET("/artists/{id}/top-tracks")
@GET("artists/{id}/top-tracks")
public Call<Pager<Track>> getArtistTopTrack(@Path("id") String artistId);

@GET("/artists/{id}/related-artists")
@GET("artists/{id}/related-artists")
public Call<Pager<Artist>> getRelatedArtists(@Path("id") String artistId, @Query("offset") int offset, @Query("limit") int limit);

@GET("/artists/{id}/related-artists")
@GET("artists/{id}/related-artists")
public Call<Pager<Artist>> getRelatedArtists(@Path("id") String artistId);

@GET("/artists/{id}")
@GET("artists/{id}")
public Call<Artist> getArtist(@Path("id") String artistId);

@GET("/artists")
@GET("artists")
public Call<Artists> getArtists(@Query("ids") String artistIds);

/**
* Tracks *
*/

@GET("/tracks/{id}")
@GET("tracks/{id}")
public Call<Track> getTrack(@Path("id") String trackId);

@GET("/tracks")
@GET("tracks")
public Call<Tracks> getTracks(@Query("ids") String trackIds);

/**
* Browse *
*/

@GET("/browse/featured-playlists")
@GET("browse/featured-playlists")
public Call<FeaturedPlaylists> getFeaturedPlaylists(@QueryMap Map<String, String> options);

@GET("/browse/featured-playlists")
@GET("browse/featured-playlists")
public Call<FeaturedPlaylists> getFeaturedPlaylists(@QueryMap Map<String, String> options, @Query("offset") int offset, @Query("limit") int limit);

@GET("/browse/new-releases")
@GET("browse/new-releases")
public Call<NewReleases> getNewReleases();

@GET("/browse/new-releases")
@GET("browse/new-releases")
public Call<NewReleases> getNewReleases(@Query("country") String country);

@GET("/browse/new-releases")
@GET("browse/new-releases")
public Call<NewReleases> getNewReleases(@Query("country") String country, @Query("offset") int offset, @Query("limit") int limit);


/**
* Library / Your Music *
*/

@GET("/me/tracks")
@GET("me/tracks")
public Call<Pager<SavedTrack>> getMySavedTracks();

@GET("/me/tracks")
@GET("me/tracks")
public Call<Pager<SavedTrack>> getMySavedTracks(@Query("offset") int offset, @Query("limit") int limit);

@GET("/me/tracks/contains")
@GET("me/tracks/contains")
public Call<Boolean[]> containsMySavedTracks(@Query("ids") String ids);

// todo: process status code and return boolean
@PUT("/me/tracks")
@PUT("me/tracks")
public Call<Boolean> addToMySavedTracks(@Query("ids") String ids);

// todo: process status code and return boolean
@DELETE("/me/tracks")
@DELETE("me/tracks")
public Call<Boolean> removeFromMySavedTracks(@Query("ids") String ids);

/**
* Search *
*/

@GET("/search?type=track")
@GET("search?type=track")
public Call<TracksPager> searchTracks(@Query("q") String q);

@GET("/search?type=track")
@GET("search?type=track")
public Call<TracksPager> searchTracks(@Query("q") String q, @Query("market") String market);

@GET("/search?type=track")
@GET("search?type=track")
public Call<TracksPager> searchTracks(@Query("q") String q, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=track")
@GET("search?type=track")
public Call<TracksPager> searchTracks(@Query("q") String q, @Query("market") String market, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=artist")
@GET("search?type=artist")
public Call<ArtistsPager> searchArtists(@Query("q") String q);

@GET("/search?type=artist")
@GET("search?type=artist")
public Call<ArtistsPager> searchArtists(@Query("q") String q, @Query("market") String market);

@GET("/search?type=artist")
@GET("search?type=artist")
public Call<ArtistsPager> searchArtists(@Query("q") String q, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=artist")
@GET("search?type=artist")
public Call<ArtistsPager> searchArtists(@Query("q") String q, @Query("market") String market, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=album")
@GET("search?type=album")
public Call<AlbumsPager> searchAlbums(@Query("q") String q);

@GET("/search?type=album")
@GET("search?type=album")
public Call<AlbumsPager> searchAlbums(@Query("q") String q, @Query("market") String market);

@GET("/search?type=album")
@GET("search?type=album")
public Call<AlbumsPager> searchAlbums(@Query("q") String q, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=album")
@GET("search?type=album")
public Call<AlbumsPager> searchAlbums(@Query("q") String q, @Query("market") String market, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=playlist")
@GET("search?type=playlist")
public Call<PlaylistsPager> searchPlaylists(@Query("q") String q);

@GET("/search?type=playlist")
@GET("search?type=playlist")
public Call<PlaylistsPager> searchPlaylists(@Query("q") String q, @Query("market") String market);

@GET("/search?type=playlist")
@GET("search?type=playlist")
public Call<PlaylistsPager> searchPlaylists(@Query("q") String q, @Query("offset") int offset, @Query("limit") int limit);

@GET("/search?type=playlist")
@GET("search?type=playlist")
public Call<PlaylistsPager> searchPlaylists(@Query("q") String q, @Query("market") String market, @Query("offset") int offset, @Query("limit") int limit);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.drivemode.spotify.rest;

import com.drivemode.spotify.auth.AccessToken;
import com.drivemode.spotify.models.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
* @author KeishinYokomaku
Expand All @@ -31,6 +36,9 @@ public Retrofit provideWebApiAdapter(Interceptor interceptor) {
return new Retrofit.Builder()
.client(mOkClient)
.baseUrl(SPOTIFY_WEB_API_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
.registerTypeAdapter(Call.class, new InterfaceAdapter<Call<User>>())
.create()))
.build();
}

Expand All @@ -41,23 +49,39 @@ public Retrofit provideAuthenticateApiAdapter() {
return new Retrofit.Builder()
.client(mOkClient)
.baseUrl(SPOTIFY_AUTHENTICATE_ENDPOINT)
.addConverterFactory(new Converter.Factory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, AccessToken>() {
@Override
public AccessToken convert(ResponseBody value) throws IOException {
AccessToken token = new AccessToken();
JsonObject responseOBject = (JsonObject) new JsonParser().parse(value.string());
token.accessToken = responseOBject.get("access_token").getAsString();
token.expiresIn = responseOBject.get("expires_in").getAsLong();
token.refreshToken = responseOBject.get("refresh_token").getAsString();
token.tokenType = responseOBject.get("token_type").getAsString();
return token;
}
};
}
})
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().registerTypeAdapter(User.class, new InterfaceAdapter<User>())
.create()))
.build();
}

private static final class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) {
final JsonObject wrapper = new JsonObject();
wrapper.addProperty("type", object.getClass().getName());
wrapper.add("data", context.serialize(object));
return wrapper;
}

public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException {
final JsonObject wrapper = (JsonObject) elem;
final JsonElement typeName = get(wrapper, "type");
final Type actualType = typeForName(typeName);
return new Gson().fromJson(elem, actualType);
}

private Type typeForName(final JsonElement typeElem) {
try {
String className = typeElem.getAsString();
return Class.forName("com.drivemode.spotify.models."+className.substring(0,1).toUpperCase() + className.substring(1));
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}

private JsonElement get(final JsonObject wrapper, String memberName) {
final JsonElement elem = wrapper.get(memberName);
if (elem == null) throw new JsonParseException("no '" + memberName + "' member found in what was expected to be an interface wrapper");
return elem;
}
}
}
Loading

0 comments on commit 1e4341c

Please sign in to comment.