Skip to content

Commit 6f96304

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

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.vertx.core.Future;
1616

1717
import java.util.concurrent.TimeUnit;
18+
import java.util.function.Function;
1819

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

45+
default <T> Future<T> request(RequestOptions options, Function<HttpClientRequest, Future<T>> handler) {
46+
return request(options).compose(handler);
47+
}
48+
4449
/**
4550
* Create an HTTP request to send to the server at the {@code host} and {@code port}.
4651
*
@@ -54,6 +59,10 @@ default Future<HttpClientRequest> request(HttpMethod method, int port, String ho
5459
return request(new RequestOptions().setMethod(method).setPort(port).setHost(host).setURI(requestURI));
5560
}
5661

62+
default <T> Future<T> request(HttpMethod method, int port, String host, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
63+
return request(method, port, host, requestURI).compose(handler);
64+
}
65+
5766
/**
5867
* Create an HTTP request to send to the server at the {@code host} and default port.
5968
*
@@ -66,6 +75,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String host, String
6675
return request(new RequestOptions().setMethod(method).setHost(host).setURI(requestURI));
6776
}
6877

78+
default <T> Future<T> request(HttpMethod method, String host, String requestURI , Function<HttpClientRequest, Future<T>> handler) {
79+
return request(method, host, requestURI).compose(handler);
80+
}
81+
6982
/**
7083
* Create an HTTP request to send to the server at the default host and port.
7184
*
@@ -77,6 +90,10 @@ default Future<HttpClientRequest> request(HttpMethod method, String requestURI)
7790
return request(new RequestOptions().setMethod(method).setURI(requestURI));
7891
}
7992

93+
default <T> Future<T> request(HttpMethod method, String requestURI, Function<HttpClientRequest, Future<T>> handler) {
94+
return request(method, requestURI).compose(handler);
95+
}
96+
8097
/**
8198
* Shutdown with a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}).
8299
*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6813,10 +6813,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
68136813
}
68146814
);
68156815
startServer(testAddress);
6816-
client.request(requestOptions)
6817-
.compose(req -> req.send()
6816+
client.request(requestOptions, req -> req
6817+
.send()
68186818
.expecting(HttpResponseExpectation.SC_OK)
6819-
.compose(HttpClientResponse::end))
6819+
.compose(HttpClientResponse::end)
6820+
)
68206821
.onComplete(onSuccess(nothing -> complete()));
68216822
await();
68226823
}

0 commit comments

Comments
 (0)