Search in sources :

Example 1 with RxRedditUtil

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();
            }
        });
    });
}
Also used : UserAccessToken(rxreddit.model.UserAccessToken) HttpException(retrofit2.HttpException) RxRedditUtil(rxreddit.util.RxRedditUtil)

Example 2 with RxRedditUtil

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();
}
Also used : Subreddit(rxreddit.model.Subreddit) Cache(okhttp3.Cache) UserSettings(rxreddit.model.UserSettings) UserIdentity(rxreddit.model.UserIdentity) Completable(io.reactivex.Completable) GsonBuilder(com.google.gson.GsonBuilder) RxJava2CallAdapterFactory(retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory) RequestBody(okhttp3.RequestBody) Listing(rxreddit.model.Listing) Gson(com.google.gson.Gson) AbsComment(rxreddit.model.AbsComment) Map(java.util.Map) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Observable(io.reactivex.Observable) SubredditSidebar(rxreddit.model.SubredditSidebar) MediaType(okhttp3.MediaType) UserAccessToken(rxreddit.model.UserAccessToken) SubmitPostResponse(rxreddit.model.SubmitPostResponse) Interceptor(okhttp3.Interceptor) Request(okhttp3.Request) RxRedditUtil(rxreddit.util.RxRedditUtil) MoreChildrenResponse(rxreddit.model.MoreChildrenResponse) ModReport(rxreddit.model.ModReport) Friend(rxreddit.model.Friend) File(java.io.File) Retrofit(retrofit2.Retrofit) ReportForm(rxreddit.model.ReportForm) List(java.util.List) ListingResponse(rxreddit.model.ListingResponse) OkHttpClient(okhttp3.OkHttpClient) AccessToken(rxreddit.model.AccessToken) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) UserReport(rxreddit.model.UserReport) SubredditRules(rxreddit.model.SubredditRules) Comment(rxreddit.model.Comment) FriendInfo(rxreddit.model.FriendInfo) UserIdentityListing(rxreddit.model.UserIdentityListing) Gson(com.google.gson.Gson) RxRedditUtil(rxreddit.util.RxRedditUtil) RequestBody(okhttp3.RequestBody)

Aggregations

UserAccessToken (rxreddit.model.UserAccessToken)2 RxRedditUtil (rxreddit.util.RxRedditUtil)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Completable (io.reactivex.Completable)1 Observable (io.reactivex.Observable)1 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 Cache (okhttp3.Cache)1 Interceptor (okhttp3.Interceptor)1 MediaType (okhttp3.MediaType)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 HttpException (retrofit2.HttpException)1 Retrofit (retrofit2.Retrofit)1 RxJava2CallAdapterFactory (retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory)1 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)1