Skip to content

Commit

Permalink
Added overloaded exchange method to keep connection open
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Jul 6, 2023
1 parent 0bf85af commit 8ba3c9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ public ResponseSpec retrieve() {
}

@Override
public <T> T exchange(ExchangeFunction<T> exchangeFunction) {
return exchangeInternal(exchangeFunction, true);
public <T> T exchange(ExchangeFunction<T> exchangeFunction, boolean close) {
return exchangeInternal(exchangeFunction, close);
}

private <T> T exchangeInternal(ExchangeFunction<T> exchangeFunction, boolean close) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,38 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
* @param <T> the type the response will be transformed to
* @return the value returned from the exchange function
*/
<T> T exchange(ExchangeFunction<T> exchangeFunction);
default <T> T exchange(ExchangeFunction<T> exchangeFunction) {
return exchange(exchangeFunction, true);
}

/**
* Exchange the {@link ClientHttpResponse} for a type {@code T}. This
* can be useful for advanced scenarios, for example to decode the
* response differently depending on the response status:
* <p><pre>
* Person person = client.get()
* .uri("/people/1")
* .accept(MediaType.APPLICATION_JSON)
* .exchange((request, response) -&gt; {
* 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
*/
<T> T exchange(ExchangeFunction<T> exchangeFunction, boolean close);


/**
Expand Down

0 comments on commit 8ba3c9d

Please sign in to comment.