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 Nov 21, 2023
1 parent d2fcda6 commit 21482ac
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 @@ -19,6 +19,7 @@
import io.vertx.core.net.SSLOptions;

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

/**
* An asynchronous HTTP client.
Expand Down Expand Up @@ -59,6 +60,10 @@ public interface HttpClient extends io.vertx.core.metrics.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 @@ -72,6 +77,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 @@ -84,6 +93,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 @@ -95,6 +108,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);
}

/**
* Close the client immediately ({@code close(0, 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 @@ -6544,10 +6544,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 21482ac

Please sign in to comment.