1717// ** All changes to this file may be overwritten. **
1818
1919import * as gax from 'google-gax' ;
20- import {
21- APICallback ,
22- Callback ,
23- CallOptions ,
24- Descriptors ,
25- ClientOptions ,
26- } from 'google-gax' ;
20+ import { APICallback , Callback , CallOptions , Descriptors , ClientOptions } from 'google-gax' ;
2721import * as path from 'path' ;
2822
2923import * as protosTypes from '../../protos/protos' ;
@@ -40,8 +34,13 @@ export class StreamingVideoIntelligenceServiceClient {
4034 private _descriptors : Descriptors = { page : { } , stream : { } , longrunning : { } } ;
4135 private _innerApiCalls : { [ name : string ] : Function } ;
4236 private _terminated = false ;
37+ private _opts : ClientOptions ;
38+ private _gaxModule : typeof gax | typeof gax . fallback ;
39+ private _gaxGrpc : gax . GrpcClient | gax . fallback . GrpcClient ;
40+ private _protos : { } ;
41+ private _defaults : { [ method : string ] : gax . CallSettings } ;
4342 auth : gax . GoogleAuth ;
44- streamingVideoIntelligenceServiceStub : Promise < { [ name : string ] : Function } > ;
43+ streamingVideoIntelligenceServiceStub ? : Promise < { [ name : string ] : Function } > ;
4544
4645 /**
4746 * Construct an instance of StreamingVideoIntelligenceServiceClient.
@@ -65,22 +64,17 @@ export class StreamingVideoIntelligenceServiceClient {
6564 * app is running in an environment which supports
6665 * {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
6766 * your project ID will be detected automatically.
68- * @param {function } [options.promise] - Custom promise module to use instead
69- * of native Promises.
7067 * @param {string } [options.apiEndpoint] - The domain name of the
7168 * API remote host.
7269 */
7370
7471 constructor ( opts ?: ClientOptions ) {
7572 // Ensure that options include the service address and port.
76- const staticMembers = this
77- . constructor as typeof StreamingVideoIntelligenceServiceClient ;
78- const servicePath =
79- opts && opts . servicePath
80- ? opts . servicePath
81- : opts && opts . apiEndpoint
82- ? opts . apiEndpoint
83- : staticMembers . servicePath ;
73+ const staticMembers = this . constructor as typeof StreamingVideoIntelligenceServiceClient ;
74+ const servicePath = opts && opts . servicePath ?
75+ opts . servicePath :
76+ ( ( opts && opts . apiEndpoint ) ? opts . apiEndpoint :
77+ staticMembers . servicePath ) ;
8478 const port = opts && opts . port ? opts . port : staticMembers . port ;
8579
8680 if ( ! opts ) {
@@ -90,33 +84,38 @@ export class StreamingVideoIntelligenceServiceClient {
9084 opts . port = opts . port || port ;
9185 opts . clientConfig = opts . clientConfig || { } ;
9286
93- const isBrowser = typeof window !== 'undefined' ;
94- if ( isBrowser ) {
87+ const isBrowser = ( typeof window !== 'undefined' ) ;
88+ if ( isBrowser ) {
9589 opts . fallback = true ;
9690 }
9791 // If we are in browser, we are already using fallback because of the
9892 // "browser" field in package.json.
9993 // But if we were explicitly requested to use fallback, let's do it now.
100- const gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
94+ this . _gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
10195
10296 // Create a `gaxGrpc` object, with any grpc-specific options
10397 // sent to the client.
104- opts . scopes = ( this
105- . constructor as typeof StreamingVideoIntelligenceServiceClient ) . scopes ;
106- const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
98+ opts . scopes = ( this . constructor as typeof StreamingVideoIntelligenceServiceClient ) . scopes ;
99+ this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
100+
101+ // Save options to use in initialize() method.
102+ this . _opts = opts ;
107103
108104 // Save the auth object to the client, for use by other methods.
109- this . auth = gaxGrpc . auth as gax . GoogleAuth ;
105+ this . auth = ( this . _gaxGrpc . auth as gax . GoogleAuth ) ;
110106
111107 // Determine the client header string.
112- const clientHeader = [ `gax/${ gaxModule . version } ` , `gapic/${ version } ` ] ;
108+ const clientHeader = [
109+ `gax/${ this . _gaxModule . version } ` ,
110+ `gapic/${ version } ` ,
111+ ] ;
113112 if ( typeof process !== 'undefined' && 'versions' in process ) {
114113 clientHeader . push ( `gl-node/${ process . versions . node } ` ) ;
115114 } else {
116- clientHeader . push ( `gl-web/${ gaxModule . version } ` ) ;
115+ clientHeader . push ( `gl-web/${ this . _gaxModule . version } ` ) ;
117116 }
118117 if ( ! opts . fallback ) {
119- clientHeader . push ( `grpc/${ gaxGrpc . grpcVersion } ` ) ;
118+ clientHeader . push ( `grpc/${ this . _gaxGrpc . grpcVersion } ` ) ;
120119 }
121120 if ( opts . libName && opts . libVersion ) {
122121 clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
@@ -125,56 +124,60 @@ export class StreamingVideoIntelligenceServiceClient {
125124 // For Node.js, pass the path to JSON proto file.
126125 // For browsers, pass the JSON content.
127126
128- const nodejsProtoPath = path . join (
129- __dirname ,
130- '..' ,
131- '..' ,
132- 'protos' ,
133- 'protos.json'
134- ) ;
135- const protos = gaxGrpc . loadProto (
136- opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
127+ const nodejsProtoPath = path . join ( __dirname , '..' , '..' , 'protos' , 'protos.json' ) ;
128+ this . _protos = this . _gaxGrpc . loadProto (
129+ opts . fallback ?
130+ require ( "../../protos/protos.json" ) :
131+ nodejsProtoPath
137132 ) ;
138133
139134 // Some of the methods on this service provide streaming responses.
140135 // Provide descriptors for these.
141136 this . _descriptors . stream = {
142- streamingAnnotateVideo : new gaxModule . StreamDescriptor (
143- gax . StreamType . BIDI_STREAMING
144- ) ,
137+ streamingAnnotateVideo : new this . _gaxModule . StreamDescriptor ( gax . StreamType . BIDI_STREAMING )
145138 } ;
146139
147140 // Put together the default options sent with requests.
148- const defaults = gaxGrpc . constructSettings (
149- 'google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceService' ,
150- gapicConfig as gax . ClientConfig ,
151- opts . clientConfig || { } ,
152- { 'x-goog-api-client' : clientHeader . join ( ' ' ) }
153- ) ;
141+ this . _defaults = this . _gaxGrpc . constructSettings (
142+ 'google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceService' , gapicConfig as gax . ClientConfig ,
143+ opts . clientConfig || { } , { 'x-goog-api-client' : clientHeader . join ( ' ' ) } ) ;
154144
155145 // Set up a dictionary of "inner API calls"; the core implementation
156146 // of calling the API is handled in `google-gax`, with this code
157147 // merely providing the destination and request information.
158148 this . _innerApiCalls = { } ;
149+ }
150+
151+ /**
152+ * Initialize the client.
153+ * Performs asynchronous operations (such as authentication) and prepares the client.
154+ * This function will be called automatically when any class method is called for the
155+ * first time, but if you need to initialize it before calling an actual method,
156+ * feel free to call initialize() directly.
157+ *
158+ * You can await on this method if you want to make sure the client is initialized.
159+ *
160+ * @returns {Promise } A promise that resolves to an authenticated service stub.
161+ */
162+ initialize ( ) {
163+ // If the client stub promise is already initialized, return immediately.
164+ if ( this . streamingVideoIntelligenceServiceStub ) {
165+ return this . streamingVideoIntelligenceServiceStub ;
166+ }
159167
160168 // Put together the "service stub" for
161169 // google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceService.
162- this . streamingVideoIntelligenceServiceStub = gaxGrpc . createStub (
163- opts . fallback
164- ? ( protos as protobuf . Root ) . lookupService (
165- 'google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceService'
166- )
167- : // tslint:disable-next-line no-any
168- ( protos as any ) . google . cloud . videointelligence . v1p3beta1
169- . StreamingVideoIntelligenceService ,
170- opts
171- ) as Promise < { [ method : string ] : Function } > ;
170+ this . streamingVideoIntelligenceServiceStub = this . _gaxGrpc . createStub (
171+ this . _opts . fallback ?
172+ ( this . _protos as protobuf . Root ) . lookupService ( 'google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceService' ) :
173+ // tslint:disable-next-line no-any
174+ ( this . _protos as any ) . google . cloud . videointelligence . v1p3beta1 . StreamingVideoIntelligenceService ,
175+ this . _opts ) as Promise < { [ method : string ] : Function } > ;
172176
173177 // Iterate over each of the methods that the service provides
174178 // and create an API call method for each.
175- const streamingVideoIntelligenceServiceStubMethods = [
176- 'streamingAnnotateVideo' ,
177- ] ;
179+ const streamingVideoIntelligenceServiceStubMethods =
180+ [ 'streamingAnnotateVideo' ] ;
178181
179182 for ( const methodName of streamingVideoIntelligenceServiceStubMethods ) {
180183 const innerCallPromise = this . streamingVideoIntelligenceServiceStub . then (
@@ -184,17 +187,16 @@ export class StreamingVideoIntelligenceServiceClient {
184187 }
185188 return stub [ methodName ] . apply ( stub , args ) ;
186189 } ,
187- ( err : Error | null | undefined ) => ( ) => {
190+ ( err : Error | null | undefined ) => ( ) => {
188191 throw err ;
189- }
190- ) ;
192+ } ) ;
191193
192- const apiCall = gaxModule . createApiCall (
194+ const apiCall = this . _gaxModule . createApiCall (
193195 innerCallPromise ,
194- defaults [ methodName ] ,
196+ this . _defaults [ methodName ] ,
195197 this . _descriptors . page [ methodName ] ||
196- this . _descriptors . stream [ methodName ] ||
197- this . _descriptors . longrunning [ methodName ]
198+ this . _descriptors . stream [ methodName ] ||
199+ this . _descriptors . longrunning [ methodName ]
198200 ) ;
199201
200202 this . _innerApiCalls [ methodName ] = (
@@ -205,6 +207,8 @@ export class StreamingVideoIntelligenceServiceClient {
205207 return apiCall ( argument , callOptions , callback ) ;
206208 } ;
207209 }
210+
211+ return this . streamingVideoIntelligenceServiceStub ;
208212 }
209213
210214 /**
@@ -234,7 +238,9 @@ export class StreamingVideoIntelligenceServiceClient {
234238 * in this service.
235239 */
236240 static get scopes ( ) {
237- return [ 'https://www.googleapis.com/auth/cloud-platform' ] ;
241+ return [
242+ 'https://www.googleapis.com/auth/cloud-platform'
243+ ] ;
238244 }
239245
240246 getProjectId ( ) : Promise < string > ;
@@ -244,9 +250,8 @@ export class StreamingVideoIntelligenceServiceClient {
244250 * @param {function(Error, string) } callback - the callback to
245251 * be called with the current project Id.
246252 */
247- getProjectId (
248- callback ?: Callback < string , undefined , undefined >
249- ) : Promise < string > | void {
253+ getProjectId ( callback ?: Callback < string , undefined , undefined > ) :
254+ Promise < string > | void {
250255 if ( callback ) {
251256 this . auth . getProjectId ( callback ) ;
252257 return ;
@@ -258,30 +263,35 @@ export class StreamingVideoIntelligenceServiceClient {
258263 // -- Service calls --
259264 // -------------------
260265
261- /**
262- * Performs video annotation with bidirectional streaming: emitting results
263- * while sending video/audio bytes.
264- * This method is only available via the gRPC API (not REST).
265- *
266- * @param {object } [options]
267- * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
268- * @returns {Stream }
269- * An object stream which is both readable and writable. It accepts objects
270- * representing [StreamingAnnotateVideoRequest]{@link google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest} for write() method, and
271- * will emit objects representing [StreamingAnnotateVideoResponse]{@link google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse} on 'data' event asynchronously.
272- */
273- streamingAnnotateVideo ( options ?: gax . CallOptions ) : gax . CancellableStream {
266+ /**
267+ * Performs video annotation with bidirectional streaming: emitting results
268+ * while sending video/audio bytes.
269+ * This method is only available via the gRPC API (not REST).
270+ *
271+ * @param {object } [options]
272+ * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
273+ * @returns {Stream }
274+ * An object stream which is both readable and writable. It accepts objects
275+ * representing [StreamingAnnotateVideoRequest]{@link google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest} for write() method, and
276+ * will emit objects representing [StreamingAnnotateVideoResponse]{@link google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse} on 'data' event asynchronously.
277+ */
278+ streamingAnnotateVideo (
279+ options ?: gax . CallOptions ) :
280+ gax . CancellableStream {
281+ this . initialize ( ) ;
274282 return this . _innerApiCalls . streamingAnnotateVideo ( options ) ;
275283 }
276284
285+
277286 /**
278287 * Terminate the GRPC channel and close the client.
279288 *
280289 * The client will no longer be usable and all future behavior is undefined.
281290 */
282291 close ( ) : Promise < void > {
292+ this . initialize ( ) ;
283293 if ( ! this . _terminated ) {
284- return this . streamingVideoIntelligenceServiceStub . then ( stub => {
294+ return this . streamingVideoIntelligenceServiceStub ! . then ( stub => {
285295 this . _terminated = true ;
286296 stub . close ( ) ;
287297 } ) ;
0 commit comments