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 May 17, 2023
1 parent ca53645 commit 9bf9444
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/main/java/io/vertx/core/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public interface HttpClient extends Measured {
*/
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 @@ -74,6 +78,10 @@ public interface HttpClient extends Measured {
*/
Future<HttpClientRequest> request(HttpMethod method, int port, String host, String 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 @@ -84,6 +92,10 @@ public interface HttpClient extends Measured {
*/
Future<HttpClientRequest> request(HttpMethod method, String host, String 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 @@ -93,6 +105,10 @@ public interface HttpClient extends Measured {
*/
Future<HttpClientRequest> request(HttpMethod method, String requestURI);

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

/**
* Connect a WebSocket to the specified port, host and relative request URI.
*
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 @@ -6708,10 +6708,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 9bf9444

Please sign in to comment.