Skip to content

Commit

Permalink
issue #491 allow the async client to deal with retries
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Oct 8, 2023
1 parent 72b830e commit 65ec51f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Empty file removed unirest-bdd-tests/results.json
Empty file.
25 changes: 25 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/RetryAsyncTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package BehaviorTests;

import kong.unirest.core.*;
Expand Down
26 changes: 24 additions & 2 deletions unirest/src/main/java/kong/unirest/core/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import java.io.File;
import java.nio.file.CopyOption;
import java.time.Instant;
import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -355,7 +358,26 @@ private <T> CompletableFuture<HttpResponse<T>> requestAsync(HttpRequest request,
Function<RawResponse, HttpResponse<T>> transformer,
CompletableFuture<HttpResponse<T>> callback,
Class<?> resultType){
return config.getClient().request(request, transformer, callback, resultType);
var asyncR = config.getClient().request(request, transformer, callback, resultType);
if(config.isAutomaticRetryAfter()){
return asyncR.thenApplyAsync(response -> {
callCount++;
var retryAfter = config.getRetryStrategy();
if(retryAfter.isRetryable(response) && callCount < config.maxRetries()) {
long waitTime = retryAfter.getWaitTime(response);
if (waitTime > 0) {
retryAfter.waitFor(waitTime);
try {
return requestAsync(this, transformer, callback, resultType).get();
} catch (Exception e) {
throw new UnirestException(e);
}
}
}
return response;
});
}
return asyncR;
}


Expand Down

0 comments on commit 65ec51f

Please sign in to comment.