package com.quinn.githubknife.model;

import android.content.Context;

import com.quinn.githubknife.account.GitHubAccount;
import com.quinn.githubknife.utils.Constants;
import com.quinn.githubknife.utils.L;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;

import java.io.IOException;

import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by Quinn on 10/12/15.
 */
public class RetrofitUtil {

    private volatile static Retrofit instance;
    public static String token;
    private final static String TAG = RetrofitUtil.class.getSimpleName();


    // Returns singleton class instance
    public static Retrofit getInstance(final Context context) {
        if (instance == null) {
            synchronized (Retrofit.class) {
                if (instance == null) {
                    OkHttpClient client = new OkHttpClient();
                    client.networkInterceptors().add(new Interceptor() {
                        @Override
                        public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
                            Request request = chain.request();
                            GitHubAccount gitHubAccount = GitHubAccount.getInstance(context);

                            token = gitHubAccount.getAuthToken();
                            request = request.newBuilder()
                                    .removeHeader("User-Agent")
                                    .addHeader("Authorization", "Token " + token)
                                    .addHeader("User-Agent", "Leaking/1.0")
                                    //.addHeader("Accept", "application/vnd.github.beta+json")
                                    .addHeader("Accept", "application/vnd.github.VERSION.raw")
                                            .build();
                            //此处build之后要返回request覆盖
                            L.i(TAG, "Interceptor header = " + request.headers());
                            L.i(TAG, "Interceptor method = " + request.method());
                            L.i(TAG, "Interceptor urlString = " + request.urlString());
                            return chain.proceed(request);
                        }
                    });



                    instance = new Retrofit.Builder()
                            .baseUrl(Constants.BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create())
                             .client(client)
                            .build();

                }
            }
        }
        return instance;
    }



    public static void printResponse(Response response){
        L.i(TAG,"response headers " + response.headers());
        L.i(TAG,"response code " + response.code());
        L.i(TAG,"response isSuccess " + response.isSuccess());
        L.i(TAG,"response message " + response.message());
        L.i(TAG,"response body " + response.body());
    }

    public static void printThrowable(Throwable throwable){
        L.i(TAG,"response Throwable " + throwable.toString());

    }

}

@@ -60,24 +60,89 @@
 t.Context;%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A
 import com.q
@@ -265,16 +265,108 @@
 tils.L;%0A
+import com.quinn.httpknife.github.Event;%0Aimport com.quinn.httpknife.payload.EventFormatter;%0A
 import c
@@ -2478,17 +2478,250 @@
   %7D);%0A%0A%0A
+                    Gson gson = new Gson();%0A                    GsonBuilder builder = new GsonBuilder();%0A                    builder.registerTypeAdapter(Event.class, new EventFormatter());%0A                    gson = builder.create();
 %0A
-
         
@@ -2904,12 +2904,15 @@
 ate(
+gson
 ))%0A
- 
     

add JsonDeserializer for retrofit