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 async iterator for paging method #199

Merged
merged 18 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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 @@ -39,6 +39,7 @@ export class BigQueryStorageClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _innerCallPromises: {[name: string]: gax.UnaryCall | gax.ServerStreamingCall | gax.ClientStreamingCall | gax.BiDiStreamingCall | Promise<gax.GRPCCall>};
auth: gax.GoogleAuth;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}, batching: {}};
innerApiCalls: {[name: string]: Function};
Expand Down Expand Up @@ -162,6 +163,7 @@ export class BigQueryStorageClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this.innerApiCalls = {};
this._innerCallPromises = {};
}

/**
Expand Down Expand Up @@ -194,9 +196,8 @@ export class BigQueryStorageClient {
// and create an API call method for each.
const bigQueryStorageStubMethods =
['createReadSession', 'readRows', 'batchCreateReadSessionStreams', 'finalizeStream', 'splitReadStream'];

for (const methodName of bigQueryStorageStubMethods) {
const innerCallPromise = this.bigQueryStorageStub.then(
this._innerCallPromises[methodName] = this.bigQueryStorageStub.then(
stub => (...args: Array<{}>) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
Expand All @@ -209,7 +210,7 @@ export class BigQueryStorageClient {
});

const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
this._innerCallPromises[methodName],
this._defaults[methodName],
this.descriptors.page[methodName] ||
this.descriptors.stream[methodName] ||
Expand Down
41 changes: 38 additions & 3 deletions baselines/disable-packing-test/src/v1beta1/echo_client.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {APICallback, Callback, CallOptions, Descriptors, ClientOptions, LROperat
import * as path from 'path';

import { Transform } from 'stream';
import { RequestType } from 'google-gax/build/src/apitypes';
import * as protos from '../../protos/protos';
import * as gapicConfig from './echo_client_config.json';

Expand All @@ -42,6 +43,7 @@ export class EchoClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _innerCallPromises: {[name: string]: gax.UnaryCall | gax.ServerStreamingCall | gax.ClientStreamingCall | gax.BiDiStreamingCall | Promise<gax.GRPCCall>};
auth: gax.GoogleAuth;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}, batching: {}};
innerApiCalls: {[name: string]: Function};
Expand Down Expand Up @@ -216,6 +218,7 @@ export class EchoClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this.innerApiCalls = {};
this._innerCallPromises = {};
}

/**
Expand Down Expand Up @@ -248,9 +251,8 @@ export class EchoClient {
// and create an API call method for each.
const echoStubMethods =
['echo', 'expand', 'collect', 'chat', 'pagedExpand', 'wait', 'block'];

for (const methodName of echoStubMethods) {
const innerCallPromise = this.echoStub.then(
this._innerCallPromises[methodName] = this.echoStub.then(
stub => (...args: Array<{}>) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
Expand All @@ -263,7 +265,7 @@ export class EchoClient {
});

const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
this._innerCallPromises[methodName],
this._defaults[methodName],
this.descriptors.page[methodName] ||
this.descriptors.stream[methodName] ||
Expand Down Expand Up @@ -726,6 +728,39 @@ export class EchoClient {
callSettings
);
}

/**
* Equivalent to {@link pagedExpand}, but returns an iterable object.
*
* for-await-of syntax is used with the iterable to recursively get response element on-demand.
*
* @param {Object} request
* The request object that will be sent.
* @param {string} request.content
* The string to expand.
* @param {number} request.pageSize
* The amount of words to returned in each page.
* @param {string} request.pageToken
* The position of the page to be returned.
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Object}
* An iterable Object that conforms to @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.
*/
pagedExpandAsync(
request?: protos.google.showcase.v1beta1.IPagedExpandRequest,
options?: gax.CallOptions):
AsyncIterable<protos.google.showcase.v1beta1.IEchoResponse>{
request = request || {};
options = options || {};
options = options || {};
const callSettings = new gax.CallSettings(options);
return this.descriptors.page.pagedExpand.asyncIterate(
this._innerCallPromises['pagedExpand'] as unknown as gax.GaxCall,
request as unknown as RequestType,
callSettings
) as AsyncIterable<protos.google.showcase.v1beta1.IEchoResponse>;
}
// --------------------
// -- Path templates --
// --------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {APICallback, Callback, CallOptions, Descriptors, ClientOptions, Paginati
import * as path from 'path';

