Refactor DefaultApiVersionInserter

This commit is contained in:
rstoyanchev
2025-05-06 20:36:56 +01:00
parent 22e7f24731
commit e5d4d7c13c
6 changed files with 179 additions and 121 deletions

View File

@@ -18,6 +18,8 @@ package org.springframework.web.client;
import java.net.URI;
import org.jspecify.annotations.Nullable;
import org.springframework.http.HttpHeaders;
/**
@@ -49,4 +51,67 @@ public interface ApiVersionInserter {
default void insertVersion(Object version, HttpHeaders headers) {
}
/**
* Create a builder for an inserter that sets a header.
* @param header the name of a header to hold the version
*/
static Builder fromHeader(@Nullable String header) {
return new DefaultApiVersionInserterBuilder(header, null, null);
}
/**
* Create a builder for an inserter that sets a query parameter.
* @param queryParam the name of a query parameter to hold the version
*/
static Builder fromQueryParam(@Nullable String queryParam) {
return new DefaultApiVersionInserterBuilder(null, queryParam, null);
}
/**
* Create a builder for an inserter that inserts a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
static Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
return new DefaultApiVersionInserterBuilder(null, null, pathSegmentIndex);
}
/**
* Builder for {@link ApiVersionInserter}.
*/
interface Builder {
/**
* Configure the inserter to set a header.
* @param header the name of the header to hold the version
*/
Builder fromHeader(@Nullable String header);
/**
* Configure the inserter to set a query parameter.
* @param queryParam the name of the query parameter to hold the version
*/
Builder fromQueryParam(@Nullable String queryParam);
/**
* Configure the inserter to insert a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
Builder fromPathSegment(@Nullable Integer pathSegmentIndex);
/**
* Format the version Object into a String using the given {@link ApiVersionFormatter}.
* <p>By default, the version is formatted with {@link Object#toString()}.
* @param versionFormatter the formatter to use
*/
Builder withVersionFormatter(ApiVersionFormatter versionFormatter);
/**
* Build the {@link ApiVersionInserter} instance.
*/
ApiVersionInserter build();
}
}

View File

@@ -27,15 +27,13 @@ import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Default implementation of {@link ApiVersionInserter} to insert the version
* into a request header, query parameter, or the URL path.
*
* <p>Use {@link #builder()} to create an instance.
* Default implementation of {@link ApiVersionInserter}.
*
* @author Rossen Stoyanchev
* @since 7.0
* @see DefaultApiVersionInserterBuilder
*/
public final class DefaultApiVersionInserter implements ApiVersionInserter {
final class DefaultApiVersionInserter implements ApiVersionInserter {
private final @Nullable String header;
@@ -46,7 +44,7 @@ public final class DefaultApiVersionInserter implements ApiVersionInserter {
private final ApiVersionFormatter versionFormatter;
private DefaultApiVersionInserter(
DefaultApiVersionInserter(
@Nullable String header, @Nullable String queryParam, @Nullable Integer pathSegmentIndex,
@Nullable ApiVersionFormatter formatter) {
@@ -92,102 +90,4 @@ public final class DefaultApiVersionInserter implements ApiVersionInserter {
}
}
/**
* Create a builder for an inserter that sets a header.
* @param header the name of a header to hold the version
*/
public static Builder fromHeader(@Nullable String header) {
return new Builder(header, null, null);
}
/**
* Create a builder for an inserter that sets a query parameter.
* @param queryParam the name of a query parameter to hold the version
*/
public static Builder fromQueryParam(@Nullable String queryParam) {
return new Builder(null, queryParam, null);
}
/**
* Create a builder for an inserter that inserts a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
public static Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
return new Builder(null, null, pathSegmentIndex);
}
/**
* Create a builder.
*/
public static Builder builder() {
return new Builder(null, null, null);
}
/**
* A builder for {@link DefaultApiVersionInserter}.
*/
public static final class Builder {
private @Nullable String header;
private @Nullable String queryParam;
private @Nullable Integer pathSegmentIndex;
private @Nullable ApiVersionFormatter versionFormatter;
private Builder(@Nullable String header, @Nullable String queryParam, @Nullable Integer pathSegmentIndex) {
this.header = header;
this.queryParam = queryParam;
this.pathSegmentIndex = pathSegmentIndex;
}
/**
* Configure the inserter to set a header.
* @param header the name of the header to hold the version
*/
public Builder fromHeader(@Nullable String header) {
this.header = header;
return this;
}
/**
* Configure the inserter to set a query parameter.
* @param queryParam the name of the query parameter to hold the version
*/
public Builder fromQueryParam(@Nullable String queryParam) {
this.queryParam = queryParam;
return this;
}
/**
* Configure the inserter to insert a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
public Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
this.pathSegmentIndex = pathSegmentIndex;
return this;
}
/**
* Format the version Object into a String using the given {@link ApiVersionFormatter}.
* <p>By default, the version is formatted with {@link Object#toString()}.
* @param versionFormatter the formatter to use
*/
public Builder withVersionFormatter(ApiVersionFormatter versionFormatter) {
this.versionFormatter = versionFormatter;
return this;
}
/**
* Build the inserter.
*/
public ApiVersionInserter build() {
return new DefaultApiVersionInserter(
this.header, this.queryParam, this.pathSegmentIndex, this.versionFormatter);
}
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.client;
import org.jspecify.annotations.Nullable;
/**
* Default implementation of {@link ApiVersionInserter.Builder}.
*
* @author Rossen Stoyanchev
* @since 7.0
* @see ApiVersionInserter#fromHeader(String)
* @see ApiVersionInserter#fromQueryParam(String)
* @see ApiVersionInserter#fromPathSegment(Integer)
*/
final class DefaultApiVersionInserterBuilder implements ApiVersionInserter.Builder {
private @Nullable String header;
private @Nullable String queryParam;
private @Nullable Integer pathSegmentIndex;
private @Nullable ApiVersionFormatter versionFormatter;
DefaultApiVersionInserterBuilder(
@Nullable String header, @Nullable String queryParam, @Nullable Integer pathSegmentIndex) {
this.header = header;
this.queryParam = queryParam;
this.pathSegmentIndex = pathSegmentIndex;
}
/**
* Configure the inserter to set a header.
* @param header the name of the header to hold the version
*/
public ApiVersionInserter.Builder fromHeader(@Nullable String header) {
this.header = header;
return this;
}
/**
* Configure the inserter to set a query parameter.
* @param queryParam the name of the query parameter to hold the version
*/
public ApiVersionInserter.Builder fromQueryParam(@Nullable String queryParam) {
this.queryParam = queryParam;
return this;
}
/**
* Configure the inserter to insert a path segment.
* @param pathSegmentIndex the index of the path segment to hold the version
*/
public ApiVersionInserter.Builder fromPathSegment(@Nullable Integer pathSegmentIndex) {
this.pathSegmentIndex = pathSegmentIndex;
return this;
}
/**
* Format the version Object into a String using the given {@link ApiVersionFormatter}.
* <p>By default, the version is formatted with {@link Object#toString()}.
* @param versionFormatter the formatter to use
*/
public ApiVersionInserter.Builder withVersionFormatter(ApiVersionFormatter versionFormatter) {
this.versionFormatter = versionFormatter;
return this;
}
/**
* Build the inserter.
*/
public ApiVersionInserter build() {
return new DefaultApiVersionInserter(
this.header, this.queryParam, this.pathSegmentIndex, this.versionFormatter);
}
}