Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support google.api.http annotation #83

Merged
merged 11 commits into from
Oct 30, 2019
54 changes: 45 additions & 9 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,24 @@ export class {{ service.name }}Client {
protosTypes{{ method.inputInterface }}|undefined, {}|undefined
]>|void {
request = request || {};
let options = optionsOrCallback;
if (typeof options === 'function' && callback === undefined) {
callback = options;
let options: gax.CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
}
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().toCamelCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot() }} || '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be snake_case for the left part and camelCase for the right part. Left part is basically taken from proto as is (since it gets passed to gRPC server as a header), right part is converted to camelCase because it's how protobufjs processes keys with underscore.

Example here: https://github.com/googleapis/nodejs-logging/blob/dcb48172656de8b449a2c05e8cbe999ebaac90bf/src/v2/metrics_service_v2_client.js#L460

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if we discuss it, it would be nice to have it covered by a baseline test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds great! but do you mean unit test? we havekeymanager translate, text-to-speechAPI which respects this routing header, do we want to add one more baseline test, or just test this functionality in the unit/? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add logging? (in a separate PR)

});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down Expand Up @@ -442,12 +454,24 @@ export class {{ service.name }}Client {
protosTypes{{ method.outputInterface }}|undefined, {}|undefined
]>|void {
request = request || {};
let options = optionsOrCallback;
if (typeof options === 'function' && callback === undefined) {
callback = options;
let options: gax.CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
}
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().toCamelCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot() }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down Expand Up @@ -486,12 +510,24 @@ export class {{ service.name }}Client {
protosTypes{{ method.outputInterface }}
]>|void {
request = request || {};
let options = optionsOrCallback;
if (typeof options === 'function' && callback === undefined) {
callback = options;
let options: gax.CallOptions;
if (typeof optionsOrCallback === 'function' && callback === undefined) {
callback = optionsOrCallback;
options = {};
}
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().toCamelCase() }}': request.{{ method.headerRequestParams.camelCaseBeforeDot() }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down
54 changes: 48 additions & 6 deletions templates/typescript_gapic/test/gapic-$service-$version.ts.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% import "../_license.njk" as license -%}
{{license.license()}}
'use strict';

import * as protosTypes from '../protos/protos';
const assert = require('assert');
const {{ service.name.toLowerCase() }}Module = require('../src');
{% if (service.streaming.length > 0) %}
Expand Down Expand Up @@ -127,7 +127,16 @@ describe('{{ service.name }}Client', () => {
projectId: 'bogus',
});
// Mock request
const request = {};
const request: protosTypes{{ method.inputInterface }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{%- endif %}

// Mock response
const expectedResponse = {};
// Mock gRPC layer
Expand All @@ -149,7 +158,16 @@ describe('{{ service.name }}Client', () => {
projectId: 'bogus',
});
// Mock request
const request = {};
const request: protosTypes{{ method.inputInterface }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{%- endif %}

// Mock response
const expectedResponse = {};
// Mock gRPC layer
Expand All @@ -175,7 +193,15 @@ describe('{{ service.name }}Client', () => {
projectId: 'bogus',
});
// Mock request
const request = {};
const request: protosTypes{{ method.inputInterface }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{%- endif %}
// Mock response
const expectedResponse = {};
// Mock gRPC layer
Expand All @@ -200,7 +226,15 @@ describe('{{ service.name }}Client', () => {
projectId: 'bogus',
});
// Mock request
const request = {};
const request: protosTypes{{ method.inputInterface }} = {};
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{%- endif %}
// Mock response
const expectedResponse = {};
// Mock gRPC layer
Expand Down Expand Up @@ -320,7 +354,15 @@ describe('{{ service.name }}Client', () => {
projectId: 'bogus',
});
// Mock request
const request = {};
const request: protosTypes{{ method.inputInterface }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
{{ chain }}.{{ field.toCamelCase() }} = {};
{%- set chain = chain + "." + field.toCamelCase() -%}
{%- endfor %}
{{ chain }}.{{ method.headerRequestParams.slice(-1)[0] }} = '';
{%- endif %}
// Mock response
const expectedResponse = {};
// Mock Grpc layer
Expand Down
4 changes: 4 additions & 0 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ interface String {
toKebabCase(): string;
toSnakeCase(): string;
}

interface Array<T> {
camelCaseBeforeDot(): string;
}
15 changes: 15 additions & 0 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface MethodDescriptorProto
retryableCodesName: string;
retryParamsName: string;
timeoutMillis?: number;
headerRequestParams: string[];
}

export class RetryableCodeMap {
Expand Down Expand Up @@ -288,6 +289,16 @@ function toLRInterface(type: string, inputType: string) {
return inputType.replace(/\.([^.]+)$/, '.I' + type);
}

export function getHeaderParms(rule: plugin.google.api.IHttpRule): string[] {
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
const message =
rule.post || rule.delete || rule.get || rule.put || rule.patch;
if (message) {
const res = message.match(/{(.*?)(?==)/);
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
return res && res[0] ? res[0].split('.') : [];
}
return [];
}

function getMethodConfig(
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig,
serviceName: string,
Expand Down Expand Up @@ -382,6 +393,10 @@ function augmentMethod(
if (method.methodConfig.timeout) {
method.timeoutMillis = milliseconds(method.methodConfig.timeout);
}
if (method.options && method.options['.google.api.http']) {
const httpRule = method.options['.google.api.http'];
method.headerRequestParams = getHeaderParms(httpRule);
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
}
return method;
}

Expand Down
8 changes: 8 additions & 0 deletions typescript/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,11 @@ String.prototype.toSnakeCase = function(this: string): string {
}
return words.join('_');
};

Array.prototype.camelCaseBeforeDot = function(this: string[]): string {
if (this.length <= 1) {
return this.toString().toCamelCase();
}
const res = this.slice(0, -1).map(w => w.toCamelCase());
return res.join('!.') + '!.' + this[this.length - 1];
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
};
Loading