import { Transform } from 'stream';
import { RequestType } from 'google-gax/build/src/apitypes';
import * as protos from '../../protos/protos';
import * as gapicConfig from './identity_client_config.json';

Expand All @@ -38,6 +39,7 @@ export class IdentityClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _innerCallPromises: {[name: string]: gax.UnaryCall | gax.ServerStreamingCall | gax.ClientStreamingCall | gax.BiDiStreamingCall | Promise<gax.GRPCCall>};
auth: gax.GoogleAuth;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}, batching: {}};
innerApiCalls: {[name: string]: Function};
Expand Down Expand Up @@ -178,6 +180,7 @@ export class IdentityClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this.innerApiCalls = {};
this._innerCallPromises = {};
}

/**
Expand Down Expand Up @@ -210,9 +213,8 @@ export class IdentityClient {
// and create an API call method for each.
const identityStubMethods =
['createUser', 'getUser', 'updateUser', 'deleteUser', 'listUsers'];

for (const methodName of identityStubMethods) {
const innerCallPromise = this.identityStub.then(
this._innerCallPromises[methodName] = this.identityStub.then(
stub => (...args: Array<{}>) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
Expand All @@ -225,7 +227,7 @@ export class IdentityClient {
});

const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
this._innerCallPromises[methodName],
this._defaults[methodName],
this.descriptors.page[methodName] ||
this.descriptors.stream[methodName] ||
Expand Down Expand Up @@ -675,6 +677,40 @@ export class IdentityClient {
callSettings
);
}

/**
* Equivalent to {@link listUsers}, but returns an iterable object.
*
* for-await-of syntax is used with the iterable to recursively get response element on-demand.
*
* @param {Object} request
* The request object that will be sent.
* @param {number} request.pageSize
* The maximum number of users to return. Server may return fewer users
* than requested. If unspecified, server will pick an appropriate default.
* @param {string} request.pageToken
* The value of google.showcase.v1beta1.ListUsersResponse.next_page_token
* returned from the previous call to
* `google.showcase.v1beta1.Identity\ListUsers` method.
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Object}
* An iterable Object that conforms to @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.
*/
listUsersAsync(
request?: protos.google.showcase.v1beta1.IListUsersRequest,
options?: gax.CallOptions):
AsyncIterable<protos.google.showcase.v1beta1.IUser>{
request = request || {};
options = options || {};
options = options || {};
const callSettings = new gax.CallSettings(options);
return this.descriptors.page.listUsers.asyncIterate(
this._innerCallPromises['listUsers'] as unknown as gax.GaxCall,
request as unknown as RequestType,
callSettings
) as AsyncIterable<protos.google.showcase.v1beta1.IUser>;
}
// --------------------
// -- Path templates --
// --------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {APICallback, Callback, CallOptions, Descriptors, ClientOptions, LROperat
import * as path from 'path';

import { Transform } from 'stream';
import { RequestType } from 'google-gax/build/src/apitypes';
import * as protos from '../../protos/protos';
import * as gapicConfig from './messaging_client_config.json';

Expand All @@ -41,6 +42,7 @@ export class MessagingClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _innerCallPromises: {[name: string]: gax.UnaryCall | gax.ServerStreamingCall | gax.ClientStreamingCall | gax.BiDiStreamingCall | Promise<gax.GRPCCall>};
auth: gax.GoogleAuth;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}, batching: {}};
innerApiCalls: {[name: string]: Function};
Expand Down Expand Up @@ -217,6 +219,7 @@ export class MessagingClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this.innerApiCalls = {};
this._innerCallPromises = {};
}

