Search in sources :

Example 1 with Token

use of com.xinshang.audient.model.entities.Token in project Audient by komamj.

the class LoginPresenter method loadAccessToken.

@Override
public void loadAccessToken(String code) {
    mView.setLoadIndicator(true);
    Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {

        @Override
        public void accept(Token token) throws Exception {
            mRepository.persistenceAccessToken(token.accessToken);
            mRepository.persistenceRefreshToken(token.refreshToken);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {

        @Override
        public void onNext(Token token) {
        }

        @Override
        public void onError(Throwable t) {
            LogUtils.e(TAG, "loadAccessToken error :" + t.toString());
            mRepository.persistenceLoginStatus(false);
            if (mView.isActive()) {
                mView.setLoadIndicator(false);
                mView.showLoadingError();
            }
        }

        @Override
        public void onComplete() {
            mRepository.persistenceLoginStatus(true);
            if (mView.isActive()) {
                mView.setLoadIndicator(false);
                mView.showSuccessfulMessage();
            }
        }
    });
    mDisposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Consumer(io.reactivex.functions.Consumer) Token(com.xinshang.audient.model.entities.Token)

Example 2 with Token

use of com.xinshang.audient.model.entities.Token in project Audient by komamj.

the class SplashPresenter method loadAccessToken.

@Override
public void loadAccessToken(String code) {
    Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {

        @Override
        public void accept(Token token) throws Exception {
            mRepository.persistenceAccessToken(token.accessToken);
            mRepository.persistenceRefreshToken(token.refreshToken);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {

        @Override
        public void onNext(Token token) {
            if (mView.isActive()) {
                mView.showStoresUI(true);
                loadStores();
            }
        }

        @Override
        public void onError(Throwable t) {
            LogUtils.e(TAG, "loadAccessToken error :" + t.toString());
            mRepository.persistenceLoginStatus(false);
            if (mView.isActive()) {
                mView.showLoadingError();
            }
        }

        @Override
        public void onComplete() {
            if (mView.isActive()) {
                mView.showSuccessfulMessage();
            }
        }
    });
    mDisposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Consumer(io.reactivex.functions.Consumer) Token(com.xinshang.audient.model.entities.Token)

Example 3 with Token

use of com.xinshang.audient.model.entities.Token in project Audient by komamj.

the class WXEntryPresenter method loadAccessToken.

@Override
public void loadAccessToken(String code) {
    mView.setLoadIndicator(true);
    Disposable disposable = mRepository.getAccessToken(code).doOnNext(new Consumer<Token>() {

        @Override
        public void accept(Token token) throws Exception {
            mRepository.persistenceAccessToken(token.accessToken);
            mRepository.persistenceRefreshToken(token.refreshToken);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSubscriber<Token>() {

        @Override
        public void onNext(Token token) {
        }

        @Override
        public void onError(Throwable t) {
            mRepository.persistenceLoginStatus(false);
            if (mView.isActive()) {
                mView.setLoadIndicator(false);
                mView.showLoadingError();
            }
        }

        @Override
        public void onComplete() {
            mRepository.persistenceLoginStatus(true);
            if (mView.isActive()) {
                mView.setLoadIndicator(false);
                mView.showSuccessfulMessage();
            }
        }
    });
    mDisposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Consumer(io.reactivex.functions.Consumer) Token(com.xinshang.audient.model.entities.Token)

Example 4 with Token

use of com.xinshang.audient.model.entities.Token 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

Token (com.xinshang.audient.model.entities.Token)4 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 Disposable (io.reactivex.disposables.Disposable)3 Consumer (io.reactivex.functions.Consumer)3 Nullable (android.support.annotation.Nullable)1 GsonBuilder (com.google.gson.GsonBuilder)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