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;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.interfaces.ApiInterface;
|
2020-03-30 23:01:54 +02:00
|
|
|
import org.mian.gitnex.ssl.MemorizingTrustManager;
|
2019-11-13 16:52:49 +01:00
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import java.io.File;
|
2020-03-30 23:01:54 +02:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
2019-11-13 16:52:49 +01:00
|
|
|
import okhttp3.Cache;
|
2019-06-03 23:25:05 +02:00
|
|
|
import okhttp3.OkHttpClient;
|
2019-11-13 16:52:49 +01:00
|
|
|
import okhttp3.Request;
|
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;
|
|
|
|
|
2020-03-30 23:01:54 +02:00
|
|
|
private RetrofitClient(String instanceUrl, Context context) {
|
|
|
|
final boolean connToInternet = AppUtil.haveNetworkConnection(context);
|
2019-11-24 13:42:57 +01:00
|
|
|
int cacheSize = 50 * 1024 * 1024; // 50MB
|
2020-03-30 23:01:54 +02:00
|
|
|
File httpCacheDirectory = new File(context.getCacheDir(), "responses");
|
2019-11-13 16:52:49 +01:00
|
|
|
Cache cache = new Cache(httpCacheDirectory, cacheSize);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
|
|
|
|
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
|
|
|
|
|
2020-03-30 23:01:54 +02:00
|
|
|
try {
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
|
|
|
|
MemorizingTrustManager memorizingTrustManager = new MemorizingTrustManager(context);
|
|
|
|
sslContext.init(null, new X509TrustManager[] { memorizingTrustManager }, new SecureRandom());
|
|
|
|
|
|
|
|
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder()
|
|
|
|
.cache(cache)
|
|
|
|
.sslSocketFactory(sslContext.getSocketFactory(), memorizingTrustManager)
|
|
|
|
.hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()))
|
|
|
|
.addInterceptor(chain -> {
|
2019-11-13 16:52:49 +01:00
|
|
|
Request request = chain.request();
|
2020-03-30 23:01:54 +02:00
|
|
|
|
|
|
|
request = (connToInternet) ?
|
|
|
|
request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build() :
|
|
|
|
request.newBuilder().header("Cache-Control",
|
|
|
|
"public, only-if-cached, max-stale=" + 60 * 60 * 24 * 30).build();
|
|
|
|
|
2019-11-13 16:52:49 +01:00
|
|
|
return chain.proceed(request);
|
2020-03-30 23:01:54 +02:00
|
|
|
});
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-03-30 23:01:54 +02:00
|
|
|
Retrofit.Builder builder = new Retrofit.Builder()
|
|
|
|
.baseUrl(instanceUrl)
|
|
|
|
.client(okHttpClient.build())
|
|
|
|
.addConverterFactory(ScalarsConverterFactory.create())
|
|
|
|
.addConverterFactory(GsonConverterFactory.create());
|
2019-11-13 16:52:49 +01:00
|
|
|
|
2020-03-30 23:01:54 +02:00
|
|
|
retrofit = builder.build();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-03-30 23:01:54 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|