Skip to content

Commit

Permalink
Experiment with HttpClient HTTP request/response API method design th…
Browse files Browse the repository at this point in the history
…at provides non racy methods when called outside event-loop.
  • Loading branch information
vietj committed Mar 4, 2024
1 parent 8de9656 commit aa6c664
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/main/java/io/vertx/core/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.core.Future;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
* The API to interacts with an HTTP server.
Expand All @@ -41,6 +42,10 @@ default Future<HttpClientRequest> request() {
*/
Future<HttpClientRequest> request(RequestOptions options);

default <T> Future<T> request(RequestOptions options, Function<HttpClientRequest, Future<T>> handler) {
return request(options).compose(handler);
}

/**
* Create an HTTP request to send to the server at the {@code host} and {@code port}.
*
Expand All @@ -54,6 +59,10 @@ default Future<HttpClientRequest> request(HttpMethod method, int port, String ho
return request(new RequestOptions().setMethod(method).setPort(port).setHost(host).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, int port, String host, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
return request(method, port, host, requestURI).compose(handler);
}

/**
* Create an HTTP request to send to the server at the {@code host} and default port.
*
Expand All @@ -66,6 +75,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String host, String
return request(new RequestOptions().setMethod(method).setHost(host).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, String host, String requestURI , Function<HttpClientRequest, Future<T>> handler) {
return request(method, host, requestURI).compose(handler);
}

/**
* Create an HTTP request to send to the server at the default host and port.
*
Expand All @@ -77,6 +90,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String requestURI)
return request(new RequestOptions().setMethod(method).setURI(requestURI));
}

default <T> Future<T> request(HttpMethod method, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
return request(method, requestURI).compose(handler);
}

/**
* Shutdown with a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}).
*
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/io/vertx/core/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6604,10 +6604,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
}
);
startServer(testAddress);
client.request(requestOptions)
.compose(req -> req.send()
client.request(requestOptions, req -> req
.send()
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
.compose(HttpClientResponse::end))
.compose(HttpClientResponse::end)
)
.onComplete(onSuccess(nothing -> complete()));
await();
}
Expand Down

0 comments on commit aa6c664

Please sign in to comment.