1

I am new to RxJava.

We are using Retrofit2 + RxJava2 for calling APIs.

I have the following scenario -

1. Call Cart API
2. If Cart API fails I need to call Login API
3. On Success of Login API I need to call Cart API again

Call {API} -> {API} fails -> call Login API -> on success of Login API -> call {API}.

Like this way, if any API fails I need to call Login API and then call the failed API.

Whats the best way handle this.

1
  • Sounds like you want to refresh a login token/session. If that's the case maybe it's worth looking into the OkHttp authenticator api or if you can't use it, then maybe use interceptors. Commented Nov 28, 2017 at 9:21

1 Answer 1

2

You could do something like this:

api.processCart()
    .retryWhen(errors -> errors
                           .filter(it -> it == LOGIN_ERROR)
                           .flatMap(api.login())

Beware that this could create a never-ending loop if you are not careful. You can insert any logic you want in the retryWhen handler. As in the example, you could check the type of the error and decide if you need to relogin or do something else.

But if you need to do this for every api call, it'd be worth taking a look at Fred's comment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.