package com.nutrition.express.model.rest;

import android.content.Context;

import com.nutrition.express.application.Constants;
import com.nutrition.express.model.rest.ApiService.BlogService;
import com.nutrition.express.model.rest.ApiService.UserService;
import com.nutrition.express.model.rest.intercept.OAuth1SigningInterceptor;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by huang on 2/17/16.
 */
public class RestClient {
    private Retrofit retrofit;
    private OkHttpClient okHttpClient;

    private static class Holder {
        private static RestClient holder = new RestClient();
    }

    public static RestClient getInstance() {
        return Holder.holder;
    }

    private RestClient() {
    }

    public void init(Context context) {
        Cache cache = new Cache(context.getCacheDir(), 10 * 1024 * 1024);
        HttpLoggingInterceptor.Logger logger = new  HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (message.startsWith("{") || message.startsWith("[")) {
                    Logger.json(message);
                } else {
                    Logger.log(Logger.DEBUG, "okhttp", message, null);
                }
            }
        };
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new OAuth1SigningInterceptor())
                .addInterceptor(interceptor)
                .cache(cache)
                .readTimeout(240, TimeUnit.SECONDS)
                .writeTimeout(240, TimeUnit.SECONDS)
                .build();

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

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public BlogService getBlogService() {
        return retrofit.create(BlogService.class);
    }

    public UserService getUserService() {
        return retrofit.create(UserService.class);
    }
}

@@ -1809,34 +1809,33 @@
    .readTimeout(
-24
+6
 0, TimeUnit.SECO
@@ -1869,18 +1869,17 @@
 Timeout(
-24
+6
 0, TimeU

set timeout for okhttp