Search in sources :

Example 1 with TokenInterceptor

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) Token(com.xinshang.audient.model.entities.Token) IOException(java.io.IOException) DisposableSubscriber(io.reactivex.subscribers.DisposableSubscriber) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Authenticator(okhttp3.Authenticator) Nullable(android.support.annotation.Nullable) Route(okhttp3.Route) TokenInterceptor(com.xinshang.audient.model.helper.TokenInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

Nullable (android.support.annotation.Nullable)1 GsonBuilder (com.google.gson.GsonBuilder)1 Token (com.xinshang.audient.model.entities.Token)1 TokenInterceptor (com.xinshang.audient.model.helper.TokenInterceptor)1 Provides (dagger.Provides)1 DisposableSubscriber (io.reactivex.subscribers.DisposableSubscriber)1 IOException (java.io.IOException)1 Singleton (javax.inject.Singleton)1 Authenticator (okhttp3.Authenticator)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 Route (okhttp3.Route)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 Retrofit (retrofit2.Retrofit)1