package com.lzy.mmd.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * com.lzy.mmd.utils
 * Created by lizhaoyong
 * 2016/8/3.
 */
public class HttpManager {

    public static Retrofit retrofit;
    private static final int DEFAULT_TIMEOUT = 20;


    public static Retrofit getInstance(){
        if (retrofit == null){
            synchronized (HttpManager.class){
                if (retrofit == null){
                    retrofit = new Retrofit.Builder()
                            .baseUrl("https://api.github.com/")
                            .client(genericClient())
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .build();
                }
            }
        }
        return retrofit;
    }

    public static OkHttpClient genericClient(){
//        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        OkHttpClient client = new OkHttpClient.Builder()
//                .addNetworkInterceptor(httpLoggingInterceptor)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
//                .addInterceptor(new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request request = chain.request().newBuilder()
//                        .addHeader("","")//可以从这里添加请求头
//                        .build();
//                return chain.proceed(request);
//            }
//        })
                .build();
        return client;
    }
}
