package com.etiennelawlor.loop.network; import com.etiennelawlor.loop.BuildConfig; import com.etiennelawlor.loop.LoopApplication; import java.io.File; import okhttp3.Cache; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import timber.log.Timber; /** * Created by etiennelawlor on 6/14/15. */ public class ServiceGenerator { // region Constants private static final int DISK_CACHE_SIZE = 10 * 1024 * 1024; // 10MB // endregion private static Retrofit.Builder retrofitBuilder = new Retrofit.Builder() .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); private static OkHttpClient defaultOkHttpClient = new OkHttpClient.Builder() .cache(getCache()) .build(); // No need to instantiate this class. private ServiceGenerator() { } public static <S> S createService(Class<S> serviceClass, String baseUrl) { return createService(serviceClass, baseUrl, null); } public static <S> S createService(Class<S> serviceClass, String baseUrl, Interceptor networkInterceptor) { OkHttpClient.Builder okHttpClientBuilder = defaultOkHttpClient.newBuilder(); if(networkInterceptor != null){ okHttpClientBuilder.addNetworkInterceptor(networkInterceptor); } OkHttpClient modifiedOkHttpClient = okHttpClientBuilder .addInterceptor(getHttpLoggingInterceptor()) .build(); retrofitBuilder.client(modifiedOkHttpClient); retrofitBuilder.baseUrl(baseUrl); Retrofit retrofit = retrofitBuilder.build(); return retrofit.create(serviceClass); } private static Cache getCache() { Cache cache = null; // Install an HTTP cache in the application cache directory. try { File cacheDir = new File(LoopApplication.getCacheDirectory(), "http"); cache = new Cache(cacheDir, DISK_CACHE_SIZE); } catch (Exception e) { Timber.e(e, "Unable to install disk cache."); } return cache; } private static HttpLoggingInterceptor getHttpLoggingInterceptor(){ HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); if (BuildConfig.DEBUG) { httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); } else { httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE); } return httpLoggingInterceptor; } }