Skip to content

Commit

Permalink
isolate calls to async so we can do some retry stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Oct 6, 2023
1 parent 3d8a475 commit a204020
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions unirest/src/main/java/kong/unirest/core/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,12 @@ public HttpResponse<Empty> asEmpty() {

@Override
public CompletableFuture<HttpResponse<Empty>> asEmptyAsync() {
return config.getClient()
.request(this, BasicResponse::new, new CompletableFuture<>(), Empty.class);
return requestAsync(this, BasicResponse::new, new CompletableFuture<>(), Empty.class);
}

@Override
public CompletableFuture<HttpResponse<Empty>> asEmptyAsync(Callback<Empty> callback) {
return config.getClient()
.request(this, BasicResponse::new, wrap(callback), Empty.class);
return requestAsync(this, BasicResponse::new, wrap(callback), Empty.class);
}

@Override
Expand All @@ -198,14 +196,12 @@ public HttpResponse<String> asString() throws UnirestException {

@Override
public CompletableFuture<HttpResponse<String>> asStringAsync() {
return config.getClient()
.request(this, r -> new StringResponse(r, responseEncoding), new CompletableFuture<>(), String.class);
return requestAsync(this, r -> new StringResponse(r, responseEncoding), new CompletableFuture<>(), String.class);
}

@Override
public CompletableFuture<HttpResponse<String>> asStringAsync(Callback<String> callback) {
return config.getClient()
.request(this, r -> new StringResponse(r, responseEncoding), wrap(callback), String.class);
return requestAsync(this, r -> new StringResponse(r, responseEncoding), wrap(callback), String.class);
}

@Override
Expand All @@ -215,12 +211,12 @@ public HttpResponse<byte[]> asBytes() {

@Override
public CompletableFuture<HttpResponse<byte[]>> asBytesAsync() {
return config.getClient().request(this, (RawResponse r) -> new ByteResponse(r, downloadMonitor), new CompletableFuture<>(), byte[].class);
return requestAsync(this, (RawResponse r) -> new ByteResponse(r, downloadMonitor), new CompletableFuture<>(), byte[].class);
}

@Override
public CompletableFuture<HttpResponse<byte[]>> asBytesAsync(Callback<byte[]> callback) {
return config.getClient().request(this, (RawResponse r) -> new ByteResponse(r, downloadMonitor), wrap(callback), byte[].class);
return requestAsync(this, (RawResponse r) -> new ByteResponse(r, downloadMonitor), wrap(callback), byte[].class);
}

@Override
Expand All @@ -230,13 +226,12 @@ public HttpResponse<JsonNode> asJson() throws UnirestException {

@Override
public CompletableFuture<HttpResponse<JsonNode>> asJsonAsync() {

return config.getClient().request(this, JsonResponse::new, new CompletableFuture<>(), JsonNode.class);
return requestAsync(this, JsonResponse::new, new CompletableFuture<>(), JsonNode.class);
}

@Override
public CompletableFuture<HttpResponse<JsonNode>> asJsonAsync(Callback<JsonNode> callback) {
return config.getClient().request(this, JsonResponse::new, wrap(callback), JsonNode.class);
return requestAsync(this, JsonResponse::new, wrap(callback), JsonNode.class);
}

@Override
Expand All @@ -256,36 +251,36 @@ public <T> HttpResponse<T> asObject(Function<RawResponse, T> function) {

@Override
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(Function<RawResponse, T> function) {
return config.getClient().request(this, funcResponse(function), new CompletableFuture<>(), JsonNode.class);
return requestAsync(this, funcResponse(function), new CompletableFuture<>(), JsonNode.class);
}

@Override
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(Class<? extends T> responseClass) {
return config.getClient().request(this,
return requestAsync(this,
r -> new ObjectResponse<T>(getObjectMapper(), r, responseClass),
new CompletableFuture<>(),
responseClass);
}

@Override
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(Class<? extends T> responseClass, Callback<T> callback) {
return config.getClient().request(this,
return requestAsync(this,
r -> new ObjectResponse<>(getObjectMapper(), r, responseClass),
wrap(callback),
responseClass);
}

@Override
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(GenericType<T> genericType) {
return config.getClient().request(this,
return requestAsync(this,
r -> new ObjectResponse<>(getObjectMapper(), r, genericType),
new CompletableFuture<>(),
genericType.getTypeClass());
}

@Override
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(GenericType<T> genericType, Callback<T> callback) {
return config.getClient().request(this,
return requestAsync(this,
r -> new ObjectResponse<>(getObjectMapper(), r, genericType),
wrap(callback),
genericType.getTypeClass());
Expand All @@ -302,7 +297,7 @@ public void thenConsume(Consumer<RawResponse> consumer) {

@Override
public void thenConsumeAsync(Consumer<RawResponse> consumer) {
config.getClient().request(this, getConsumer(consumer), new CompletableFuture<>(), Object.class);
requestAsync(this, getConsumer(consumer), new CompletableFuture<>(), Object.class);
}

@Override
Expand All @@ -312,15 +307,15 @@ public HttpResponse<File> asFile(String path, CopyOption... copyOptions) {

@Override
public CompletableFuture<HttpResponse<File>> asFileAsync(String path, CopyOption... copyOptions) {
return config.getClient().request(this,
return requestAsync(this,
r -> new FileResponse(r, path, downloadMonitor, copyOptions),
new CompletableFuture<>(),
File.class);
}

@Override
public CompletableFuture<HttpResponse<File>> asFileAsync(String path, Callback<File> callback, CopyOption... copyOptions) {
return config.getClient().request(this,
return requestAsync(this,
r -> new FileResponse(r, path, downloadMonitor, copyOptions),
wrap(callback),
File.class);
Expand Down Expand Up @@ -356,6 +351,13 @@ private <E> HttpResponse<E> request(Function<RawResponse, HttpResponse<E>> trans
return response;
}

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);
}


private Function<RawResponse, HttpResponse<Object>> getConsumer(Consumer<RawResponse> consumer) {
return r -> {
Expand Down

0 comments on commit a204020

Please sign in to comment.