Skip to content

Commit

Permalink
feat: check status of long running operation by its name (#590)
Browse files Browse the repository at this point in the history
For each client method returning a long running operation, a separate method to check its status is added.

Added methods: `checkLongRunningRecognizeProgress`.
  • Loading branch information
alexander-fenster authored May 6, 2020
1 parent 5defd86 commit 28e6dac
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 317 deletions.
556 changes: 278 additions & 278 deletions packages/google-cloud-speech/protos/protos.js

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion packages/google-cloud-speech/src/v1/speech_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as path from 'path';

import * as protos from '../../protos/protos';
import * as gapicConfig from './speech_client_config.json';

import {operationsProtos} from 'google-gax';
const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -509,6 +509,42 @@ export class SpeechClient {
this.initialize();
return this.innerApiCalls.longRunningRecognize(request, options, callback);
}
/**
* Check the status of the long running operation returned by the longRunningRecognize() method.
* @param {String} name
* The operation name that will be passed.
* @returns {Promise} - The promise which resolves to an object.
* The decoded operation object has result and metadata field to get information from.
*
* @example:
* const decodedOperation = await checkLongRunningRecognizeProgress(name);
* console.log(decodedOperation.result);
* console.log(decodedOperation.done);
* console.log(decodedOperation.metadata);
*
*/
async checkLongRunningRecognizeProgress(
name: string
): Promise<
LROperation<
protos.google.cloud.speech.v1.LongRunningRecognizeResponse,
protos.google.cloud.speech.v1.LongRunningRecognizeMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
operation,
this.descriptors.longrunning.longRunningRecognize,
gax.createDefaultBackoffSettings()
);
return decodeOperation as LROperation<
protos.google.cloud.speech.v1.LongRunningRecognizeResponse,
protos.google.cloud.speech.v1.LongRunningRecognizeMetadata
>;
}

/**
* Terminate the GRPC channel and close the client.
Expand Down
38 changes: 37 additions & 1 deletion packages/google-cloud-speech/src/v1p1beta1/speech_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import * as path from 'path';

import * as protos from '../../protos/protos';
import * as gapicConfig from './speech_client_config.json';

import {operationsProtos} from 'google-gax';
const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -524,6 +524,42 @@ export class SpeechClient {
this.initialize();
return this.innerApiCalls.longRunningRecognize(request, options, callback);
}
/**
* Check the status of the long running operation returned by the longRunningRecognize() method.
* @param {String} name
* The operation name that will be passed.
* @returns {Promise} - The promise which resolves to an object.
* The decoded operation object has result and metadata field to get information from.
*
* @example:
* const decodedOperation = await checkLongRunningRecognizeProgress(name);
* console.log(decodedOperation.result);
* console.log(decodedOperation.done);
* console.log(decodedOperation.metadata);
*
*/
async checkLongRunningRecognizeProgress(
name: string
): Promise<
LROperation<
protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse,
protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
operation,
this.descriptors.longrunning.longRunningRecognize,
gax.createDefaultBackoffSettings()
);
return decodeOperation as LROperation<
protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse,
protos.google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata
>;
}
// --------------------
// -- Path templates --
// --------------------
Expand Down
14 changes: 3 additions & 11 deletions packages/google-cloud-speech/synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
{
"git": {
"name": ".",
"remote": "https://github.com/googleapis/nodejs-speech.git",
"sha": "016458a23a630b0fc0ce8251d7d043dfaf031a2c"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "42ee97c1b93a0e3759bbba3013da309f670a90ab",
"internalRef": "307114445"
"remote": "[email protected]:googleapis/nodejs-speech.git",
"sha": "fbf5e043110fb651d385c41e092746b882292e87"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "19465d3ec5e5acdb01521d8f3bddd311bcbee28d"
"sha": "ab883569eb0257bbf16a6d825fd018b3adde3912"
}
}
],
Expand Down
59 changes: 46 additions & 13 deletions packages/google-cloud-speech/test/gapic_speech_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as speechModule from '../src';

import {PassThrough} from 'stream';

import {protobuf, LROperation} from 'google-gax';
import {protobuf, LROperation, operationsProtos} from 'google-gax';

