Skip to content

Commit

Permalink
feat: support pagestreaming method (#113)
Browse files Browse the repository at this point in the history
* add pagestreaming method, unit test, integration test

* only import transform with page method

* update gax version

* dummy commit

* update typescript version in test app
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Nov 8, 2019
1 parent 4909e3c commit ba9891b
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"file-system": "^2.2.2",
"fs-extra": "^8.1.0",
"get-stdin": "^7.0.0",
"google-gax": "^1.7.5",
"google-gax": "^1.8.0",
"nunjucks": "^3.1.3",
"object-hash": "^2.0.0",
"protobufjs": "^6.8.8",
Expand Down
2 changes: 1 addition & 1 deletion templates/typescript_gapic/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ limitations under the License.
"build/protos"
],
"dependencies": {
"google-gax": "^1.7.5"
"google-gax": "^1.8.0"
},
"devDependencies": {
"@types/node": "^12.0.0",
Expand Down
17 changes: 16 additions & 1 deletion templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ limitations under the License.

import * as gax from 'google-gax';
import * as path from 'path';

{% if (service.paging.length > 0) %}
import { Transform } from 'stream';
{%- endif %}
import * as protosTypes from '../../protos/protos';
import * as gapicConfig from './{{ service.name.toSnakeCase() }}_client_config.json';

Expand Down Expand Up @@ -566,6 +568,19 @@ export class {{ service.name }}Client {
{%- endif %}
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}

{{ method.name.toCamelCase() }}Stream(
request?: protosTypes{{ util.toInterface(method.inputInterface) }},
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.{{ method.name.toCamelCase() }}.createStream(
this._innerApiCalls.{{ method.name.toCamelCase() }} as gax.GaxCall,
request,
callSettings
);
}
{%- endfor %}
{%- if (service.pathTemplates.length > 0) %}
// --------------------
Expand Down
24 changes: 24 additions & 0 deletions templates/typescript_gapic/test/gapic-$service-$version.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,29 @@ describe('{{ service.name }}Client', () => {
});
});
});
describe('{{ method.name.toCamelCase() }}Stream', () => {
it('invokes {{ method.name.toCamelCase() }}Stream without error', done => {
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Mock request
{{ util.initRequestWithHeaderParam(method) }}
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
const stream = client.{{ method.name.toCamelCase() }}Stream(request, {}).on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
});
});
{%- endfor %}
});
23 changes: 23 additions & 0 deletions typescript/test/test_application_js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function runTest(client, opts) {
testChat(client);
}
testPagedExpand(client);
testPagedExpandStream(client);
testWait(client);
}

Expand Down Expand Up @@ -98,6 +99,28 @@ function testPagedExpand(client) {
});
}

function testPagedExpandStream(client) {
it('pagedExpand with streaming', async () => {
const words = ['I', 'did', 'not', 'even', 'know', 'it', 'works'];
const request = {
content: words.join(' '),
pageSize: 2,
};
const result = await new Promise((resolve, reject) => {
const stream = client.pagedExpandStream(request);
const result = [];
stream.on('data', response => {
result.push(response.content);
});
stream.on('end', () => {
resolve(result);
});
stream.on('error', reject);
});
assert.deepStrictEqual(words, result);
});
}

function testCollect(client) {
it('collect', async () => {
const words = ['nobody', 'ever', 'reads', 'test', 'input'];
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/test_application_ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"gts": "^1.1.0",
"typescript": "~3.5.0",
"typescript": "~3.7.0",
"@types/node": "^10.0.3",
"@types/mocha": "^5.2.7",
"mocha": "^6.2.0",
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/testdata/keymanager/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.7.5"
"google-gax": "^1.8.0"
},
"devDependencies": {
"@types/node": "^12.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import * as gax from 'google-gax';
import * as path from 'path';

import { Transform } from 'stream';
import * as protosTypes from '../../protos/protos';
import * as gapicConfig from './key_management_service_client_config.json';

Expand Down Expand Up @@ -1631,6 +1632,19 @@ export class KeyManagementServiceClient {
'parent': request.parent || '',
});
return this._innerApiCalls.listKeyRings(request, options, callback);
}

listKeyRingsStream(
request?: protosTypes.google.cloud.kms.v1.IListKeyRingsRequest,
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.listKeyRings.createStream(
this._innerApiCalls.listKeyRings as gax.GaxCall,
request,
callSettings
);
}
listCryptoKeys(
request: protosTypes.google.cloud.kms.v1.IListCryptoKeysRequest,
Expand Down Expand Up @@ -1716,6 +1730,19 @@ export class KeyManagementServiceClient {
'parent': request.parent || '',
});
return this._innerApiCalls.listCryptoKeys(request, options, callback);
}

