Skip to content

Commit 9e1e5fb

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

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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
*

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6801,10 +6801,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except
68016801
}
68026802
);
68036803
startServer(testAddress);
6804-
client.request(requestOptions)
6805-
.compose(req -> req.send()
6804+
client.request(requestOptions, req -> req
6805+
.send()
68066806
.andThen(onSuccess(resp -> assertEquals(200, resp.statusCode())))
6807-
.compose(HttpClientResponse::end))
6807+
.compose(HttpClientResponse::end)
6808+
)
68086809
.onComplete(onSuccess(nothing -> complete()));
68096810
await();
68106811
}

0 commit comments

Comments
 (0)