Add defaultApiVersion to RestClient and WebClient
Closes gh-34857
This commit is contained in:
@@ -94,6 +94,8 @@ final class DefaultWebClient implements WebClient {
|
||||
|
||||
private final @Nullable MultiValueMap<String, String> defaultCookies;
|
||||
|
||||
private final @Nullable Object defaultApiVersion;
|
||||
|
||||
private final @Nullable ApiVersionInserter apiVersionInserter;
|
||||
|
||||
private final @Nullable Consumer<RequestHeadersSpec<?>> defaultRequest;
|
||||
@@ -110,7 +112,7 @@ final class DefaultWebClient implements WebClient {
|
||||
DefaultWebClient(ExchangeFunction exchangeFunction, @Nullable ExchangeFilterFunction filterFunctions,
|
||||
UriBuilderFactory uriBuilderFactory, @Nullable HttpHeaders defaultHeaders,
|
||||
@Nullable MultiValueMap<String, String> defaultCookies,
|
||||
@Nullable ApiVersionInserter apiVersionInserter,
|
||||
@Nullable Object defaultApiVersion, @Nullable ApiVersionInserter apiVersionInserter,
|
||||
@Nullable Consumer<RequestHeadersSpec<?>> defaultRequest,
|
||||
@Nullable Map<Predicate<HttpStatusCode>, Function<ClientResponse, Mono<? extends Throwable>>> statusHandlerMap,
|
||||
ObservationRegistry observationRegistry, @Nullable ClientRequestObservationConvention observationConvention,
|
||||
@@ -121,6 +123,7 @@ final class DefaultWebClient implements WebClient {
|
||||
this.uriBuilderFactory = uriBuilderFactory;
|
||||
this.defaultHeaders = defaultHeaders;
|
||||
this.defaultCookies = defaultCookies;
|
||||
this.defaultApiVersion = defaultApiVersion;
|
||||
this.apiVersionInserter = apiVersionInserter;
|
||||
this.defaultRequest = defaultRequest;
|
||||
this.defaultStatusHandlers = initStatusHandlers(statusHandlerMap);
|
||||
@@ -491,13 +494,18 @@ final class DefaultWebClient implements WebClient {
|
||||
|
||||
private URI initUri() {
|
||||
URI uriToUse = (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
|
||||
if (this.apiVersion != null) {
|
||||
Object version = getApiVersionOrDefault();
|
||||
if (version != null) {
|
||||
Assert.state(apiVersionInserter != null, "No ApiVersionInserter configured");
|
||||
uriToUse = apiVersionInserter.insertVersion(this.apiVersion, uriToUse);
|
||||
uriToUse = apiVersionInserter.insertVersion(version, uriToUse);
|
||||
}
|
||||
return uriToUse;
|
||||
}
|
||||
|
||||
private @Nullable Object getApiVersionOrDefault() {
|
||||
return (this.apiVersion != null ? this.apiVersion : DefaultWebClient.this.defaultApiVersion);
|
||||
}
|
||||
|
||||
private void initHeaders(HttpHeaders out) {
|
||||
if (defaultHeaders != null && !defaultHeaders.isEmpty()) {
|
||||
out.putAll(defaultHeaders);
|
||||
@@ -505,9 +513,10 @@ final class DefaultWebClient implements WebClient {
|
||||
if (this.headers != null && !this.headers.isEmpty()) {
|
||||
out.putAll(this.headers);
|
||||
}
|
||||
if (this.apiVersion != null) {
|
||||
Object version = getApiVersionOrDefault();
|
||||
if (version != null) {
|
||||
Assert.state(apiVersionInserter != null, "No ApiVersionInserter configured");
|
||||
apiVersionInserter.insertVersion(this.apiVersion, out);
|
||||
apiVersionInserter.insertVersion(version, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
|
||||
private @Nullable MultiValueMap<String, String> defaultCookies;
|
||||
|
||||
private @Nullable Object defaultApiVersion;
|
||||
|
||||
private @Nullable ApiVersionInserter apiVersionInserter;
|
||||
|
||||
private @Nullable Consumer<WebClient.RequestHeadersSpec<?>> defaultRequest;
|
||||
@@ -122,6 +124,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
}
|
||||
|
||||
this.defaultCookies = (other.defaultCookies != null ? new LinkedMultiValueMap<>(other.defaultCookies) : null);
|
||||
|
||||
this.defaultApiVersion = other.defaultApiVersion;
|
||||
this.apiVersionInserter = other.apiVersionInserter;
|
||||
|
||||
this.defaultRequest = other.defaultRequest;
|
||||
@@ -194,6 +198,11 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
return this.defaultCookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder defaultApiVersion(Object version) {
|
||||
this.defaultApiVersion = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebClient.Builder apiVersionInserter(ApiVersionInserter apiVersionInserter) {
|
||||
@@ -308,7 +317,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
|
||||
return new DefaultWebClient(
|
||||
exchange, filterFunctions,
|
||||
initUriBuilderFactory(), defaultHeaders, defaultCookies,
|
||||
this.apiVersionInserter,
|
||||
this.defaultApiVersion, this.apiVersionInserter,
|
||||
this.defaultRequest,
|
||||
this.statusHandlers,
|
||||
this.observationRegistry, this.observationConvention,
|
||||
|
||||
@@ -252,6 +252,15 @@ public interface WebClient {
|
||||
*/
|
||||
Builder defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
|
||||
|
||||
/**
|
||||
* Global option to specify an API version to add to every request,
|
||||
* if not already set.
|
||||
* @param version the version to use
|
||||
* @return this builder
|
||||
* @since 7.0
|
||||
*/
|
||||
Builder defaultApiVersion(Object version);
|
||||
|
||||
/**
|
||||
* Configure an {@link ApiVersionInserter} to abstract how an API version
|
||||
* specified via {@link RequestHeadersSpec#apiVersion(Object)}
|
||||
|
||||
Reference in New Issue
Block a user