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 }}': request.{{ method.headerRequestParams.CamelCaseBeforeDot() }} || '',
});
{%- 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 }}': 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 }}': request.{{ method.headerRequestParams.CamelCaseBeforeDot() }} || '',
});
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
Expand Down
1 change: 1 addition & 0 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ interface String {
toPascalCase(): string;
toKebabCase(): string;
toSnakeCase(): string;
CamelCaseBeforeDot(): string;
}
22 changes: 22 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,23 @@ function toLRInterface(type: string, inputType: string) {
return inputType.replace(/\.([^.]+)$/, '.I' + type);
}

function getHeaderParms(rule: plugin.google.api.IHttpRule): string {
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
const message = rule.post
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
? rule.post
: rule.delete
? rule.delete
: rule.get
? rule.get
: rule.put
? 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] : '';
}
return '';
}

function getMethodConfig(
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig,
serviceName: string,
Expand Down Expand Up @@ -382,6 +400,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
13 changes: 13 additions & 0 deletions typescript/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ String.prototype.toSnakeCase = function(this: string): string {
}
return words.join('_');
};

String.prototype.CamelCaseBeforeDot = function(this: string): string{
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
const words = this.split('.');
const res:string[] = [];
for(var i = 0; i < words.length - 1; i++){
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
res.push(words[i].toCamelCase());
}
if(res.length > 0){
res.push(words[words.length - 1]);
return res.join('!.');
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
}
else return this.toCamelCase();
}
Loading