Add RestClient.RequestHeadersSpec#exchangeForRequiredValue
This commit adds a variant to RestClient.RequestHeadersSpec#exchange suitable for functions returning non-null values. Closes gh-34692
This commit is contained in:
@@ -533,6 +533,13 @@ final class DefaultRestClient implements RestClient {
|
||||
return exchangeInternal(exchangeFunction, close);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T exchangeForRequiredValue(RequiredValueExchangeFunction<T> exchangeFunction, boolean close) {
|
||||
T value = exchangeInternal(exchangeFunction, close);
|
||||
Assert.state(value != null, "The exchanged value must not be null");
|
||||
return value;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean close) {
|
||||
Assert.notNull(exchangeFunction, "ExchangeFunction must not be null");
|
||||
|
||||
@@ -671,12 +671,41 @@ public interface RestClient {
|
||||
* @param exchangeFunction the function to handle the response with
|
||||
* @param <T> the type the response will be transformed to
|
||||
* @return the value returned from the exchange function, potentially {@code null}
|
||||
* @see RequestHeadersSpec#exchangeForRequiredValue(RequiredValueExchangeFunction)
|
||||
*/
|
||||
@Nullable
|
||||
default <T> T exchange(ExchangeFunction<T> exchangeFunction) {
|
||||
return exchange(exchangeFunction, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchange the {@link ClientHttpResponse} for a value of type {@code T}.
|
||||
* This can be useful for advanced scenarios, for example to decode the
|
||||
* response differently depending on the response status:
|
||||
* <pre class="code">
|
||||
* Person person = client.get()
|
||||
* .uri("/people/1")
|
||||
* .accept(MediaType.APPLICATION_JSON)
|
||||
* .exchange((request, response) -> {
|
||||
* if (response.getStatusCode().equals(HttpStatus.OK)) {
|
||||
* return deserialize(response.getBody());
|
||||
* }
|
||||
* else {
|
||||
* throw new BusinessException();
|
||||
* }
|
||||
* });
|
||||
* </pre>
|
||||
* <p><strong>Note:</strong> The response is
|
||||
* {@linkplain ClientHttpResponse#close() closed} after the exchange
|
||||
* function has been invoked.
|
||||
* @param exchangeFunction the function to handle the response with
|
||||
* @param <T> the type the response will be transformed to
|
||||
* @return the value returned from the exchange function, never {@code null}
|
||||
*/
|
||||
default <T> T exchangeForRequiredValue(RequiredValueExchangeFunction<T> exchangeFunction) {
|
||||
return exchangeForRequiredValue(exchangeFunction, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchange the {@link ClientHttpResponse} for a value of type {@code T}.
|
||||
* This can be useful for advanced scenarios, for example to decode the
|
||||
@@ -703,10 +732,40 @@ public interface RestClient {
|
||||
* {@code exchangeFunction} is invoked, {@code false} to keep it open
|
||||
* @param <T> the type the response will be transformed to
|
||||
* @return the value returned from the exchange function, potentially {@code null}
|
||||
* @see RequestHeadersSpec#exchangeForRequiredValue(RequiredValueExchangeFunction, boolean)
|
||||
*/
|
||||
@Nullable
|
||||
<T> T exchange(ExchangeFunction<T> exchangeFunction, boolean close);
|
||||
|
||||
/**
|
||||
* Exchange the {@link ClientHttpResponse} for a value of type {@code T}.
|
||||
* This can be useful for advanced scenarios, for example to decode the
|
||||
* response differently depending on the response status:
|
||||
* <pre class="code">
|
||||
* Person person = client.get()
|
||||
* .uri("/people/1")
|
||||
* .accept(MediaType.APPLICATION_JSON)
|
||||
* .exchange((request, response) -> {
|
||||
* if (response.getStatusCode().equals(HttpStatus.OK)) {
|
||||
* return deserialize(response.getBody());
|
||||
* }
|
||||
* else {
|
||||
* throw new BusinessException();
|
||||
* }
|
||||
* });
|
||||
* </pre>
|
||||
* <p><strong>Note:</strong> If {@code close} is {@code true},
|
||||
* then the response is {@linkplain ClientHttpResponse#close() closed}
|
||||
* after the exchange function has been invoked. When set to
|
||||
* {@code false}, the caller is responsible for closing the response.
|
||||
* @param exchangeFunction the function to handle the response with
|
||||
* @param close {@code true} to close the response after
|
||||
* {@code exchangeFunction} is invoked, {@code false} to keep it open
|
||||
* @param <T> the type the response will be transformed to
|
||||
* @return the value returned from the exchange function, never {@code null}
|
||||
*/
|
||||
<T> T exchangeForRequiredValue(RequiredValueExchangeFunction<T> exchangeFunction, boolean close);
|
||||
|
||||
|
||||
/**
|
||||
* Defines the contract for {@link #exchange(ExchangeFunction)}.
|
||||
@@ -726,6 +785,22 @@ public interface RestClient {
|
||||
T exchange(HttpRequest clientRequest, ConvertibleClientHttpResponse clientResponse) throws IOException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link ExchangeFunction} returning a non-null required value.
|
||||
* @param <T> the type the response will be transformed to
|
||||
*/
|
||||
@FunctionalInterface
|
||||
interface RequiredValueExchangeFunction<T> extends ExchangeFunction<T> {
|
||||
|
||||
/**
|
||||
* Exchange the given response into a value of type {@code T}.
|
||||
* @param clientRequest the request
|
||||
* @param clientResponse the response
|
||||
* @return the exchanged value, never {@code null}
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
T exchange(HttpRequest clientRequest, ConvertibleClientHttpResponse clientResponse) throws IOException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension of {@link ClientHttpResponse} that can convert the body.
|
||||
|
||||
Reference in New Issue
Block a user