2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.clients;
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import androidx.annotation.NonNull;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.interfaces.ApiInterface;
|
2019-11-13 16:52:49 +01:00
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import okhttp3.Cache;
|
|
|
|
import okhttp3.Interceptor;
|
2019-06-03 23:25:05 +02:00
|
|
|
import okhttp3.OkHttpClient;
|
2019-11-13 16:52:49 +01:00
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
2019-06-03 23:25:05 +02:00
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RetrofitClient {
|
|
|
|
|
|
|
|
private Retrofit retrofit;
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
private RetrofitClient(String instanceUrl, Context ctx) {
|
|
|
|
|
|
|
|
final boolean connToInternet = AppUtil.haveNetworkConnection(ctx);
|
2019-11-24 13:42:57 +01:00
|
|
|
int cacheSize = 50 * 1024 * 1024; // 50MB
|
2019-11-13 16:52:49 +01:00
|
|
|
File httpCacheDirectory = new File(ctx.getCacheDir(), "responses");
|
|
|
|
Cache cache = new Cache(httpCacheDirectory, cacheSize);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
|
|
|
|
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
|
|
|
.cache(cache)
|
|
|
|
//.addInterceptor(logging)
|
|
|
|
.addInterceptor(new Interceptor() {
|
|
|
|
@NonNull
|
|
|
|
@Override public Response intercept(@NonNull Chain chain) throws IOException {
|
|
|
|
Request request = chain.request();
|
|
|
|
if (connToInternet) {
|
|
|
|
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build();
|
|
|
|
} else {
|
|
|
|
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 30).build();
|
|
|
|
}
|
|
|
|
return chain.proceed(request);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
Retrofit.Builder builder = new Retrofit.Builder()
|
2019-06-03 23:25:05 +02:00
|
|
|
.baseUrl(instanceUrl)
|
2019-11-13 16:52:49 +01:00
|
|
|
.client(okHttpClient)
|
2019-06-03 23:25:05 +02:00
|
|
|
.addConverterFactory(ScalarsConverterFactory.create())
|
2019-11-13 16:52:49 +01:00
|
|
|
.addConverterFactory(GsonConverterFactory.create());
|
|
|
|
|
|
|
|
retrofit = builder.build();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
public static synchronized RetrofitClient getInstance(String instanceUrl, Context ctx) {
|
|
|
|
return new RetrofitClient(instanceUrl, ctx);
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ApiInterface getApiInterface() {
|
|
|
|
return retrofit.create(ApiInterface.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|