import * as gax from 'google-gax' ; import { Callback , CallOptions , Descriptors , ClientOptions , LROperation , } from 'google-gax' ; import * as protos from '../../protos/protos' ; import jsonProtos = require ( '../../protos/protos.json' ) ; /**
 * Client JSON configuration object, loaded from
 * `src/v1/speech_client_config.json`.
 * This file defines retry strategy and timeouts for all API methods in this library.
 */ import * as gapicConfig from './speech_client_config.json' ; import { operationsProtos } from 'google-gax' ; const version = require ( '../../../package.json' ) . version ; class memberof export class SpeechClient { private _terminated = false ; private _opts : ClientOptions ; private _providedCustomServicePath : boolean ; private _gaxModule : typeof gax | typeof gax . fallback ; private _gaxGrpc : gax . GrpcClient | gax . fallback . GrpcClient ; private _protos : { } ; private _defaults : { [ method ] : gax . CallSettings ; } ; auth : gax . GoogleAuth ; descriptors : Descriptors = { page : { } , stream : { } , longrunning : { } , batching : { } , } ; warn : ( code : string , message : string , warnType ? : string ) => void ; innerApiCalls : { [ name ] : Function ; } ; pathTemplates : { [ name ] : gax . PathTemplate ; } ; operationsClient : gax . OperationsClient ; speechStub ? : Promise < { [ name ] : Function ; } > ; param options param options . credentials param { string } options . credentials . client_email param { string } options . credentials . private_key param { string } options . email param { string } options . keyFilename param { number } options . port param { string } options . projectId - The project ID from the Google
     *     Developer's Console, e.g. 'grape-spaceship-123'. We will also check
     *     the environment variable GCLOUD_PROJECT for your project ID. If your
     *     app is running in an environment which supports
     *      https ,
     *     your project ID will be detected automatically.
     *  param { string } options . apiEndpoint param { gax . ClientConfig } options . clientConfig - Client configuration override.
     *     Follows the structure of  gapicConfig .
     *  param { boolean } options . fallback constructor ( opts ? ) { const staticMembers = this . constructor as typeof SpeechClient ; const servicePath = opts ?. servicePath || opts ?. apiEndpoint || staticMembers . servicePath ; this . _providedCustomServicePath = ! ! ( opts ?. servicePath || opts ?. apiEndpoint ) ; const port = opts ?. port || staticMembers . port ; const clientConfig = opts ?. clientConfig ?? { } ; const fallback = opts ?. fallback ?? ( typeof window !== 'undefined' && typeof window ?. fetch === 'function' ) ; opts = Object . assign ( { servicePath , port , clientConfig , fallback } , opts ) ; if ( servicePath !== staticMembers . servicePath && ! ( 'scopes' in opts ) ) { opts [ 'scopes' ] = staticMembers . scopes ; } this . _gaxModule = opts . fallback ? gax . fallback : gax ; this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ; this . _opts = opts ; this . auth = this . _gaxGrpc . auth as gax . GoogleAuth ; this . auth . useJWTAccessWithScope = true ; this . auth . defaultServicePath = staticMembers . servicePath ; if ( servicePath === staticMembers . servicePath ) { this . auth . defaultScopes = staticMembers . scopes ; } const clientHeader = [ `gax/${ this . _gaxModule . version }` , `gapic/${ version }` ] ; if ( typeof process !== 'undefined' && 'versions' in process ) { clientHeader . push ( `gl-node/${ process . versions . node }` ) ; } else { clientHeader . push ( `gl-web/${ this . _gaxModule . version }` ) ; } if ( ! opts . fallback ) { clientHeader . push ( `grpc/${ this . _gaxGrpc . grpcVersion }` ) ; } else if ( opts . fallback === 'rest' ) { clientHeader . push ( `rest/${ this . _gaxGrpc . grpcVersion }` ) ; } if ( opts . libName && opts . libVersion ) { clientHeader . push ( `${ opts . libName }/${ opts . libVersion }` ) ; } this . _protos = this . _gaxGrpc . loadProtoJSON ( jsonProtos ) ; this . pathTemplates = { customClassPathTemplate : new this . _gaxModule . PathTemplate ( 'projects/{project}/locations/{location}/customClasses/{custom_class}' ) , phraseSetPathTemplate : new this . _gaxModule . PathTemplate ( 'projects/{project}/locations/{location}/phraseSets/{phrase_set}' ) , } ; this . descriptors . stream = { streamingRecognize : new this . _gaxModule . StreamDescriptor ( gax . StreamType . BIDI_STREAMING ) , } ; const protoFilesRoot = this . _gaxModule . protobuf . Root . fromJSON ( jsonProtos ) ; this . operationsClient = this . _gaxModule . lro ( { auth : this . auth , grpc : 'grpc' in this . _gaxGrpc ? this . _gaxGrpc . grpc : undefined , } ) . operationsClient ( opts ) ; const longRunningRecognizeResponse = protoFilesRoot . lookup ( '.google.cloud.speech.v1.LongRunningRecognizeResponse' ) as gax . protobuf . Type ; const longRunningRecognizeMetadata = protoFilesRoot . lookup ( '.google.cloud.speech.v1.LongRunningRecognizeMetadata' ) as gax . protobuf . Type ; this . descriptors . longrunning = { longRunningRecognize : new this . _gaxModule . LongrunningDescriptor ( this . operationsClient , longRunningRecognizeResponse . decode . bind ( longRunningRecognizeResponse ) , longRunningRecognizeMetadata . decode . bind ( longRunningRecognizeMetadata ) ) , } ; this . _defaults = this . _gaxGrpc . constructSettings ( 'google.cloud.speech.v1.Speech' , gapicConfig as gax . ClientConfig , opts . clientConfig || { } , { 'x-goog-api-client' : clientHeader . join ( ' ' ) } ) ; this . innerApiCalls = { } ; this . warn = gax . warn ; } returns { Promise } initialize ( ) { if ( this . speechStub ) { return this . speechStub ; } this . speechStub = this . _gaxGrpc . createStub ( this . _opts . fallback ? ( this . _protos as protobuf . Root ) . lookupService ( 'google.cloud.speech.v1.Speech' ) : ( this . _protos as any ) . google . cloud . speech . v1 . Speech , this . _opts , this . _providedCustomServicePath ) as Promise < { [ method ] : Function ; } > ; const speechStubMethods = [ 'recognize' , 'longRunningRecognize' , 'streamingRecognize' , ] ; for ( const methodName of speechStubMethods ) { const callPromise = this . speechStub . then ( stub => ( ... args ) => { if ( this . _terminated ) { return Promise . reject ( 'The client has already been closed.' ) ; } const func = stub [ methodName ] ; return func . apply ( stub , args ) ; } , ( err ) => ( ) => { throw err ; } ) ; const descriptor = this . descriptors . stream [ methodName ] || this . descriptors . longrunning [ methodName ] || undefined ; const apiCall = this . _gaxModule . createApiCall ( callPromise , this . _defaults [ methodName ] , descriptor ) ; this . innerApiCalls [ methodName ] = apiCall ; } return this . speechStub ; } returns { string } static get servicePath ( ) { return 'speech.googleapis.com' ; } returns { string } static get apiEndpoint ( ) { return 'speech.googleapis.com' ; } returns { number } static get port ( ) { return 443 ; } returns { string [ ] } static get scopes ( ) { return [ 'https://www.googleapis.com/auth/cloud-platform' ] ; } getProjectId ( ) ; getProjectId ( callback ) ; returns { Promise } getProjectId ( callback ? ) { if ( callback ) { this . auth . getProjectId ( callback ) ; return ; } return this . auth . getProjectId ( ) ; } param request param { google . cloud . speech . v1 . RecognitionConfig } request . config param { google . cloud . speech . v1 . RecognitionAudio } request . audio param { object } options Call options. See  https for more details.
     *  returns { Promise } - The promise which resolves to an array.
     *   The first element of the array is an object representing [RecognizeResponse] google . cloud . speech . v1 . RecognizeResponse .
     *   Please see the
     *   [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
     *   for more details and examples.
     *  example recognize ( request ? , options ? ) ; recognize ( request , options , callback ) ; recognize ( request , callback ) ; recognize ( request ? , optionsOrCallback ? , callback ? ) { request = request || { } ; let options ; if ( typeof optionsOrCallback === 'function' && callback === undefined ) { callback = optionsOrCallback ; options = { } ; } else { options = optionsOrCallback as CallOptions ; } options = options || { } ; options . otherArgs = options . otherArgs || { } ; options . otherArgs . headers = options . otherArgs . headers || { } ; this . initialize ( ) ; return this . innerApiCalls . recognize ( request , options , callback ) ; } param { object } options Call options. See  https for more details.
     *  returns { Stream } *   An object stream which is both readable and writable. It accepts objects
     *   representing [StreamingRecognizeRequest] google . cloud . speech . v1 . StreamingRecognizeRequest for write() method, and
     *   will emit objects representing [StreamingRecognizeResponse] google . cloud . speech . v1 . StreamingRecognizeResponse on 'data' event asynchronously.
     *   Please see the
     *   [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#bi-directional-streaming)
     *   for more details and examples.
     *  example _streamingRecognize ( options ? ) { this . initialize ( ) ; return this . innerApiCalls . streamingRecognize ( options ) ; } param request param { google . cloud . speech . v1 . RecognitionConfig } request . config param { google . cloud . speech . v1 . RecognitionAudio } request . audio param { google . cloud . speech . v1 . TranscriptOutputConfig } request . outputConfig param { object } options Call options. See  https for more details.
     *  returns { Promise } example longRunningRecognize ( request ? , options ? ) ; longRunningRecognize ( request , options , callback ) ; longRunningRecognize ( request , callback ) ; longRunningRecognize ( request ? , optionsOrCallback ? , callback ? ) { request = request || { } ; let options ; if ( typeof optionsOrCallback === 'function' && callback === undefined ) { callback = optionsOrCallback ; options = { } ; } else { options = optionsOrCallback as CallOptions ; } options = options || { } ; options . otherArgs = options . otherArgs || { } ; options . otherArgs . headers = options . otherArgs . headers || { } ; this . initialize ( ) ; return this . innerApiCalls . longRunningRecognize ( request , options , callback ) ; } param { String } name returns { Promise } example async checkLongRunningRecognizeProgress ( name ) { 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 > ; } param { string } project param { string } location param { string } custom_class returns { string } customClassPath ( project , location , customClass ) { return this . pathTemplates . customClassPathTemplate . render ( { project : project , location : location , custom_class : customClass , } ) ; } param { string } customClassName returns { string } matchProjectFromCustomClassName ( customClassName ) { return this . pathTemplates . customClassPathTemplate . match ( customClassName ) . project ; } param { string } customClassName returns { string } matchLocationFromCustomClassName ( customClassName ) { return this . pathTemplates . customClassPathTemplate . match ( customClassName ) . location ; } param { string } customClassName returns { string } matchCustomClassFromCustomClassName ( customClassName ) { return this . pathTemplates . customClassPathTemplate . match ( customClassName ) . custom_class ; } param { string } project param { string } location param { string } phrase_set returns { string } phraseSetPath ( project , location , phraseSet ) { return this . pathTemplates . phraseSetPathTemplate . render ( { project : project , location : location , phrase_set : phraseSet , } ) ; } param { string } phraseSetName returns { string } matchProjectFromPhraseSetName ( phraseSetName ) { return this . pathTemplates . phraseSetPathTemplate . match ( phraseSetName ) . project ; } param { string } phraseSetName returns { string } matchLocationFromPhraseSetName ( phraseSetName ) { return this . pathTemplates . phraseSetPathTemplate . match ( phraseSetName ) . location ; } param { string } phraseSetName returns { string } matchPhraseSetFromPhraseSetName ( phraseSetName ) { return this . pathTemplates . phraseSetPathTemplate . match ( phraseSetName ) . phrase_set ; } returns { Promise } close ( ) { this . initialize ( ) ; if ( ! this . _terminated ) { return this . speechStub ! . then ( stub => { this . _terminated = true ; stub . close ( ) ; this . operationsClient . close ( ) ; } ) ; } return Promise . resolve ( ) ; } } import { ImprovedStreamingClient } from '../helpers' ; export interface SpeechClient extends ImprovedStreamingClient { }