Skip to content

Commit 9bf9444

Browse files
committed
Experiment with HttpClient HTTP request/response API method design that provides non racy methods when called outside event-loop.
1 parent ca53645 commit 9bf9444

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main/java/io/vertx/core/http/HttpClient.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public interface HttpClient extends Measured {
6363
*/
6464
Future<HttpClientRequest> request(RequestOptions options);
6565

66+
default <T> Future<T> request(RequestOptions options, Function<HttpClientRequest, Future<T>> handler) {
67+
return request(options).compose(handler);
68+
}
69+
6670
/**
6771
* Create an HTTP request to send to the server at the {@code host} and {@code port}.
6872
*
@@ -74,6 +78,10 @@ public interface HttpClient extends Measured {
7478
*/
7579
Future<HttpClientRequest> request(HttpMethod method, int port, String host, String requestURI);
7680

81+
default <T> Future<T> request(HttpMethod method, int port, String host, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
82+
return request(method, port, host, requestURI).compose(handler);
83+
}
84+
7785
/**
7886
* Create an HTTP request to send to the server at the {@code host} and default port.
7987
*
@@ -84,6 +92,10 @@ public interface HttpClient extends Measured {
8492
*/
8593
Future<HttpClientRequest> request(HttpMethod method, String host, String requestURI);
8694

95+
default <T> Future<T> request(HttpMethod method, String host, String requestURI , Function<HttpClientRequest, Future<T>> handler) {
96+
return request(method, host, requestURI).compose(handler);
97+
}
98+
8799
/**
88100
* Create an HTTP request to send to the server at the default host and port.
89101
*
@@ -93,6 +105,10 @@ public interface HttpClient extends Measured {
93105
*/
94106
Future<HttpClientRequest> request(HttpMethod method, String requestURI);
95107

108+
default <T> Future<T> request(HttpMethod method, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
109+
return request(method, requestURI).compose(handler);
110+
}
111+
96112
/**
97113
* Connect a WebSocket to the specified port, host and relative request URI.
98114
*

src/test/java/io/vertx/core/http/HttpTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6708,10 +6708,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
67086708
}
67096709
);
67106710
startServer(testAddress);
6711-
client.request(requestOptions)
6712-
.compose(req -> req.send()
6711+
client.request(requestOptions, req -> req
6712+
.send()
67136713
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
6714-
.compose(HttpClientResponse::end))
6714+
.compose(HttpClientResponse::end)
6715+
)
67156716
.onComplete(onSuccess(nothing -> complete()));
67166717
await();
67176718
}

0 commit comments

Comments
 (0)