@@ -41,8 +41,13 @@ export class LanguageServiceClient {
41
41
private _descriptors : Descriptors = { page : { } , stream : { } , longrunning : { } } ;
42
42
private _innerApiCalls : { [ name : string ] : Function } ;
43
43
private _terminated = false ;
44
+ private _opts : ClientOptions ;
45
+ private _gaxModule : typeof gax | typeof gax . fallback ;
46
+ private _gaxGrpc : gax . GrpcClient | gax . fallback . GrpcClient ;
47
+ private _protos : { } ;
48
+ private _defaults : { [ method : string ] : gax . CallSettings } ;
44
49
auth : gax . GoogleAuth ;
45
- languageServiceStub : Promise < { [ name : string ] : Function } > ;
50
+ languageServiceStub ? : Promise < { [ name : string ] : Function } > ;
46
51
47
52
/**
48
53
* Construct an instance of LanguageServiceClient.
@@ -66,8 +71,6 @@ export class LanguageServiceClient {
66
71
* app is running in an environment which supports
67
72
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
68
73
* your project ID will be detected automatically.
69
- * @param {function } [options.promise] - Custom promise module to use instead
70
- * of native Promises.
71
74
* @param {string } [options.apiEndpoint] - The domain name of the
72
75
* API remote host.
73
76
*/
@@ -97,25 +100,28 @@ export class LanguageServiceClient {
97
100
// If we are in browser, we are already using fallback because of the
98
101
// "browser" field in package.json.
99
102
// But if we were explicitly requested to use fallback, let's do it now.
100
- const gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
103
+ this . _gaxModule = ! isBrowser && opts . fallback ? gax . fallback : gax ;
101
104
102
105
// Create a `gaxGrpc` object, with any grpc-specific options
103
106
// sent to the client.
104
107
opts . scopes = ( this . constructor as typeof LanguageServiceClient ) . scopes ;
105
- const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
108
+ this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
109
+
110
+ // Save options to use in initialize() method.
111
+ this . _opts = opts ;
106
112
107
113
// Save the auth object to the client, for use by other methods.
108
- this . auth = gaxGrpc . auth as gax . GoogleAuth ;
114
+ this . auth = this . _gaxGrpc . auth as gax . GoogleAuth ;
109
115
110
116
// Determine the client header string.
111
- const clientHeader = [ `gax/${ gaxModule . version } ` , `gapic/${ version } ` ] ;
117
+ const clientHeader = [ `gax/${ this . _gaxModule . version } ` , `gapic/${ version } ` ] ;
112
118
if ( typeof process !== 'undefined' && 'versions' in process ) {
113
119
clientHeader . push ( `gl-node/${ process . versions . node } ` ) ;
114
120
} else {
115
- clientHeader . push ( `gl-web/${ gaxModule . version } ` ) ;
121
+ clientHeader . push ( `gl-web/${ this . _gaxModule . version } ` ) ;
116
122
}
117
123
if ( ! opts . fallback ) {
118
- clientHeader . push ( `grpc/${ gaxGrpc . grpcVersion } ` ) ;
124
+ clientHeader . push ( `grpc/${ this . _gaxGrpc . grpcVersion } ` ) ;
119
125
}
120
126
if ( opts . libName && opts . libVersion ) {
121
127
clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
@@ -131,12 +137,12 @@ export class LanguageServiceClient {
131
137
'protos' ,
132
138
'protos.json'
133
139
) ;
134
- const protos = gaxGrpc . loadProto (
140
+ this . _protos = this . _gaxGrpc . loadProto (
135
141
opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
136
142
) ;
137
143
138
144
// Put together the default options sent with requests.
139
- const defaults = gaxGrpc . constructSettings (
145
+ this . _defaults = this . _gaxGrpc . constructSettings (
140
146
'google.cloud.language.v1.LanguageService' ,
141
147
gapicConfig as gax . ClientConfig ,
142
148
opts . clientConfig || { } ,
@@ -147,17 +153,35 @@ export class LanguageServiceClient {
147
153
// of calling the API is handled in `google-gax`, with this code
148
154
// merely providing the destination and request information.
149
155
this . _innerApiCalls = { } ;
156
+ }
157
+
158
+ /**
159
+ * Initialize the client.
160
+ * Performs asynchronous operations (such as authentication) and prepares the client.
161
+ * This function will be called automatically when any class method is called for the
162
+ * first time, but if you need to initialize it before calling an actual method,
163
+ * feel free to call initialize() directly.
164
+ *
165
+ * You can await on this method if you want to make sure the client is initialized.
166
+ *
167
+ * @returns {Promise } A promise that resolves to an authenticated service stub.
168
+ */
169
+ initialize ( ) {
170
+ // If the client stub promise is already initialized, return immediately.
171
+ if ( this . languageServiceStub ) {
172
+ return this . languageServiceStub ;
173
+ }
150
174
151
175
// Put together the "service stub" for
152
176
// google.cloud.language.v1.LanguageService.
153
- this . languageServiceStub = gaxGrpc . createStub (
154
- opts . fallback
155
- ? ( protos as protobuf . Root ) . lookupService (
177
+ this . languageServiceStub = this . _gaxGrpc . createStub (
178
+ this . _opts . fallback
179
+ ? ( this . _protos as protobuf . Root ) . lookupService (
156
180
'google.cloud.language.v1.LanguageService'
157
181
)
158
182
: // tslint:disable-next-line no-any
159
- ( protos as any ) . google . cloud . language . v1 . LanguageService ,
160
- opts
183
+ ( this . _protos as any ) . google . cloud . language . v1 . LanguageService ,
184
+ this . _opts
161
185
) as Promise < { [ method : string ] : Function } > ;
162
186
163
187
// Iterate over each of the methods that the service provides
@@ -184,9 +208,9 @@ export class LanguageServiceClient {
184
208
}
185
209
) ;
186
210
187
- const apiCall = gaxModule . createApiCall (
211
+ const apiCall = this . _gaxModule . createApiCall (
188
212
innerCallPromise ,
189
- defaults [ methodName ] ,
213
+ this . _defaults [ methodName ] ,
190
214
this . _descriptors . page [ methodName ] ||
191
215
this . _descriptors . stream [ methodName ] ||
192
216
this . _descriptors . longrunning [ methodName ]
@@ -200,6 +224,8 @@ export class LanguageServiceClient {
200
224
return apiCall ( argument , callOptions , callback ) ;
201
225
} ;
202
226
}
227
+
228
+ return this . languageServiceStub ;
203
229
}
204
230
205
231
/**
@@ -320,6 +346,7 @@ export class LanguageServiceClient {
320
346
options = optionsOrCallback as gax . CallOptions ;
321
347
}
322
348
options = options || { } ;
349
+ this . initialize ( ) ;
323
350
return this . _innerApiCalls . analyzeSentiment ( request , options , callback ) ;
324
351
}
325
352
analyzeEntities (
@@ -389,6 +416,7 @@ export class LanguageServiceClient {
389
416
options = optionsOrCallback as gax . CallOptions ;
390
417
}
391
418
options = options || { } ;
419
+ this . initialize ( ) ;
392
420
return this . _innerApiCalls . analyzeEntities ( request , options , callback ) ;
393
421
}
394
422
analyzeEntitySentiment (
@@ -415,7 +443,7 @@ export class LanguageServiceClient {
415
443
>
416
444
) : void ;
417
445
/**
418
- * Finds entities, similar to [AnalyzeEntities][ google.cloud.language.v1.LanguageService.AnalyzeEntities] in the text and analyzes
446
+ * Finds entities, similar to { @link google.cloud.language.v1.LanguageService.AnalyzeEntities|AnalyzeEntities} in the text and analyzes
419
447
* sentiment associated with each entity and its mentions.
420
448
*
421
449
* @param {Object } request
@@ -465,6 +493,7 @@ export class LanguageServiceClient {
465
493
options = optionsOrCallback as gax . CallOptions ;
466
494
}
467
495
options = options || { } ;
496
+ this . initialize ( ) ;
468
497
return this . _innerApiCalls . analyzeEntitySentiment (
469
498
request ,
470
499
options ,
@@ -538,6 +567,7 @@ export class LanguageServiceClient {
538
567
options = optionsOrCallback as gax . CallOptions ;
539
568
}
540
569
options = options || { } ;
570
+ this . initialize ( ) ;
541
571
return this . _innerApiCalls . analyzeSyntax ( request , options , callback ) ;
542
572
}
543
573
classifyText (
@@ -602,6 +632,7 @@ export class LanguageServiceClient {
602
632
options = optionsOrCallback as gax . CallOptions ;
603
633
}
604
634
options = options || { } ;
635
+ this . initialize ( ) ;
605
636
return this . _innerApiCalls . classifyText ( request , options , callback ) ;
606
637
}
607
638
annotateText (
@@ -671,6 +702,7 @@ export class LanguageServiceClient {
671
702
options = optionsOrCallback as gax . CallOptions ;
672
703
}
673
704
options = options || { } ;
705
+ this . initialize ( ) ;
674
706
return this . _innerApiCalls . annotateText ( request , options , callback ) ;
675
707
}
676
708
@@ -680,8 +712,9 @@ export class LanguageServiceClient {
680
712
* The client will no longer be usable and all future behavior is undefined.
681
713
*/
682
714
close ( ) : Promise < void > {
715
+ this . initialize ( ) ;
683
716
if ( ! this . _terminated ) {
684
- return this . languageServiceStub . then ( stub => {
717
+ return this . languageServiceStub ! . then ( stub => {
685
718
this . _terminated = true ;
686
719
stub . close ( ) ;
687
720
} ) ;
0 commit comments