package kaaes.spotify.webapi.android;

import com.squareup.okhttp.OkHttpClient;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;

/**
 * Creates and configures a REST adapter for Spotify Web API.
 *
 * Basic usage:
 * SpotifyApi wrapper = new SpotifyApi();
 *
 * Setting access token is optional for certain endpoints
 * so if you know you'll only use the ones that don't require authorisation
 * you can skip this step:
 * wrapper.setAccessToken(authenticationResponse.getAccessToken());
 *
 * SpotifyService spotify = wrapper.getService();
 *
 * Album album = spotify.getAlbum("2dIGnmEIy1WZIcZCFSj6i8");
 */
public class SpotifyApi {

    /**
     * Main Spotify Web API endpoint
     */
    public static final String SPOTIFY_WEB_API_ENDPOINT = "https://api.spotify.com/v1";

    /**
     * The request interceptor that will add the header with OAuth
     * token to every request made with the wrapper.
     */
    private class WebApiAuthenticator implements RequestInterceptor {
        @Override
        public void intercept(RequestFacade request) {
            if (mAccessToken != null) {
                request.addHeader("Authorization", "Bearer " + mAccessToken);
            }
        }
    }

    private final SpotifyService mSpotifyService;

    private String mAccessToken;

    public SpotifyApi() {
        Executor executor = Executors.newSingleThreadExecutor();
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.BASIC)
                .setClient(new OkClient(new OkHttpClient()))
                .setExecutors(executor, executor)
                .setEndpoint(SPOTIFY_WEB_API_ENDPOINT)
                .setRequestInterceptor(new WebApiAuthenticator())
                .build();

        mSpotifyService = restAdapter.create(SpotifyService.class);
    }

    /**
     * Sets access token on the wrapper.
     * Use to set or update token with the new value.
     * If you want to remove token set it to null.
     *
     * @param accessToken The token to set on the wrapper.
     * @return The instance of the wrapper.
     */
    public SpotifyApi setAccessToken(String accessToken) {
        mAccessToken = accessToken;
        return this;
    }

    /**
     * @return The SpotifyApi instance
     */
    public SpotifyService getService() {
        return mSpotifyService;
    }
}

@@ -1421,38 +1421,96 @@
     
-public SpotifyApi() %7B
+/**%0A     * Create instance of SpotifyApi with given executors.
 %0A    
+ *%0A
     
+ * @param http
 Exec
@@ -1527,54 +1527,453 @@
 tor 
-= Executors.newSingleThreadExecutor();%0A       
+for http request. Cannot be null.%0A     * @param callbackExecutor executor for callbacks. If null is passed than the same%0A     *                         thread that created the instance is used.%0A     */%0A    public SpotifyApi(Executor httpExecutor, Executor callbackExecutor) %7B%0A        mSpotifyService = init(httpExecutor, callbackExecutor);%0A    %7D%0A%0A    private SpotifyService init(Executor httpExecutor, Executor callbackExecutor) %7B%0A%0A        final
  Res
@@ -2169,17 +2169,21 @@
 ecutors(
-e
+httpE
 xecutor,
@@ -2183,17 +2183,25 @@
 ecutor, 
-e
+callbackE
 xecutor)
@@ -2361,65 +2361,328 @@
     
-mSpotifyService = restAdapter.create(SpotifyService.class
+ return restAdapter.create(SpotifyService.class);%0A    %7D%0A%0A    /**%0A     *  New instance of SpotifyApi,%0A     *  with single thread executor both for http and callbacks.%0A     */%0A    public SpotifyApi() %7B%0A        final Executor executor = Executors.newSingleThreadExecutor();%0A        mSpotifyService = init(executor, executor
 );%0A 

add possibility to use custom Executors for http and callbacks.