Skip to content

Commit

Permalink
feat: support streaming method
Browse files Browse the repository at this point in the history
* feat: streaming methods

* fix: reslove review feedback

* fix: baseline test result
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Aug 29, 2019
1 parent 6f91152 commit eb563e9
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 32 deletions.
5 changes: 2 additions & 3 deletions templates/typescript_gapic/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
"!build/src/**/*.map"
],
"dependencies": {
"google-gax": "^1.3.0"
"google-gax": "^1.5.0"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
"@types/through2": "^2.0.34",
"gts": "^0.9.0",
"mocha": "^6.0.0",
"typescript": "~3.5.3",
"google-gax": "^1.3.0"
"typescript": "~3.5.3"
},
"scripts": {
"lint": "gts check",
Expand Down
77 changes: 61 additions & 16 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,24 @@ export class {{ service.name }}Client {
{%- endfor -%}
];

for (const methodName of echoStubMethods) {
for (const methodName of {{ service.name.toCamelCase() }}StubMethods) {
const innerCallPromise = {{ service.name.toCamelCase() }}Stub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gax.createApiCall(
{{ service.name.toCamelCase() }}Stub.then(
// @ts-ignore
stub => (...args) => {
stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
}),
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]);
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);
}
}

/**
* The DNS address for this API service.
*/
Expand Down Expand Up @@ -271,9 +272,7 @@ export class {{ service.name }}Client {
protos{{ method.outputInterface }},
protos{{ method.inputInterface }}|undefined, {}|undefined
]>|void {
{%- if (method.streaming == 'SERVER_STREAMING' or method.streaming == 'NON_STREAMING') %}
request = request || {};
{%- endif %}
let options = optionsOrCallback;
if (typeof options === 'function' && callback === undefined) {
callback = options;
Expand All @@ -283,4 +282,50 @@ export class {{ service.name }}Client {
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
{% for method in service.streaming %}
{%- if method.serverStreaming and method.clientStreaming %}
{{ method.name.toCamelCase() }}(
options?: gax.CallOptions):
gax.BiDiStreamingCall{
options = options || {};
return this._innerApiCalls.{{ method.name.toCamelCase() }}(options);
}
{%- elif method.serverStreaming %}
{{ method.name.toCamelCase() }}(
request?: protos{{ method.inputInterface }},
options?: gax.CallOptions):
gax.ServerStreamingCall{
request = request || {};
options = options || {};
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options);
}
{%- elif method.clientStreaming %}
{{ method.name.toCamelCase() }}(
options: gax.CallOptions,
callback: Callback<
protos{{ method.outputInterface }},
protos{{ method.inputInterface }}|undefined, {}|undefined>):
gax.ClientStreamingCall;
{{ method.name.toCamelCase() }}(
callback: Callback<
protos{{ method.outputInterface }},
protos{{ method.inputInterface }}|undefined, {}|undefined>):
gax.ClientStreamingCall;
{{ method.name.toCamelCase() }}(
optionsOrCallback: gax.CallOptions|Callback<
protos{{ method.outputInterface }},
protos{{ method.inputInterface }}|undefined, {}|undefined>,
callback?: Callback<
protos{{ method.outputInterface }},
protos{{ method.inputInterface }}|undefined, {}|undefined>):
gax.ClientStreamingCall {
if (optionsOrCallback instanceof Function && callback === undefined) {
callback = optionsOrCallback;
optionsOrCallback = {};
}
optionsOrCallback = optionsOrCallback || {};
return this._innerApiCalls.{{ method.name.toCamelCase() }}(null, optionsOrCallback, callback);
}
{%- endif %}
{% endfor %}
}
72 changes: 59 additions & 13 deletions typescript/test/testdata/echo_client_baseline.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,23 @@ export class EchoClient {
['echo', 'expand', 'collect', 'chat', 'pagedExpand', 'wait'];

for (const methodName of echoStubMethods) {
const innerCallPromise = echoStub.then(
stub => (...args: Array<{}>) => {
return stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
});

this._innerApiCalls[methodName] = gax.createApiCall(
echoStub.then(
// @ts-ignore
stub => (...args) => {
stub[methodName].apply(stub, args);
},
(err: Error|null|undefined) => () => {
throw err;
}),
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]);
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
);
}
}

/**
* The DNS address for this API service.
*/
Expand Down Expand Up @@ -219,6 +220,7 @@ export class EchoClient {
protos.google.showcase.v1beta1.IEchoResponse,
protos.google.showcase.v1beta1.IEchoRequest|undefined, {}|undefined
]>|void {
request = request || {};
let options = optionsOrCallback;
if (typeof options === 'function' && callback === undefined) {
callback = options;
Expand All @@ -227,4 +229,48 @@ export class EchoClient {
options = options || {};
return this._innerApiCalls.echo(request, options, callback);
}

expand(
request?: protos.google.showcase.v1beta1.IExpandRequest,
options?: gax.CallOptions):
gax.ServerStreamingCall{
request = request || {};
options = options || {};
return this._innerApiCalls.expand(request, options);
}

collect(
options: gax.CallOptions,
callback: Callback<
protos.google.showcase.v1beta1.IEchoResponse,
protos.google.showcase.v1beta1.IEchoRequest|undefined, {}|undefined>):
gax.ClientStreamingCall;
collect(
callback: Callback<
protos.google.showcase.v1beta1.IEchoResponse,
protos.google.showcase.v1beta1.IEchoRequest|undefined, {}|undefined>):
gax.ClientStreamingCall;
collect(
optionsOrCallback: gax.CallOptions|Callback<
protos.google.showcase.v1beta1.IEchoResponse,
protos.google.showcase.v1beta1.IEchoRequest|undefined, {}|undefined>,
callback?: Callback<
protos.google.showcase.v1beta1.IEchoResponse,
protos.google.showcase.v1beta1.IEchoRequest|undefined, {}|undefined>):
gax.ClientStreamingCall {
if (optionsOrCallback instanceof Function && callback === undefined) {
callback = optionsOrCallback;
optionsOrCallback = {};
}
optionsOrCallback = optionsOrCallback || {};
return this._innerApiCalls.collect(null, optionsOrCallback, callback);
}

chat(
options?: gax.CallOptions):
gax.BiDiStreamingCall{
options = options || {};
return this._innerApiCalls.chat(options);
}

}

0 comments on commit eb563e9

Please sign in to comment.