package com.alorma.gitskarios.core.client;

import com.alorma.gitskarios.core.ApiClient;

import java.util.concurrent.TimeUnit;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Client;
import retrofit.converter.Converter;
import rx.Observable;
import rx.functions.Func2;

public abstract class BaseClient<K> implements RequestInterceptor, RestAdapter.Log {

    private ApiClient client;

    public BaseClient(ApiClient client) {
        this.client = client;
    }

    protected RestAdapter getRestAdapter() {
        RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder().setEndpoint(client.getApiEndpoint())
                .setRequestInterceptor(this)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setLog(this);

        if (customConverter() != null) {
            restAdapterBuilder.setConverter(customConverter());
        }

        if (getInterceptor() != null) {
            restAdapterBuilder.setClient(getInterceptor());
        }

        return restAdapterBuilder.build();
    }

    protected Client getInterceptor() {
        return null;
    }

    public Observable<K> observable() {
        return getApiObservable(getRestAdapter()).retry(new Func2<Integer, Throwable, Boolean>() {
            @Override
            public Boolean call(Integer integer, Throwable throwable) {
                if (throwable instanceof RetrofitError) {
                    return ((RetrofitError) throwable).getResponse().getStatus() == 202 && integer < 3;
                }
                return integer < 3;
            }
        }).debounce(100, TimeUnit.MILLISECONDS);
    }

    protected abstract Observable<K> getApiObservable(RestAdapter restAdapter);

    protected Converter customConverter() {
        return null;
    }

    protected String getToken() {
        if (TokenProvider.getInstance() != null) {
            return TokenProvider.getInstance().getToken();
        }
        return "";
    }

    public ApiClient getClient() {
        return client;
    }
}

@@ -249,16 +249,49 @@
 Client;%0A
+import retrofit.client.Response;%0A
 import r
@@ -684,16 +684,29 @@
 uilder()
+%0A            
 .setEndp
@@ -1313,27 +1313,18 @@
 try(
-new Func2%3CI
+(i
 nteger, 
 Thro
@@ -1319,25 +1319,25 @@
 nteger, 
-T
+t
 hrowable
 , Boolea
@@ -1332,20 +1332,12 @@
 able
-, Boolean%3E()
+) -%3E
  %7B%0A 
@@ -1351,216 +1351,204 @@
     
-@Override%0A            public Boolean call(Integer integer, Throwable throwable) %7B%0A                if (throwable instanceof RetrofitError) %7B%0A                    return ((RetrofitError) throwable).getR
+if (throwable instanceof RetrofitError) %7B%0A                Response response = ((RetrofitError) throwable).getResponse();%0A                if (response != null) %7B%0A                    return r
 esponse
-()
 .get
@@ -1597,24 +1597,34 @@
       %7D%0A    
+        %7D%0A
             
@@ -1643,30 +1643,16 @@
 er %3C 3;%0A
-            %7D%0A
         

fix gitskarios/Gitskarios #351