0

Is there any way to do interception or authentication of request async? For example in flow like this: get password from user, use it to obtain token an then retry the call that was intercepted?

Dominik Mičuta
  • 357
  • 4
  • 13

1 Answers1

3

I have the same problem here.

I use wait()and notify() in the getToken method in my code, but because i use OkHttp in Retrofit library, it is creating a new thread and makes it possible to have async operations here.

    public synchronized void getToken() throws InterruptedException {
    if (!isRefreshing()) {
        //This is very important to call notify() on the same object that we call wait();
        final TokenProvider myInstance = this;
        setRefreshing(true);
        Log.d("refreshToken", "Refreshing token..." );

       //Make async call
       getRestClient().getAccountService().getRefreshedToken(mLoginData.getRefreshToken())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        synchronized (myInstance) {
                            setRefreshing(false);
                            Log.d("refreshToken", "notifyAll onComplete.");
                            myInstance.notifyAll();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        synchronized (myInstance) {
                            setRefreshing(false);
                            myInstance.notifyAll();
                            Log.d("refreshToken", "onError .");
                        }
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        synchronized (myInstance) {
                            mLoginData = loginResponse;
                            Log.d("refreshToken", "notifyAll onNext .");
                            myInstance.notifyAll();
                        }
                    }
                });
    }

        Log.d("refreshToken", "before wait ." + android.os.Process.getThreadPriority(android.os.Process.myTid()) + this.toString());
        this.wait();
        Log.d("refreshToken", "after wait ." + android.os.Process.getThreadPriority(android.os.Process.myTid()) + this.toString());
}

You can try if this works for you.

Community
  • 1
  • 1
Morteza Rastgoo
  • 6,772
  • 7
  • 40
  • 61