Skip to content

Commit a3746f3

Browse files
fix: allow passing gax instance to client constructor (#924)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: googleapis/googleapis-gen@f16a1d2 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZjE2YTFkMjI0ZjAwYTYzMGVhNDNkNmE5YTFhMzFmNTY2ZjQ1Y2RlYSJ9 feat: accept google-gax instance as a parameter Please see the documentation of the client constructor for details. PiperOrigin-RevId: 470332808 Source-Link: googleapis/googleapis@d4a2367 Source-Link: googleapis/googleapis-gen@e97a1ac Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
1 parent ba6dcc1 commit a3746f3

File tree

4 files changed

+126
-70
lines changed

4 files changed

+126
-70
lines changed

packages/google-cloud-speech/src/v1/adaptation_client.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {
20+
import type * as gax from 'google-gax';
21+
import type {
2222
Callback,
2323
CallOptions,
2424
Descriptors,
2525
ClientOptions,
2626
PaginationCallback,
2727
GaxCall,
2828
} from 'google-gax';
29-
3029
import {Transform} from 'stream';
3130
import * as protos from '../../protos/protos';
3231
import jsonProtos = require('../../protos/protos.json');
@@ -36,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3635
* This file defines retry strategy and timeouts for all API methods in this library.
3736
*/
3837
import * as gapicConfig from './adaptation_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

4240
/**
@@ -96,8 +94,18 @@ export class AdaptationClient {
9694
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9795
* For more information, please check the
9896
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
97+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
98+
* need to avoid loading the default gRPC version and want to use the fallback
99+
* HTTP implementation. Load only fallback version and pass it to the constructor:
100+
* ```
101+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
102+
* const client = new AdaptationClient({fallback: 'rest'}, gax);
103+
* ```
99104
*/
100-
constructor(opts?: ClientOptions) {
105+
constructor(
106+
opts?: ClientOptions,
107+
gaxInstance?: typeof gax | typeof gax.fallback
108+
) {
101109
// Ensure that options include all the required fields.
102110
const staticMembers = this.constructor as typeof AdaptationClient;
103111
const servicePath =
@@ -117,8 +125,13 @@ export class AdaptationClient {
117125
opts['scopes'] = staticMembers.scopes;
118126
}
119127

128+
// Load google-gax module synchronously if needed
129+
if (!gaxInstance) {
130+
gaxInstance = require('google-gax') as typeof gax;
131+
}
132+
120133
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
121-
this._gaxModule = opts.fallback ? gax.fallback : gax;
134+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
122135

123136
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
124137
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -206,7 +219,7 @@ export class AdaptationClient {
206219
this.innerApiCalls = {};
207220

208221
// Add a warn function to the client constructor so it can be easily tested.
209-
this.warn = gax.warn;
222+
this.warn = this._gaxModule.warn;
210223
}
211224

212225
/**
@@ -432,7 +445,7 @@ export class AdaptationClient {
432445
options.otherArgs = options.otherArgs || {};
433446
options.otherArgs.headers = options.otherArgs.headers || {};
434447
options.otherArgs.headers['x-goog-request-params'] =
435-
gax.routingHeader.fromParams({
448+
this._gaxModule.routingHeader.fromParams({
436449
parent: request.parent || '',
437450
});
438451
this.initialize();
@@ -523,7 +536,7 @@ export class AdaptationClient {
523536
options.otherArgs = options.otherArgs || {};
524537
options.otherArgs.headers = options.otherArgs.headers || {};
525538
options.otherArgs.headers['x-goog-request-params'] =
526-
gax.routingHeader.fromParams({
539+
this._gaxModule.routingHeader.fromParams({
527540
name: request.name || '',
528541
});
529542
this.initialize();
@@ -621,7 +634,7 @@ export class AdaptationClient {
621634
options.otherArgs = options.otherArgs || {};
622635
options.otherArgs.headers = options.otherArgs.headers || {};
623636
options.otherArgs.headers['x-goog-request-params'] =
624-
gax.routingHeader.fromParams({
637+
this._gaxModule.routingHeader.fromParams({
625638
'phrase_set.name': request.phraseSet!.name || '',
626639
});
627640
this.initialize();
@@ -708,7 +721,7 @@ export class AdaptationClient {
708721
options.otherArgs = options.otherArgs || {};
709722
options.otherArgs.headers = options.otherArgs.headers || {};
710723
options.otherArgs.headers['x-goog-request-params'] =
711-
gax.routingHeader.fromParams({
724+
this._gaxModule.routingHeader.fromParams({
712725
name: request.name || '',
713726
});
714727
this.initialize();
@@ -815,7 +828,7 @@ export class AdaptationClient {
815828
options.otherArgs = options.otherArgs || {};
816829
options.otherArgs.headers = options.otherArgs.headers || {};
817830
options.otherArgs.headers['x-goog-request-params'] =
818-
gax.routingHeader.fromParams({
831+
this._gaxModule.routingHeader.fromParams({
819832
parent: request.parent || '',
820833
});
821834
this.initialize();
@@ -902,7 +915,7 @@ export class AdaptationClient {
902915
options.otherArgs = options.otherArgs || {};
903916
options.otherArgs.headers = options.otherArgs.headers || {};
904917
options.otherArgs.headers['x-goog-request-params'] =
905-
gax.routingHeader.fromParams({
918+
this._gaxModule.routingHeader.fromParams({
906919
name: request.name || '',
907920
});
908921
this.initialize();
@@ -1006,7 +1019,7 @@ export class AdaptationClient {
10061019
options.otherArgs = options.otherArgs || {};
10071020
options.otherArgs.headers = options.otherArgs.headers || {};
10081021
options.otherArgs.headers['x-goog-request-params'] =
1009-
gax.routingHeader.fromParams({
1022+
this._gaxModule.routingHeader.fromParams({
10101023
'custom_class.name': request.customClass!.name || '',
10111024
});
10121025
this.initialize();
@@ -1105,7 +1118,7 @@ export class AdaptationClient {
11051118
options.otherArgs = options.otherArgs || {};
11061119
options.otherArgs.headers = options.otherArgs.headers || {};
11071120
options.otherArgs.headers['x-goog-request-params'] =
1108-
gax.routingHeader.fromParams({
1121+
this._gaxModule.routingHeader.fromParams({
11091122
name: request.name || '',
11101123
});
11111124
this.initialize();
@@ -1213,7 +1226,7 @@ export class AdaptationClient {
12131226
options.otherArgs = options.otherArgs || {};
12141227
options.otherArgs.headers = options.otherArgs.headers || {};
12151228
options.otherArgs.headers['x-goog-request-params'] =
1216-
gax.routingHeader.fromParams({
1229+
this._gaxModule.routingHeader.fromParams({
12171230
parent: request.parent || '',
12181231
});
12191232
this.initialize();
@@ -1266,7 +1279,7 @@ export class AdaptationClient {
12661279
options.otherArgs = options.otherArgs || {};
12671280
options.otherArgs.headers = options.otherArgs.headers || {};
12681281
options.otherArgs.headers['x-goog-request-params'] =
1269-
gax.routingHeader.fromParams({
1282+
this._gaxModule.routingHeader.fromParams({
12701283
parent: request.parent || '',
12711284
});
12721285
const defaultCallSettings = this._defaults['listPhraseSet'];
@@ -1328,7 +1341,7 @@ export class AdaptationClient {
13281341
options.otherArgs = options.otherArgs || {};
13291342
options.otherArgs.headers = options.otherArgs.headers || {};
13301343
options.otherArgs.headers['x-goog-request-params'] =
1331-
gax.routingHeader.fromParams({
1344+
this._gaxModule.routingHeader.fromParams({
13321345
parent: request.parent || '',
13331346
});
13341347
const defaultCallSettings = this._defaults['listPhraseSet'];
@@ -1447,7 +1460,7 @@ export class AdaptationClient {
14471460
options.otherArgs = options.otherArgs || {};
14481461
options.otherArgs.headers = options.otherArgs.headers || {};
14491462
options.otherArgs.headers['x-goog-request-params'] =
1450-
gax.routingHeader.fromParams({
1463+
this._gaxModule.routingHeader.fromParams({
14511464
parent: request.parent || '',
14521465
});
14531466
this.initialize();
@@ -1500,7 +1513,7 @@ export class AdaptationClient {
15001513
options.otherArgs = options.otherArgs || {};
15011514
options.otherArgs.headers = options.otherArgs.headers || {};
15021515
options.otherArgs.headers['x-goog-request-params'] =
1503-
gax.routingHeader.fromParams({
1516+
this._gaxModule.routingHeader.fromParams({
15041517
parent: request.parent || '',
15051518
});
15061519
const defaultCallSettings = this._defaults['listCustomClasses'];
@@ -1562,7 +1575,7 @@ export class AdaptationClient {
15621575
options.otherArgs = options.otherArgs || {};
15631576
options.otherArgs.headers = options.otherArgs.headers || {};
15641577
options.otherArgs.headers['x-goog-request-params'] =
1565-
gax.routingHeader.fromParams({
1578+
this._gaxModule.routingHeader.fromParams({
15661579
parent: request.parent || '',
15671580
});
15681581
const defaultCallSettings = this._defaults['listCustomClasses'];

packages/google-cloud-speech/src/v1/speech_client.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {
20+
import type * as gax from 'google-gax';
21+
import type {
2222
Callback,
2323
CallOptions,
2424
Descriptors,
2525
ClientOptions,
2626
GrpcClientOptions,
2727
LROperation,
28-
GoogleError,
2928
} from 'google-gax';
30-
3129
import {PassThrough} from 'stream';
3230
import * as protos from '../../protos/protos';
3331
import jsonProtos = require('../../protos/protos.json');
@@ -37,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3735
* This file defines retry strategy and timeouts for all API methods in this library.
3836
*/
3937
import * as gapicConfig from './speech_client_config.json';
40-
import {operationsProtos} from 'google-gax';
4138
const version = require('../../../package.json').version;
4239

4340
/**
@@ -98,8 +95,18 @@ export class SpeechClient {
9895
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9996
* For more information, please check the
10097
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
98+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
99+
* need to avoid loading the default gRPC version and want to use the fallback
100+
* HTTP implementation. Load only fallback version and pass it to the constructor:
101+
* ```
102+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
103+
* const client = new SpeechClient({fallback: 'rest'}, gax);
104+
* ```
101105
*/
102-
constructor(opts?: ClientOptions) {
106+
constructor(
107+
opts?: ClientOptions,
108+
gaxInstance?: typeof gax | typeof gax.fallback
109+
) {
103110
// Ensure that options include all the required fields.
104111
const staticMembers = this.constructor as typeof SpeechClient;
105112
const servicePath =
@@ -119,8 +126,13 @@ export class SpeechClient {
119126
opts['scopes'] = staticMembers.scopes;
120127
}
121128

129+
// Load google-gax module synchronously if needed
130+
if (!gaxInstance) {
131+
gaxInstance = require('google-gax') as typeof gax;
132+
}
133+
122134
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
123-
this._gaxModule = opts.fallback ? gax.fallback : gax;
135+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
124136

125137
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
126138
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -176,7 +188,7 @@ export class SpeechClient {
176188
// Provide descriptors for these.
177189
this.descriptors.stream = {
178190
streamingRecognize: new this._gaxModule.StreamDescriptor(
179-
gax.StreamType.BIDI_STREAMING,
191+
this._gaxModule.StreamType.BIDI_STREAMING,
180192
opts.fallback === 'rest'
181193
),
182194
};
@@ -234,7 +246,7 @@ export class SpeechClient {
234246
this.innerApiCalls = {};
235247

236248
// Add a warn function to the client constructor so it can be easily tested.
237-
this.warn = gax.warn;
249+
this.warn = this._gaxModule.warn;
238250
}
239251

240252
/**
@@ -284,7 +296,9 @@ export class SpeechClient {
284296
setImmediate(() => {
285297
stream.emit(
286298
'error',
287-
new GoogleError('The client has already been closed.')
299+
new this._gaxModule.GoogleError(
300+
'The client has already been closed.'
301+
)
288302
);
289303
});
290304
return stream;
@@ -603,11 +617,12 @@ export class SpeechClient {
603617
protos.google.cloud.speech.v1.LongRunningRecognizeMetadata
604618
>
605619
> {
606-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
607-
{name}
608-
);
620+
const request =
621+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
622+
{name}
623+
);
609624
const [operation] = await this.operationsClient.getOperation(request);
610-
const decodeOperation = new gax.Operation(
625+
const decodeOperation = new this._gaxModule.Operation(
611626
operation,
612627
this.descriptors.longrunning.longRunningRecognize,
613628
this._gaxModule.createDefaultBackoffSettings()

0 commit comments

Comments
 (0)