diff --git a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ApiMethodDescriptor.java b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ApiMethodDescriptor.java index fcd3c6b33..6952f4885 100644 --- a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ApiMethodDescriptor.java +++ b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ApiMethodDescriptor.java @@ -68,6 +68,8 @@ public static Builder newBuilder() { .setType(MethodType.UNARY); } + public abstract Builder toBuilder(); + @AutoValue.Builder public abstract static class Builder { @@ -76,6 +78,8 @@ public abstract static class Builder { public abstract Builder setRequestFormatter( HttpRequestFormatter requestFormatter); + public abstract HttpRequestFormatter getRequestFormatter(); + public abstract Builder setResponseParser( HttpResponseParser responseParser); diff --git a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java index 9f75ba576..e617522d7 100644 --- a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java +++ b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/ProtoMessageRequestFormatter.java @@ -30,6 +30,7 @@ package com.google.api.gax.httpjson; import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; import com.google.api.pathtemplate.PathTemplate; import com.google.protobuf.Message; import java.util.List; @@ -45,16 +46,19 @@ public class ProtoMessageRequestFormatter // Map> returned value type of the getQueryParamNames interface method // implemented by this class. private final FieldsExtractor>> queryParamsExtractor; + private final String rawPath; private final PathTemplate pathTemplate; private final FieldsExtractor> pathVarsExtractor; private ProtoMessageRequestFormatter( FieldsExtractor requestBodyExtractor, FieldsExtractor>> queryParamsExtractor, + String rawPath, PathTemplate pathTemplate, FieldsExtractor> pathVarsExtractor) { this.requestBodyExtractor = requestBodyExtractor; this.queryParamsExtractor = queryParamsExtractor; + this.rawPath = rawPath; this.pathTemplate = pathTemplate; this.pathVarsExtractor = pathVarsExtractor; } @@ -64,6 +68,13 @@ ProtoMessageRequestFormatter.Builder newBuilder() { return new Builder<>(); } + public Builder toBuilder() { + return new Builder() + .setPath(rawPath, pathVarsExtractor) + .setQueryParamsExtractor(queryParamsExtractor) + .setRequestBodyExtractor(requestBodyExtractor); + } + /* {@inheritDoc} */ @Override public Map> getQueryParamNames(RequestT apiMessage) { @@ -93,7 +104,7 @@ public PathTemplate getPathTemplate() { public static class Builder { private FieldsExtractor requestBodyExtractor; private FieldsExtractor>> queryParamsExtractor; - private String path; + private String rawPath; private FieldsExtractor> pathVarsExtractor; public Builder setRequestBodyExtractor( @@ -109,15 +120,25 @@ public Builder setQueryParamsExtractor( } public Builder setPath( - String path, FieldsExtractor> pathVarsExtractor) { - this.path = path; + String rawPath, FieldsExtractor> pathVarsExtractor) { + this.rawPath = rawPath; this.pathVarsExtractor = pathVarsExtractor; return this; } + @InternalApi + public Builder updateRawPath(String target, String replacement) { + this.rawPath = this.rawPath.replace(target, replacement); + return this; + } + public ProtoMessageRequestFormatter build() { return new ProtoMessageRequestFormatter<>( - requestBodyExtractor, queryParamsExtractor, PathTemplate.create(path), pathVarsExtractor); + requestBodyExtractor, + queryParamsExtractor, + rawPath, + PathTemplate.create(rawPath), + pathVarsExtractor); } } } diff --git a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/longrunning/stub/HttpJsonOperationsStub.java b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/longrunning/stub/HttpJsonOperationsStub.java index dd9cfb54a..32cad36f7 100644 --- a/gax-httpjson/src/main/java/com/google/api/gax/httpjson/longrunning/stub/HttpJsonOperationsStub.java +++ b/gax-httpjson/src/main/java/com/google/api/gax/httpjson/longrunning/stub/HttpJsonOperationsStub.java @@ -53,6 +53,7 @@ import com.google.longrunning.ListOperationsResponse; import com.google.longrunning.Operation; import com.google.protobuf.Empty; +import com.google.protobuf.Message; import com.google.protobuf.TypeRegistry; import java.io.IOException; import java.util.ArrayList; @@ -60,6 +61,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** @@ -69,6 +72,9 @@ */ @BetaApi("A restructuring of stub classes is planned, so this may break in the future") public class HttpJsonOperationsStub extends OperationsStub { + private static final Pattern CLIENT_PACKAGE_VERSION_PATTERN = + Pattern.compile("\\.(?v\\d+[a-zA-Z]*\\d*[a-zA-Z]*\\d*)\\.[\\w.]*stub"); + private static final ApiMethodDescriptor listOperationsMethodDescriptor = ApiMethodDescriptor.newBuilder() @@ -77,7 +83,7 @@ public class HttpJsonOperationsStub extends OperationsStub { .setRequestFormatter( ProtoMessageRequestFormatter.newBuilder() .setPath( - "/v1/{name=operations}", + "/v1/{name=**}/operations", request -> { Map fields = new HashMap<>(); ProtoRestSerializer serializer = @@ -111,7 +117,7 @@ public class HttpJsonOperationsStub extends OperationsStub { .setRequestFormatter( ProtoMessageRequestFormatter.newBuilder() .setPath( - "/v1/{name=operations/**}", + "/v1/{name=**/operations/*}", request -> { Map fields = new HashMap<>(); ProtoRestSerializer serializer = @@ -141,7 +147,7 @@ public class HttpJsonOperationsStub extends OperationsStub { .setRequestFormatter( ProtoMessageRequestFormatter.newBuilder() .setPath( - "/v1/{name=operations/**}", + "/v1/{name=**/operations/*}", request -> { Map fields = new HashMap<>(); ProtoRestSerializer serializer = @@ -166,7 +172,7 @@ public class HttpJsonOperationsStub extends OperationsStub { .setRequestFormatter( ProtoMessageRequestFormatter.newBuilder() .setPath( - "/v1/{name=operations/**}:cancel", + "/v1/{name=**/operations/*}:cancel", request -> { Map fields = new HashMap<>(); ProtoRestSerializer serializer = @@ -249,25 +255,34 @@ protected HttpJsonOperationsStub( throws IOException { this.callableFactory = callableFactory; + Matcher packageMatcher = + CLIENT_PACKAGE_VERSION_PATTERN.matcher(callableFactory.getClass().getPackage().getName()); + + String apiVersion = packageMatcher.find() ? packageMatcher.group("version") : null; + HttpJsonCallSettings listOperationsTransportSettings = HttpJsonCallSettings.newBuilder() - .setMethodDescriptor(listOperationsMethodDescriptor) + .setMethodDescriptor( + getApiVersionedMethodDescriptor(listOperationsMethodDescriptor, apiVersion)) .setTypeRegistry(typeRegistry) .build(); HttpJsonCallSettings getOperationTransportSettings = HttpJsonCallSettings.newBuilder() - .setMethodDescriptor(getOperationMethodDescriptor) + .setMethodDescriptor( + getApiVersionedMethodDescriptor(getOperationMethodDescriptor, apiVersion)) .setTypeRegistry(typeRegistry) .build(); HttpJsonCallSettings deleteOperationTransportSettings = HttpJsonCallSettings.newBuilder() - .setMethodDescriptor(deleteOperationMethodDescriptor) + .setMethodDescriptor( + getApiVersionedMethodDescriptor(deleteOperationMethodDescriptor, apiVersion)) .setTypeRegistry(typeRegistry) .build(); HttpJsonCallSettings cancelOperationTransportSettings = HttpJsonCallSettings.newBuilder() - .setMethodDescriptor(cancelOperationMethodDescriptor) + .setMethodDescriptor( + getApiVersionedMethodDescriptor(cancelOperationMethodDescriptor, apiVersion)) .setTypeRegistry(typeRegistry) .build(); @@ -296,6 +311,24 @@ protected HttpJsonOperationsStub( backgroundResources = new BackgroundResourceAggregation(clientContext.getBackgroundResources()); } + private static + ApiMethodDescriptor getApiVersionedMethodDescriptor( + ApiMethodDescriptor methodDescriptor, String apiVersion) { + if (apiVersion == null) { + return methodDescriptor; + } + + ApiMethodDescriptor.Builder descriptorBuilder = + methodDescriptor.toBuilder(); + ProtoMessageRequestFormatter requestFormatter = + (ProtoMessageRequestFormatter) descriptorBuilder.getRequestFormatter(); + + return descriptorBuilder + .setRequestFormatter( + requestFormatter.toBuilder().updateRawPath("/v1/", '/' + apiVersion + '/').build()) + .build(); + } + @InternalApi public static List getMethodDescriptors() { List methodDescriptors = new ArrayList<>();