Skip to content

Commit

Permalink
feat: support additional_bindings in google.api.http option (#223)
Browse files Browse the repository at this point in the history
* feat: support additional_bindings in google.api.http option

* fix: pr feedback
  • Loading branch information
alexander-fenster committed Feb 3, 2020
1 parent 1e00a2d commit 245deef
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 82 deletions.
23 changes: 20 additions & 3 deletions templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,32 @@ request.{{ oneComment.paramName.toCamelCase() }}
{{- arr[arr.length - 1] -}}
{%- endmacro -%}

{%- macro buildHeaderRequestParam(method) -%}
options = options || {};
{%- if method.headerRequestParams.length > 0 %}
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
{%- for requestParam in method.headerRequestParams %}
'{{ requestParam.toSnakeCaseString(".") }}': request.{{ requestParam.toCamelCaseString("!.") }} || '',
{%- endfor %}
});
{%- endif %}
{%- endmacro -%}

{%- macro initRequestWithHeaderParam(method) -%}
const request: {{ toInterface(method.inputInterface) }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- if method.headerRequestParams.length > 0 %}
{%- for requestParam in method.headerRequestParams %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{%- for field in requestParam.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0].toCamelCase() }} = '';
{{ chain }}.{{ requestParam.slice(-1)[0].toCamelCase() }} = '';
{%- endfor %}
{%- endif %}
{%- endmacro -%}

Expand Down
33 changes: 3 additions & 30 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,7 @@ export class {{ service.name }}Client {
else {
options = optionsOrCallback as gax.CallOptions;
}
options = options || {};
{%- if method.headerRequestParams.length > 0 %}
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toSnakeCaseString(".") }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
{{ util.buildHeaderRequestParam(method) }}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down Expand Up @@ -480,16 +471,7 @@ export class {{ service.name }}Client {
else {
options = optionsOrCallback as gax.CallOptions;
}
options = options || {};
{%- if method.headerRequestParams.length > 0 %}
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
{{ util.buildHeaderRequestParam(method) }}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down Expand Up @@ -536,16 +518,7 @@ export class {{ service.name }}Client {
else {
options = optionsOrCallback as gax.CallOptions;
}
options = options || {};
{%- if method.headerRequestParams.length > 0 %}
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'{{ method.headerRequestParams.toString().toSnakeCase() }}': request.{{ method.headerRequestParams.toCamelCaseString("!.") }} || '',
});
{%- endif %}
{{ util.buildHeaderRequestParam(method) }}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
// Mock request
{{ util.initRequestWithHeaderParam(method) }}
// Mock response
const expectedResponse = {};
const expectedResponse = {response: 'data'};
// Mock Grpc layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
Expand All @@ -380,7 +380,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
stream.write(expectedResponse);
});
});
{%- endfor %}
Expand Down
48 changes: 42 additions & 6 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ interface MethodDescriptorProto
retryableCodesName: string;
retryParamsName: string;
timeoutMillis?: number;
headerRequestParams: string[];
// headerRequestParams: if we need to pass "request.foo" and "request.bar"
// into x-goog-request-params header, the array will contain
// [ ['request', 'foo'], ['request', 'bar']]
headerRequestParams: string[][];
}

export class RetryableCodeMap {
Expand Down Expand Up @@ -336,7 +339,9 @@ function pagingResponseType(
return plugin.google.protobuf.FieldDescriptorProto.Type[field.type];
}

export function getHeaderParams(rule: plugin.google.api.IHttpRule): string[] {
export function getSingleHeaderParam(
rule: plugin.google.api.IHttpRule
): string[] {
const message =
rule.post || rule.delete || rule.get || rule.put || rule.patch;
if (message) {
Expand Down Expand Up @@ -459,13 +464,44 @@ function augmentMethod(
if (method.methodConfig.timeout) {
method.timeoutMillis = milliseconds(method.methodConfig.timeout);
}
if (method.options?.['.google.api.http']) {
const httpRule = method.options['.google.api.http'];
method.headerRequestParams = getHeaderParams(httpRule);
} else method.headerRequestParams = [];
method.headerRequestParams = getHeaderRequestParams(
method.options?.['.google.api.http']
);
return method;
}

export function getHeaderRequestParams(
httpRule: plugin.google.api.IHttpRule | null | undefined
) {
if (!httpRule) {
return [];
}
const params: string[][] = [];
params.push(getSingleHeaderParam(httpRule));

httpRule.additionalBindings = httpRule.additionalBindings ?? [];
params.push(
...httpRule.additionalBindings.map(binding => getSingleHeaderParam(binding))
);

// de-dup result array
const used = new Set();
const result: string[][] = [];
for (const param of params) {
if (param.length === 0) {
continue;
}
const joined = param.join('.');
if (used.has(joined)) {
continue;
}
used.add(joined);
result.push(param);
}

return result;
}

function augmentService(
messages: MessagesMap,
packageName: string,
Expand Down

0 comments on commit 245deef

Please sign in to comment.