Class DmxHttpClient
HttpClient that returns Result<T, HttpError>
instead of throwing exceptions.
Both network failures (IOException, InterruptedException) and
HTTP error status codes (4xx, 5xx) are surfaced as typed HttpError variants,
collapsing two separate failure modes into one:
DmxHttpClient client = DmxHttpClient.of(HttpClient.newHttpClient());
client.send(request, BodyHandlers.ofString())
.peek(body -> process(body))
.peekError(err -> log.warn("HTTP call failed: {}", err));
Limitation: null-body handlers are not supported
Body handlers that produce a null body — specifically
HttpResponse.BodyHandlers.discarding() — are not supported.
Result.ok() does not accept null values, so passing a null-body handler
will throw NullPointerException on a successful response.
If you need to send a request and ignore the response body, use the plain
HttpClient directly, or wait for a future sendDiscarding() overload.
-
Method Summary
Modifier and TypeMethodDescriptionstatic DmxHttpClientof(HttpClient client) Creates a newDmxHttpClientwrapping the givenHttpClient.send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Sends an HTTP request synchronously and returns the result.send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, CheckedFunction<T, R> deserializer) Sends an HTTP request synchronously, then appliesdeserializerto the body.<T> CompletableFuture<Result<T, HttpError>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Sends an HTTP request asynchronously and returns aCompletableFuturethat completes with aResult.
-
Method Details
-
of
Creates a newDmxHttpClientwrapping the givenHttpClient.- Parameters:
client- the underlying JDK HTTP client; must not benull- Returns:
- a new
DmxHttpClient
-
send
Sends an HTTP request synchronously and returns the result.Status mapping:
- 2xx / 3xx →
Result.ok(body) - 4xx →
Result.err(ClientError) - 5xx →
Result.err(ServerError) HttpTimeoutException→Result.err(Timeout)IOException/InterruptedException→Result.err(NetworkFailure)
- Type Parameters:
T- the body type- Parameters:
request- the request to send; must not benullbodyHandler- the response body handler; must not benull- Returns:
- a
Resultwrapping the body or anHttpError
- 2xx / 3xx →
-
send
public <T,R> Result<R, HttpError> send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, CheckedFunction<T, R> deserializer) Sends an HTTP request synchronously, then appliesdeserializerto the body.If the HTTP call succeeds but
deserializerthrows, the exception is wrapped in aHttpError.NetworkFailure.- Type Parameters:
T- the raw body type produced bybodyHandlerR- the deserialized value type- Parameters:
request- the request to send; must not benullbodyHandler- the response body handler; must not benulldeserializer- function applied to the raw body on success; must not benull- Returns:
- a
Resultwrapping the deserialized value or anHttpError
-
sendAsync
public <T> CompletableFuture<Result<T, HttpError>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Sends an HTTP request asynchronously and returns aCompletableFuturethat completes with aResult.The returned future never completes exceptionally — transport failures are wrapped in
HttpError.NetworkFailureorHttpError.Timeout.- Type Parameters:
T- the body type- Parameters:
request- the request to send; must not benullbodyHandler- the response body handler; must not benull- Returns:
- a future that completes with a
Resultwrapping the body or anHttpError
-