package com.prt2121.githubsdk.client;

import android.content.Context;
import com.squareup.okhttp.OkHttpClient;
import retrofit.Converter;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by pt2121 on 9/23/15.
 */
public abstract class BaseClient<T> {

  private static final String BASE_URL = "https://api.github.com";
  protected Context context;
  private CredentialStorage credentialStorage;

  public BaseClient(Context context) {
    this.context = context.getApplicationContext();
  }

  protected Retrofit getRetrofit() {
    Retrofit.Builder builder =
        new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .baseUrl(BASE_URL);

    if (customConverter() != null) {
      builder.addConverterFactory(customConverter());
    }

    if (getClient() != null) {
      builder.client(getClient());
    }

    return builder.build();
  }

  protected Converter.Factory customConverter() {
    return null;
  }

  protected OkHttpClient getClient() {
    return null;
  }

  protected abstract Observable<T> execute();

  protected <T1> Observable.Transformer<T1, T1> applySchedulers() {
    return observable -> observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());
  }
}

@@ -75,39 +75,106 @@
 ort 
-com.squareup.okhttp.OkHttpClien
+android.text.TextUtils;%0Aimport com.squareup.okhttp.OkHttpClient;%0Aimport com.squareup.okhttp.Reques
 t;%0Ai
@@ -409,16 +409,56 @@
 ulers;%0A%0A
+import static java.lang.String.format;%0A%0A
 /**%0A * C
@@ -657,26 +657,109 @@
 age 
-credentialStorage;
+storage;%0A  private String token;%0A%0A  public BaseClient(Context context) %7B%0A    this(context, null);%0A  %7D
 %0A%0A  
@@ -783,32 +783,46 @@
 (Context context
+, String token
 ) %7B%0A    this.con
@@ -857,24 +857,79 @@
 nContext();%0A
+    this.token = token;%0A    storage.storeToken(token);%0A
   %7D%0A%0A  prote
@@ -1174,16 +1174,403 @@
 _URL);%0A%0A
+    if (!TextUtils.isEmpty(storage.token())) %7B%0A      OkHttpClient client = new OkHttpClient();%0A      client.interceptors().add(chain -%3E %7B%0A        Request request = chain.request();%0A        Request newReq =%0A            request.newBuilder().addHeader(%22Authorization%22, format(%22token %25s%22, token)).build();%0A        return chain.proceed(newReq);%0A      %7D);%0A      builder.client(client);%0A    %7D%0A%0A
     if (

Add token to client