Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Python: Add routing header #1658

Merged
merged 6 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@

import com.google.api.codegen.config.DiscoveryMethodModel;
import com.google.api.codegen.config.FieldModel;
import com.google.api.codegen.config.GapicMethodConfig;
import com.google.api.codegen.config.MethodConfig;
import com.google.api.codegen.config.MethodModel;
import com.google.api.codegen.config.PageStreamingConfig;
import com.google.api.codegen.config.ProtoField;
import com.google.api.codegen.config.TransportProtocol;
import com.google.api.codegen.config.VisibilityConfig;
import com.google.api.codegen.discovery.Method;
import com.google.api.codegen.viewmodel.ApiCallSettingsView;
import com.google.api.codegen.viewmodel.ApiCallableImplType;
import com.google.api.codegen.viewmodel.ApiCallableView;
import com.google.api.codegen.viewmodel.HeaderRequestParamView;
import com.google.api.codegen.viewmodel.HttpMethodView;
import com.google.api.codegen.viewmodel.LongRunningOperationDetailView;
import com.google.api.codegen.viewmodel.MethodDescriptorView;
import com.google.api.codegen.viewmodel.RetryCodesDefinitionView;
import com.google.api.codegen.viewmodel.RetryParamsDefinitionView;
import com.google.api.codegen.viewmodel.ServiceMethodType;
import com.google.api.tools.framework.model.Field;
import com.google.api.tools.framework.model.MessageType;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -48,11 +42,13 @@ public class ApiCallableTransformer {
private final BatchingTransformer batchingTransformer;
private final RetryDefinitionsTransformer retryDefinitionsTransformer;
private final LongRunningTransformer lroTransformer;
private final HeaderRequestParamTransformer headerRequestParamTransformer;

public ApiCallableTransformer() {
this.batchingTransformer = new BatchingTransformer();
this.retryDefinitionsTransformer = new RetryDefinitionsTransformer();
this.lroTransformer = new LongRunningTransformer();
this.headerRequestParamTransformer = new HeaderRequestParamTransformer();
}

public List<ApiCallableView> generateStaticLangApiCallables(InterfaceContext context) {
Expand Down Expand Up @@ -116,7 +112,8 @@ private ApiCallableView generateMainApiCallable(MethodContext context) {
apiCallableBuilder.memberName(namer.getSettingsMemberName(method));
apiCallableBuilder.settingsFunctionName(namer.getSettingsFunctionName(method));
apiCallableBuilder.grpcClientVarName(namer.getReroutedGrpcClientVarName(methodConfig));
apiCallableBuilder.headerRequestParams(getHeaderRequestParams(context));
apiCallableBuilder.headerRequestParams(
headerRequestParamTransformer.generateHeaderRequestParams(context));

setCommonApiCallableFields(context, apiCallableBuilder);

Expand Down Expand Up @@ -380,62 +377,11 @@ private MethodDescriptorView generateMethodDescriptor(MethodContext context) {
methodDescriptorBuilder.fullServiceName(context.getTargetInterface().getFullName());
methodDescriptorBuilder.transportSettingsVar(namer.getTransportSettingsVar(method));

methodDescriptorBuilder.headerRequestParams(getHeaderRequestParams(context));
methodDescriptorBuilder.headerRequestParams(
headerRequestParamTransformer.generateHeaderRequestParams(context));

methodDescriptorBuilder.httpMethod(generateHttpFields(context));

return methodDescriptorBuilder.build();
}

public static List<HeaderRequestParamView> getHeaderRequestParams(MethodContext context) {
if (!context.getProductConfig().getTransportProtocol().equals(TransportProtocol.GRPC)) {
return ImmutableList.of();
}

GapicMethodConfig methodConfig = (GapicMethodConfig) context.getMethodConfig();
com.google.api.tools.framework.model.Method method = methodConfig.getMethod();
SurfaceNamer namer = context.getNamer();
if (method.getInputType() == null || !method.getInputType().isMessage()) {
return ImmutableList.of();
}
ImmutableList.Builder<HeaderRequestParamView> headerRequestParams = ImmutableList.builder();
MessageType inputMessageType = method.getInputType().getMessageType();
for (String headerRequestParam : methodConfig.getHeaderRequestParams()) {
headerRequestParams.add(getHeaderRequestParam(headerRequestParam, inputMessageType, namer));
}

return headerRequestParams.build();
}

private static HeaderRequestParamView getHeaderRequestParam(
String headerRequestParam, MessageType inputMessageType, SurfaceNamer namer) {
String[] fieldNameTokens = headerRequestParam.split("\\.");
ImmutableList.Builder<String> gettersChain = ImmutableList.builder();

MessageType subMessageType = inputMessageType;
for (String fieldNameToken : fieldNameTokens) {
Field matchingField = subMessageType.lookupField(fieldNameToken);
if (matchingField == null) {
throw new IllegalArgumentException(
"Unknown field name token '"
+ fieldNameToken
+ "' in header request param '"
+ headerRequestParam
+ "'");
}

String matchingFieldGetter = namer.getFieldGetFunctionName(new ProtoField(matchingField));
gettersChain.add(matchingFieldGetter);
if (matchingField.getType() != null && matchingField.getType().isMessage()) {
subMessageType = matchingField.getType().getMessageType();
}
}

HeaderRequestParamView.Builder headerParam =
HeaderRequestParamView.newBuilder()
.fullyQualifiedName(headerRequestParam)
.gettersChain(gettersChain.build());

return headerParam.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class DynamicLangApiMethodTransformer {
private final ApiMethodParamTransformer apiMethodParamTransformer;
private final InitCodeTransformer initCodeTransformer;
private final LongRunningTransformer lroTransformer = new LongRunningTransformer();
private final HeaderRequestParamTransformer headerRequestParamTransformer =
new HeaderRequestParamTransformer();
private final PageStreamingTransformer pageStreamingTransformer = new PageStreamingTransformer();

public DynamicLangApiMethodTransformer(ApiMethodParamTransformer apiMethodParamTransformer) {
Expand Down Expand Up @@ -139,6 +141,8 @@ public OptionalArrayMethodView generateMethod(
: null);

apiMethod.oneofParams(context.getMethodConfig().getOneofNames(namer));
apiMethod.headerRequestParams(
headerRequestParamTransformer.generateHeaderRequestParams(context));

return apiMethod.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Copyright 2017 Google LLC
*
* 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
*
* http://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 com.google.api.codegen.transformer;

import com.google.api.codegen.config.GapicMethodConfig;
import com.google.api.codegen.config.ProtoField;
import com.google.api.codegen.config.TransportProtocol;
import com.google.api.codegen.viewmodel.HeaderRequestParamView;
import com.google.api.tools.framework.model.Field;
import com.google.api.tools.framework.model.MessageType;
import com.google.api.tools.framework.model.Method;
import com.google.common.collect.ImmutableList;
import java.util.List;

public class HeaderRequestParamTransformer {
public List<HeaderRequestParamView> generateHeaderRequestParams(MethodContext context) {

This comment was marked as spam.

This comment was marked as spam.

if (!context.getProductConfig().getTransportProtocol().equals(TransportProtocol.GRPC)) {
return ImmutableList.of();
}

GapicMethodConfig methodConfig = (GapicMethodConfig) context.getMethodConfig();
Method method = methodConfig.getMethod();
SurfaceNamer namer = context.getNamer();
if (method.getInputType() == null || !method.getInputType().isMessage()) {
return ImmutableList.of();
}
ImmutableList.Builder<HeaderRequestParamView> headerRequestParams = ImmutableList.builder();
MessageType inputMessageType = method.getInputType().getMessageType();
for (String headerRequestParam : methodConfig.getHeaderRequestParams()) {
headerRequestParams.add(
generateHeaderRequestParam(headerRequestParam, inputMessageType, namer));
}

return headerRequestParams.build();
}

private HeaderRequestParamView generateHeaderRequestParam(
String headerRequestParam, MessageType inputMessageType, SurfaceNamer namer) {
String[] fieldNameTokens = headerRequestParam.split("\\.");
ImmutableList.Builder<String> gettersChain = ImmutableList.builder();

MessageType subMessageType = inputMessageType;
for (String fieldNameToken : fieldNameTokens) {
Field matchingField = subMessageType.lookupField(fieldNameToken);
if (matchingField == null) {
throw new IllegalArgumentException(
"Unknown field name token '"
+ fieldNameToken
+ "' in header request param '"
+ headerRequestParam
+ "'");
}

String matchingFieldGetter = namer.getFieldGetFunctionName(new ProtoField(matchingField));
gettersChain.add(matchingFieldGetter);
if (matchingField.getType() != null && matchingField.getType().isMessage()) {
subMessageType = matchingField.getType().getMessageType();
}
}

HeaderRequestParamView.Builder headerParam =
HeaderRequestParamView.newBuilder()
.fullyQualifiedName(headerRequestParam)
.gettersChain(gettersChain.build());

return headerParam.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class StaticLangApiMethodTransformer {
private final LongRunningTransformer lroTransformer = new LongRunningTransformer();
private final StaticLangResourceObjectTransformer resourceObjectTransformer =
new StaticLangResourceObjectTransformer();
private final HeaderRequestParamTransformer headerRequestParamTransformer =
new HeaderRequestParamTransformer();

public StaticLangApiMethodView generatePagedFlattenedMethod(MethodContext context) {
return generatePagedFlattenedMethod(context, Collections.<ParamWithSimpleDoc>emptyList());
Expand Down Expand Up @@ -489,7 +491,8 @@ private void setCommonFields(
} else {
methodViewBuilder.hasReturnValue(method.hasReturnValue());
}
methodViewBuilder.headerRequestParams(ApiCallableTransformer.getHeaderRequestParams(context));
methodViewBuilder.headerRequestParams(
headerRequestParamTransformer.generateHeaderRequestParams(context));
}

protected void setServiceResponseTypeName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public boolean hasApiVersion() {

public abstract Iterable<Iterable<String>> oneofParams();

public abstract List<HeaderRequestParamView> headerRequestParams();

public static Builder newBuilder() {
return new AutoValue_OptionalArrayMethodView.Builder();
}
Expand Down Expand Up @@ -182,6 +184,8 @@ public abstract Builder optionalRequestObjectParamsNoPageToken(

public abstract Builder localPackageName(String val);

public abstract Builder headerRequestParams(List<HeaderRequestParamView> val);

public abstract OptionalArrayMethodView build();
}
}
46 changes: 42 additions & 4 deletions src/main/resources/com/google/api/codegen/py/main.snip
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

@private callLine(apiMethod)
@if apiMethod.isLongRunningOperation
operation = self._{@apiMethod.name}({@apiMethod.requestVariableName}, retry=retry, timeout=timeout)
operation = self._{@apiMethod.name}({@apiMethod.requestVariableName}{@optionalParams(apiMethod)})
return google.api_core.operation.from_gapic(
operation,
self.operations_client,
Expand All @@ -252,14 +252,14 @@
@switch apiMethod.type
@case "OptionalArrayMethod"
@if apiMethod.hasReturnValue
return self._{@apiMethod.name}({@apiMethod.requestVariableName}, retry=retry, timeout=timeout)
return self._{@apiMethod.name}({@apiMethod.requestVariableName}{@optionalParams(apiMethod)})
@else
self._{@apiMethod.name}({@apiMethod.requestVariableName}, retry=retry, timeout=timeout)
self._{@apiMethod.name}({@apiMethod.requestVariableName}{@optionalParams(apiMethod)})
@end
@case "PagedOptionalArrayMethod"
iterator = google.api_core.page_iterator.GRPCIterator(
client=None,
method=functools.partial(self._{@apiMethod.name}, retry=retry, timeout=timeout),
method=functools.partial(self._{@apiMethod.name}{@optionalParams(apiMethod)}),
request={@apiMethod.requestVariableName},
items_field='{@apiMethod.pageStreamingView.resourcesFieldName}',
request_token_field='{@apiMethod.pageStreamingView.requestTokenFieldName}',
Expand All @@ -271,6 +271,14 @@
@end
@end

@private optionalParams(apiMethod)
@if apiMethod.headerRequestParams
, retry=retry, timeout=timeout, metadata=metadata
@else
, retry=retry, timeout=timeout
@end
@end

@private flattenedMethod(apiMethod)
def {@apiMethod.name}(
{@renderMethodParams(apiMethod.methodParams)}):
Expand All @@ -297,6 +305,14 @@
request = {@apiMethod.requestTypeName}()
@end
@end

@if apiMethod.headerRequestParams

metadata = [
(b'x-google-request-params',
{@routingHeader(apiMethod.headerRequestParams)})]

@end
@end
{@callLine(apiMethod)}
@end
Expand Down Expand Up @@ -329,3 +345,25 @@
{@param.keyName}={@param.name}
@end
@end

@private routingHeader(params)
b'{@routingHeaderFmtStr(params)}'.format({@routingHeaderFmtArgs(params)})

This comment was marked as spam.

@end

@private routingHeaderFmtStr(params)
@join param : params on "&"
{@param.fullyQualifiedName}={}
@end
@end

@private routingHeaderFmtArgs(params)
@join param : params on ", "
{@routingHeaderGetters(param.gettersChain)}
@end
@end

@private routingHeaderGetters(getters)
@join getter : getters on "."
{@getter}
@end
@end
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,12 @@ class LibraryServiceClient(object):
request = library_pb2.CreateBookRequest(
name=name,
book=book)
return self._create_book(request, retry=retry, timeout=timeout)

metadata = [
(b'x-google-request-params',

This comment was marked as spam.

b'name={}&book.read={}'.format(name, book.read))]

return self._create_book(request, retry=retry, timeout=timeout, metadata=metadata)

def publish_series(
self,
Expand Down

This file was deleted.

This file was deleted.