use of com.xinshang.audient.model.helper.TokenInterceptor in project Audient by komamj.
the class AudientRepositoryModule method provideOkHttpClient.
@Singleton
@Provides
OkHttpClient provideOkHttpClient(Cache cache, final SharedPreferences sharedPreferences, final Gson gson) {
final HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
}
return new OkHttpClient.Builder().authenticator(new Authenticator() {
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
String refershToken = sharedPreferences.getString(Constants.REFRESH_TOKEN, "");
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.AUDIENT_HOST).client(new OkHttpClient.Builder().addInterceptor(logInterceptor).build()).addConverterFactory(GsonConverterFactory.create(gson)).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
AudientApi audientApi = retrofit.create(AudientApi.class);
audientApi.refreshAccessToken(GRANT_TYPE, refershToken, Constants.CLIENT_ID, Constants.CLIENT_SECRET).subscribeWith(new DisposableSubscriber<Token>() {
@Override
public void onNext(Token token) {
sharedPreferences.edit().putString(Constants.ACCESS_TOKEN, token.accessToken).commit();
sharedPreferences.edit().putString(Constants.REFRESH_TOKEN, token.refreshToken).commit();
}
@Override
public void onError(Throwable t) {
LogUtils.e(TAG, "refresh token error :" + t.getMessage());
sharedPreferences.edit().putBoolean(Constants.LOGIN_STATUS, false).commit();
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
public void onComplete() {
}
});
return response.request().newBuilder().header("Authorization", "Bearer " + sharedPreferences.getString(Constants.ACCESS_TOKEN, "")).build();
}
}).addInterceptor(new TokenInterceptor(sharedPreferences)).addInterceptor(logInterceptor).cache(cache).connectTimeout(15, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).retryOnConnectionFailure(true).build();
}
Aggregations