Merge branch 'master' of s416084/find-my-tutor-android into develop

This commit is contained in:
Mieszko Wrzeszczyński 2018-12-04 20:37:06 +00:00 committed by Gogs
commit 15529eccb4

View File

@ -7,67 +7,80 @@ import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.Interceptor; import okhttp3.Interceptor;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.TlsVersion;
import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient { public class ApiClient {
private static Retrofit retrofit = null; private static Retrofit retrofit = null;
private static int REQUEST_TIMEOUT = 60; private static int REQUEST_TIMEOUT = 60;
private static OkHttpClient okHttpClient; private static OkHttpClient okHttpClient;
private static final String BASE_URL = "https://s416084.projektstudencki.pl/master/"; private static final String BASE_URL = "https://s416084.projektstudencki.pl/master/";
public static Retrofit getClient(Context context) { public static Retrofit getClient(Context context) {
if (okHttpClient == null) if (okHttpClient == null)
initOkHttp(context); initOkHttp(context);
if (retrofit == null) { if (retrofit == null) {
retrofit = new Retrofit.Builder() retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL) .baseUrl(BASE_URL)
.client(okHttpClient) .client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build(); .build();
}
return retrofit;
}
private static void initOkHttp(final Context context) {
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
// Android 7.0.0 fix: https://stackoverflow.com/questions/39133437/sslhandshakeexception-handshake-failed-on-android-n-7-0?fbclid=IwAR1FpUjQlE_iP_2hiZ3okHoFs-Ik4AilVcJaKDKs4FHNFIxn7wb-Uxb_WWY
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
httpClient.connectionSpecs(Collections.singletonList(spec));
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json");
// Adding Authorization token (API Key)
// Requests will be denied without API key
if (!TextUtils.isEmpty(PrefUtils.getApiKey(context))) {
requestBuilder.addHeader("Authorization", "Bearer " + PrefUtils.getApiKey(context));
} }
return retrofit;
}
private static void initOkHttp(final Context context) { Request request = requestBuilder.build();
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder() return chain.proceed(request);
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS) });
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); okHttpClient = httpClient.build();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); }
};
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json");
// Adding Authorization token (API Key)
// Requests will be denied without API key
if (!TextUtils.isEmpty(PrefUtils.getApiKey(context))) {
requestBuilder.addHeader("Authorization", "Bearer " + PrefUtils.getApiKey(context));
}
Request request = requestBuilder.build();
return chain.proceed(request);
});
okHttpClient = httpClient.build();
}
};