Search in sources :

Example 1 with RateLimiter

use of com.github.natanbc.reliqua.limiter.RateLimiter in project Ree6 by Ree6-Applications.

the class PendingRequest method async.

/**
 * Execute this request asynchronously, calling the appropriate callback when it's done.
 *
 * @param onSuccess Called when this request completes successfully.
 * @param onError Called when there's an error executing the request or parsing the response.
 */
public void async(@Nullable Consumer<T> onSuccess, @Nullable Consumer<RequestException> onError) {
    StackTraceElement[] callSite = api.isTrackingCallSites() ? Thread.currentThread().getStackTrace() : null;
    if (onSuccess == null)
        onSuccess = v -> {
        };
    if (onError == null)
        onError = Throwable::printStackTrace;
    Consumer<T> finalOnSuccess = onSuccess;
    Consumer<RequestException> finalOnError = onError;
    Runnable r = () -> api.getClient().newCall(httpRequest).enqueue(new Callback() {

        @Override
        public void onFailure(@Nonnull Call call, @Nonnull IOException e) {
            finalOnError.accept(new RequestException(e, callSite));
        }

        @Override
        public void onResponse(@Nonnull Call call, @Nonnull Response response) {
            try {
                ResponseBody body = response.body();
                if (!statusCodeValidator.test(response.code())) {
                    try {
                        onError(new RequestContext<>(callSite, finalOnSuccess, finalOnError, response));
                    } finally {
                        if (body != null) {
                            body.close();
                        }
                    }
                    return;
                }
                try {
                    finalOnSuccess.accept(onSuccess(response));
                } finally {
                    if (body != null) {
                        body.close();
                    }
                }
            } catch (RequestException e) {
                finalOnError.accept(e);
            } catch (Exception e) {
                finalOnError.accept(new RequestException(e, callSite));
            }
        }
    });
    if (rateLimiter == null) {
        r.run();
    } else {
        rateLimiter.queue(r);
    }
}
Also used : Request(okhttp3.Request) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) IntPredicate(java.util.function.IntPredicate) ArrayList(java.util.ArrayList) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) CheckReturnValue(javax.annotation.CheckReturnValue) Reliqua(com.github.natanbc.reliqua.Reliqua) List(java.util.List) Future(java.util.concurrent.Future) CompletionStage(java.util.concurrent.CompletionStage) StatusCodeValidator(com.github.natanbc.reliqua.util.StatusCodeValidator) Response(okhttp3.Response) RateLimiter(com.github.natanbc.reliqua.limiter.RateLimiter) Call(okhttp3.Call) Callback(okhttp3.Callback) Nonnull(javax.annotation.Nonnull) Collections(java.util.Collections) ResponseBody(okhttp3.ResponseBody) Nullable(javax.annotation.Nullable) Call(okhttp3.Call) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback)

Aggregations

Reliqua (com.github.natanbc.reliqua.Reliqua)1 RateLimiter (com.github.natanbc.reliqua.limiter.RateLimiter)1 StatusCodeValidator (com.github.natanbc.reliqua.util.StatusCodeValidator)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 Consumer (java.util.function.Consumer)1 IntPredicate (java.util.function.IntPredicate)1 CheckReturnValue (javax.annotation.CheckReturnValue)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 Request (okhttp3.Request)1