/**
Expand Down Expand Up @@ -249,9 +252,8 @@ export class MessagingClient {
// and create an API call method for each.
const messagingStubMethods =
['createRoom', 'getRoom', 'updateRoom', 'deleteRoom', 'listRooms', 'createBlurb', 'getBlurb', 'updateBlurb', 'deleteBlurb', 'listBlurbs', 'searchBlurbs', 'streamBlurbs', 'sendBlurbs', 'connect'];

for (const methodName of messagingStubMethods) {
const innerCallPromise = this.messagingStub.then(
this._innerCallPromises[methodName] = this.messagingStub.then(
stub => (...args: Array<{}>) => {
if (this._terminated) {
return Promise.reject('The client has already been closed.');
Expand All @@ -264,7 +266,7 @@ export class MessagingClient {
});

const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
this._innerCallPromises[methodName],
this._defaults[methodName],
this.descriptors.page[methodName] ||
this.descriptors.stream[methodName] ||
Expand Down Expand Up @@ -1156,6 +1158,40 @@ export class MessagingClient {
callSettings
);
}

/**
* Equivalent to {@link listRooms}, but returns an iterable object.
*
* for-await-of syntax is used with the iterable to recursively get response element on-demand.
*
* @param {Object} request
* The request object that will be sent.
* @param {number} request.pageSize
* The maximum number of rooms return. Server may return fewer rooms
* than requested. If unspecified, server will pick an appropriate default.
* @param {string} request.pageToken
* The value of google.showcase.v1beta1.ListRoomsResponse.next_page_token
* returned from the previous call to
* `google.showcase.v1beta1.Messaging\ListRooms` method.
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Object}
* An iterable Object that conforms to @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.
*/
listRoomsAsync(
request?: protos.google.showcase.v1beta1.IListRoomsRequest,
options?: gax.CallOptions):
AsyncIterable<protos.google.showcase.v1beta1.IRoom>{
request = request || {};
options = options || {};
options = options || {};
const callSettings = new gax.CallSettings(options);
return this.descriptors.page.listRooms.asyncIterate(
this._innerCallPromises['listRooms'] as unknown as gax.GaxCall,
request as unknown as RequestType,
callSettings
) as AsyncIterable<protos.google.showcase.v1beta1.IRoom>;
}
listBlurbs(
request: protos.google.showcase.v1beta1.IListBlurbsRequest,
options?: gax.CallOptions):
Expand Down Expand Up @@ -1298,6 +1334,50 @@ export class MessagingClient {
callSettings
);
}

/**
* Equivalent to {@link listBlurbs}, but returns an iterable object.
*
* for-await-of syntax is used with the iterable to recursively get response element on-demand.
*
* @param {Object} request
* The request object that will be sent.
* @param {string} request.parent
* The resource name of the requested room or profile whos blurbs to list.
* @param {number} request.pageSize
* The maximum number of blurbs to return. Server may return fewer
* blurbs than requested. If unspecified, server will pick an appropriate
* default.
* @param {string} request.pageToken
* The value of google.showcase.v1beta1.ListBlurbsResponse.next_page_token
* returned from the previous call to
* `google.showcase.v1beta1.Messaging\ListBlurbs` method.
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Object}
* An iterable Object that conforms to @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.
*/
listBlurbsAsync(
request?: protos.google.showcase.v1beta1.IListBlurbsRequest,
options?: gax.CallOptions):
AsyncIterable<protos.google.showcase.v1beta1.IBlurb>{
request = request || {};
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams({
'parent': request.parent || '',
});
options = options || {};
const callSettings = new gax.CallSettings(options);
return this.descriptors.page.listBlurbs.asyncIterate(
this._innerCallPromises['listBlurbs'] as unknown as gax.GaxCall,
request as unknown as RequestType,
callSettings
) as AsyncIterable<protos.google.showcase.v1beta1.IBlurb>;
}
// --------------------
// -- Path templates --
// --------------------
Expand Down
Loading