android - Managing Auth Token using RxJava -
i've been wondering what's best way tackle issue of token refresh. i'm connecting api supplies me auth-token, if sometime time during calls invalid_auth need re-authenticate. naive implementation did this
@suppresswarnings("unchecked") @override public observable<user> getusers() { return runcommandandrefreshauthifnecessary(new requestcommand() { @override public observable create() { return createservice(usersapi.class).getusers(); } }); } private observable runcommandandrefreshauthifnecessary(final requestcommand command) { return command.create() .onerrorresumenext(new func1<throwable, observable<?>>() { @override public observable<?> call(throwable throwable) { return handlerefreshtoken(command); } }); } private observable<?> handlerefreshtoken(final requestcommand command) { return refreshtoken().flatmap(new func1<boolean, observable<?>>() { @override public observable<?> call(boolean aboolean) { return command.create(); } }); }
as can see i'm wrapping retrofit command, if error run refreshtoken(), token refreshes , run retrofit command again, observable passed subscriber. works expected. thing i'm struggling with, happens multiple calls made, example, i'm calling getusers , getflags 1 after another. both of them invalid_auth, both of fire refreshtoken(), bad. i'm looking rx-java way manage calls, meaning after first call of getusers fires refreshtoken, call after needs wait refreshtoken end, fire retrofit command.
any suggestion appreciated.
you can use .cache()
on observable
token refreshing: http://reactivex.io/documentation/operators/replay.html
Comments
Post a Comment