I am using Google Speech API to transcript long files. The API is called from Google Cloud Functions. I want to check the result of longRunningRecognize later with operations.get. I know the name/id of the operation, but i cannot find a good way to check the status of operation from Google Cloud Function by the operation name.
Of course, i can just make a GET HTTP request to this url:
https://speech.googleapis.com/v1/operations/{name}?key=API_KEY
This is an example code that works:
const functions = require('firebase-functions');
const speech = require('@google-cloud/speech');
const request = require('request');
exports.transcribe = functions.storage.object().onFinalize((object) => {
// some code to get data required for speech API
const payload = {
audio: {
uri: 'some_uri/to/google/storage/file'
},
config: {
encoding: 'FLAC',
languageCode: 'en-US'
}
};
const client = new speech.SpeechClient({
projectId: 'my-project-id'
});
client.longRunningRecognize(payload)
.then(responses => {
const operation = responses[0];
// current example of getting operation status by operation name with HTTP call
request(`https://speech.googleapis.com/v1/operations/${operation.latestResponse.name}?key=MY-API-KEY`, (error, response, body) => {
console.log('Operation status response: ', body);
});
});
});
But it seems like there should be a more clear way of doing this. At least I can find this ruby way of getting operation status and this description of OperationsClient, so i want something like this to check the status:
// this line is the most confusing part of the puzzle
const client = longrunning.operationsClient();
const name = '';
client.getOperation({name: name}).then(function(responses) {
var response = responses[0];
// doThingsWith(response)
});
Thanks for any help!