function generateSampleMessage<T extends object>(instance: T) {
const filledObject = (instance.constructor as typeof protobuf.Message).toObject(
Expand Down Expand Up @@ -255,9 +255,7 @@ describe('v1.SpeechClient', () => {
const expectedOptions = {};
const expectedError = new Error('expected');
client.innerApiCalls.recognize = stubSimpleCall(undefined, expectedError);
await assert.rejects(async () => {
await client.recognize(request);
}, expectedError);
await assert.rejects(client.recognize(request), expectedError);
assert(
(client.innerApiCalls.recognize as SinonStub)
.getCall(0)
Expand Down Expand Up @@ -355,9 +353,7 @@ describe('v1.SpeechClient', () => {
undefined,
expectedError
);
await assert.rejects(async () => {
await client.longRunningRecognize(request);
}, expectedError);
await assert.rejects(client.longRunningRecognize(request), expectedError);
assert(
(client.innerApiCalls.longRunningRecognize as SinonStub)
.getCall(0)
Expand All @@ -382,15 +378,54 @@ describe('v1.SpeechClient', () => {
expectedError
);
const [operation] = await client.longRunningRecognize(request);
await assert.rejects(async () => {
await operation.promise();
}, expectedError);
await assert.rejects(operation.promise(), expectedError);
assert(
(client.innerApiCalls.longRunningRecognize as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
});

it('invokes checkLongRunningRecognizeProgress without error', async () => {
const client = new speechModule.v1.SpeechClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
client.initialize();
const expectedResponse = generateSampleMessage(
new operationsProtos.google.longrunning.Operation()
);
expectedResponse.name = 'test';
expectedResponse.response = {type_url: 'url', value: Buffer.from('')};
expectedResponse.metadata = {type_url: 'url', value: Buffer.from('')};

client.operationsClient.getOperation = stubSimpleCall(expectedResponse);
const decodedOperation = await client.checkLongRunningRecognizeProgress(
expectedResponse.name
);
assert.deepStrictEqual(decodedOperation.name, expectedResponse.name);
assert(decodedOperation.metadata);
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
});

it('invokes checkLongRunningRecognizeProgress with error', async () => {
const client = new speechModule.v1.SpeechClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
client.initialize();
const expectedError = new Error('expected');

client.operationsClient.getOperation = stubSimpleCall(
undefined,
expectedError
);
await assert.rejects(
client.checkLongRunningRecognizeProgress(''),
expectedError
);
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
});
});

describe('streamingRecognize', () => {
Expand Down Expand Up @@ -470,9 +505,7 @@ describe('v1.SpeechClient', () => {
stream.write(request);
stream.end();
});
await assert.rejects(async () => {
await promise;
}, expectedError);
await assert.rejects(promise, expectedError);
assert(
(client.innerApiCalls.streamingRecognize as SinonStub)
.getCall(0)
Expand Down
59 changes: 46 additions & 13 deletions packages/google-cloud-speech/test/gapic_speech_v1p1beta1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as speechModule from '../src';

import {PassThrough} from 'stream';

import {protobuf, LROperation} from 'google-gax';
import {protobuf, LROperation, operationsProtos} from 'google-gax';

function generateSampleMessage<T extends object>(instance: T) {
const filledObject = (instance.constructor as typeof protobuf.Message).toObject(
Expand Down Expand Up @@ -255,9 +255,7 @@ describe('v1p1beta1.SpeechClient', () => {
const expectedOptions = {};
const expectedError = new Error('expected');
client.innerApiCalls.recognize = stubSimpleCall(undefined, expectedError);
await assert.rejects(async () => {
await client.recognize(request);
}, expectedError);
await assert.rejects(client.recognize(request), expectedError);
assert(
(client.innerApiCalls.recognize as SinonStub)
.getCall(0)
Expand Down Expand Up @@ -355,9 +353,7 @@ describe('v1p1beta1.SpeechClient', () => {
undefined,
expectedError
);
await assert.rejects(async () => {
await client.longRunningRecognize(request);
}, expectedError);
await assert.rejects(client.longRunningRecognize(request), expectedError);
assert(
(client.innerApiCalls.longRunningRecognize as SinonStub)
.getCall(0)
Expand All @@ -382,15 +378,54 @@ describe('v1p1beta1.SpeechClient', () => {
expectedError
);
const [operation] = await client.longRunningRecognize(request);
await assert.rejects(async () => {
await operation.promise();
}, expectedError);
await assert.rejects(operation.promise(), expectedError);
assert(
(client.innerApiCalls.longRunningRecognize as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
});

it('invokes checkLongRunningRecognizeProgress without error', async () => {
const client = new speechModule.v1p1beta1.SpeechClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
client.initialize();
const expectedResponse = generateSampleMessage(
new operationsProtos.google.longrunning.Operation()
);
expectedResponse.name = 'test';
expectedResponse.response = {type_url: 'url', value: Buffer.from('')};
expectedResponse.metadata = {type_url: 'url', value: Buffer.from('')};

client.operationsClient.getOperation = stubSimpleCall(expectedResponse);
const decodedOperation = await client.checkLongRunningRecognizeProgress(
expectedResponse.name
);
assert.deepStrictEqual(decodedOperation.name, expectedResponse.name);
assert(decodedOperation.metadata);
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
});

it('invokes checkLongRunningRecognizeProgress with error', async () => {
const client = new speechModule.v1p1beta1.SpeechClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
client.initialize();
const expectedError = new Error('expected');

client.operationsClient.getOperation = stubSimpleCall(
undefined,
expectedError
);
await assert.rejects(
client.checkLongRunningRecognizeProgress(''),
expectedError
);
assert((client.operationsClient.getOperation as SinonStub).getCall(0));
});
});

describe('streamingRecognize', () => {
Expand Down Expand Up @@ -470,9 +505,7 @@ describe('v1p1beta1.SpeechClient', () => {
stream.write(request);
stream.end();
});
await assert.rejects(async () => {
await promise;
}, expectedError);
await assert.rejects(promise, expectedError);
assert(
(client.innerApiCalls.streamingRecognize as SinonStub)
.getCall(0)
Expand Down

0 comments on commit 28e6dac

Please sign in to comment.