listCryptoKeysStream(
request?: protosTypes.google.cloud.kms.v1.IListCryptoKeysRequest,
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.listCryptoKeys.createStream(
this._innerApiCalls.listCryptoKeys as gax.GaxCall,
request,
callSettings
);
}
listCryptoKeyVersions(
request: protosTypes.google.cloud.kms.v1.IListCryptoKeyVersionsRequest,
Expand Down Expand Up @@ -1802,6 +1829,19 @@ export class KeyManagementServiceClient {
'parent': request.parent || '',
});
return this._innerApiCalls.listCryptoKeyVersions(request, options, callback);
}

listCryptoKeyVersionsStream(
request?: protosTypes.google.cloud.kms.v1.IListCryptoKeyVersionsRequest,
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.listCryptoKeyVersions.createStream(
this._innerApiCalls.listCryptoKeyVersions as gax.GaxCall,
request,
callSettings
);
}
listImportJobs(
request: protosTypes.google.cloud.kms.v1.IListImportJobsRequest,
Expand Down Expand Up @@ -1885,5 +1925,18 @@ export class KeyManagementServiceClient {
'parent': request.parent || '',
});
return this._innerApiCalls.listImportJobs(request, options, callback);
}

listImportJobsStream(
request?: protosTypes.google.cloud.kms.v1.IListImportJobsRequest,
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.listImportJobs.createStream(
this._innerApiCalls.listImportJobs as gax.GaxCall,
request,
callSettings
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,30 @@ describe('KeyManagementServiceClient', () => {
});
});
});
describe('listKeyRingsStream', () => {
it('invokes listKeyRingsStream without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Mock request
const request: protosTypes.google.cloud.kms.v1.IListKeyRingsRequest = {};
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.listKeyRings = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
const stream = client.listKeyRingsStream(request, {}).on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
});
});
describe('listCryptoKeys', () => {
it('invokes listCryptoKeys without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
Expand All @@ -1004,6 +1028,30 @@ describe('KeyManagementServiceClient', () => {
});
});
});
describe('listCryptoKeysStream', () => {
it('invokes listCryptoKeysStream without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Mock request
const request: protosTypes.google.cloud.kms.v1.IListCryptoKeysRequest = {};
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.listCryptoKeys = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
const stream = client.listCryptoKeysStream(request, {}).on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
});
});
describe('listCryptoKeyVersions', () => {
it('invokes listCryptoKeyVersions without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
Expand All @@ -1026,6 +1074,30 @@ describe('KeyManagementServiceClient', () => {
});
});
});
describe('listCryptoKeyVersionsStream', () => {
it('invokes listCryptoKeyVersionsStream without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Mock request
const request: protosTypes.google.cloud.kms.v1.IListCryptoKeyVersionsRequest = {};
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.listCryptoKeyVersions = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
const stream = client.listCryptoKeyVersionsStream(request, {}).on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
});
});
describe('listImportJobs', () => {
it('invokes listImportJobs without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
Expand All @@ -1048,4 +1120,28 @@ describe('KeyManagementServiceClient', () => {
});
});
});
describe('listImportJobsStream', () => {
it('invokes listImportJobsStream without error', done => {
const client = new keymanagementserviceModule.v1.KeyManagementServiceClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
// Mock request
const request: protosTypes.google.cloud.kms.v1.IListImportJobsRequest = {};
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.listImportJobs = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
const stream = client.listImportJobsStream(request, {}).on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
});
});
});
2 changes: 1 addition & 1 deletion typescript/test/testdata/showcase/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build/protos"
],
"dependencies": {
"google-gax": "^1.7.5"
"google-gax": "^1.8.0"
},
"devDependencies": {
"@types/node": "^12.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import * as gax from 'google-gax';
import * as path from 'path';

import { Transform } from 'stream';
import * as protosTypes from '../../protos/protos';
import * as gapicConfig from './echo_client_config.json';

Expand Down Expand Up @@ -562,5 +563,18 @@ export class EchoClient {
}
options = options || {};
return this._innerApiCalls.pagedExpand(request, options, callback);
}

pagedExpandStream(
request?: protosTypes.google.showcase.v1beta1.IPagedExpandRequest,
options?: gax.CallOptions | {}):
Transform{
request = request || {};
const callSettings = new gax.CallSettings(options);
return this._descriptors.page.pagedExpand.createStream(
this._innerApiCalls.pagedExpand as gax.GaxCall,
request,
callSettings
);
}
}

0 comments on commit ba9891b

Please sign in to comment.