class CognitoIdentityProvider {

    @NonNull
    public Single<Optional<Cognito>> refreshCognitoToken() {
        return this.identityManager.getMe()
                .doOnSubscribe(disposable -> {
                    Logger.debug(CognitoIdentityProvider.this, "Clearing the cached cognito token to refresh");
                    localCognitoTokenStore.persist(null);
                })
                .<Optional<Cognito>>map(meResponse -> {
                    if (meResponse != null && meResponse.getUser() != null) {
                        Logger.debug(CognitoIdentityProvider.this, "Retrieve a valid token response");
                        final User user = meResponse.getUser();
                        return Optional.of(new Cognito(user.getCognitoToken(), user.getIdentityId(), user.getCognitoTokenExpiresAt()));
                    } else {
                        Logger.warn(CognitoIdentityProvider.this, "Failed to fetch a valid token");
                        return Optional.absent();
                    }
                })
                .doOnNext(optionalCognito -> {
                    if (optionalCognito.isPresent()) {
                        localCognitoTokenStore.persist(optionalCognito.get());
                    }else {
                        localCognitoTokenStore.persist(null);
                    }
                })
                .singleOrError();
    }

    @Inject public  CognitoIdentityProvider(@NonNull IdentityManager identityManager,
                                   @NonNull LocalCognitoTokenStore localCognitoTokenStore);

}