use of rxreddit.util.RxRedditUtil in project RxReddit by damien5314.
the class RedditAuthService method refreshUserAccessToken.
@Override
public Observable<UserAccessToken> refreshUserAccessToken() {
return Observable.defer(() -> {
UserAccessToken token = getUserAccessToken();
if (token == null) {
return Observable.error(new IllegalStateException("No user access token available"));
}
if (token.secondsUntilExpiration() > EXPIRATION_THRESHOLD) {
return Observable.just(token);
}
String refreshToken = token.getRefreshToken();
if (refreshToken == null) {
clearUserAccessToken();
return Observable.error(new IllegalStateException("No refresh token available"));
}
String grantType = "refresh_token";
return authService.refreshUserAuthToken(grantType, refreshToken).flatMap(RxRedditUtil::responseToBody).doOnNext(this::saveUserAccessToken).doOnError(error -> {
if (error instanceof HttpException && ((HttpException) error).code() == 403) {
// 403 means our refresh token is no longer good, just discard it
clearUserAccessToken();
}
});
});
}
use of rxreddit.util.RxRedditUtil in project RxReddit by damien5314.
the class RedditService method updateUserSettings.
@Override
public Completable updateUserSettings(Map<String, String> settings) {
String json = new Gson().toJson(settings);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
return requireUserAccessToken().flatMap(token -> api.updateUserSettings(body).flatMap(RxRedditUtil::checkResponse)).ignoreElements();
}
Aggregations