Skip to content

Commit b55761c

Browse files
fix: allow passing gax instance to client constructor (#650)
- [ ] 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 a860eaa commit b55761c

File tree

3 files changed

+124
-85
lines changed

3 files changed

+124
-85
lines changed

packages/google-cloud-tasks/src/v2/cloud_tasks_client.ts

+40-27
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 './cloud_tasks_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

4240
/**
@@ -97,8 +95,18 @@ export class CloudTasksClient {
9795
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9896
* For more information, please check the
9997
* {@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 CloudTasksClient({fallback: 'rest'}, gax);
104+
* ```
100105
*/
101-
constructor(opts?: ClientOptions) {
106+
constructor(
107+
opts?: ClientOptions,
108+
gaxInstance?: typeof gax | typeof gax.fallback
109+
) {
102110
// Ensure that options include all the required fields.
103111
const staticMembers = this.constructor as typeof CloudTasksClient;
104112
const servicePath =
@@ -118,8 +126,13 @@ export class CloudTasksClient {
118126
opts['scopes'] = staticMembers.scopes;
119127
}
120128

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

124137
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
125138
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -207,7 +220,7 @@ export class CloudTasksClient {
207220
this.innerApiCalls = {};
208221

209222
// Add a warn function to the client constructor so it can be easily tested.
210-
this.warn = gax.warn;
223+
this.warn = this._gaxModule.warn;
211224
}
212225

213226
/**
@@ -420,7 +433,7 @@ export class CloudTasksClient {
420433
options.otherArgs = options.otherArgs || {};
421434
options.otherArgs.headers = options.otherArgs.headers || {};
422435
options.otherArgs.headers['x-goog-request-params'] =
423-
gax.routingHeader.fromParams({
436+
this._gaxModule.routingHeader.fromParams({
424437
name: request.name || '',
425438
});
426439
this.initialize();
@@ -523,7 +536,7 @@ export class CloudTasksClient {
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
parent: request.parent || '',
528541
});
529542
this.initialize();
@@ -630,7 +643,7 @@ export class CloudTasksClient {
630643
options.otherArgs = options.otherArgs || {};
631644
options.otherArgs.headers = options.otherArgs.headers || {};
632645
options.otherArgs.headers['x-goog-request-params'] =
633-
gax.routingHeader.fromParams({
646+
this._gaxModule.routingHeader.fromParams({
634647
'queue.name': request.queue!.name || '',
635648
});
636649
this.initialize();
@@ -726,7 +739,7 @@ export class CloudTasksClient {
726739
options.otherArgs = options.otherArgs || {};
727740
options.otherArgs.headers = options.otherArgs.headers || {};
728741
options.otherArgs.headers['x-goog-request-params'] =
729-
gax.routingHeader.fromParams({
742+
this._gaxModule.routingHeader.fromParams({
730743
name: request.name || '',
731744
});
732745
this.initialize();
@@ -815,7 +828,7 @@ export class CloudTasksClient {
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
name: request.name || '',
820833
});
821834
this.initialize();
@@ -905,7 +918,7 @@ export class CloudTasksClient {
905918
options.otherArgs = options.otherArgs || {};
906919
options.otherArgs.headers = options.otherArgs.headers || {};
907920
options.otherArgs.headers['x-goog-request-params'] =
908-
gax.routingHeader.fromParams({
921+
this._gaxModule.routingHeader.fromParams({
909922
name: request.name || '',
910923
});
911924
this.initialize();
@@ -1001,7 +1014,7 @@ export class CloudTasksClient {
10011014
options.otherArgs = options.otherArgs || {};
10021015
options.otherArgs.headers = options.otherArgs.headers || {};
10031016
options.otherArgs.headers['x-goog-request-params'] =
1004-
gax.routingHeader.fromParams({
1017+
this._gaxModule.routingHeader.fromParams({
10051018
name: request.name || '',
10061019
});
10071020
this.initialize();
@@ -1096,7 +1109,7 @@ export class CloudTasksClient {
10961109
options.otherArgs = options.otherArgs || {};
10971110
options.otherArgs.headers = options.otherArgs.headers || {};
10981111
options.otherArgs.headers['x-goog-request-params'] =
1099-
gax.routingHeader.fromParams({
1112+
this._gaxModule.routingHeader.fromParams({
11001113
resource: request.resource || '',
11011114
});
11021115
this.initialize();
@@ -1201,7 +1214,7 @@ export class CloudTasksClient {
12011214
options.otherArgs = options.otherArgs || {};
12021215
options.otherArgs.headers = options.otherArgs.headers || {};
12031216
options.otherArgs.headers['x-goog-request-params'] =
1204-
gax.routingHeader.fromParams({
1217+
this._gaxModule.routingHeader.fromParams({
12051218
resource: request.resource || '',
12061219
});
12071220
this.initialize();
@@ -1296,7 +1309,7 @@ export class CloudTasksClient {
12961309
options.otherArgs = options.otherArgs || {};
12971310
options.otherArgs.headers = options.otherArgs.headers || {};
12981311
options.otherArgs.headers['x-goog-request-params'] =
1299-
gax.routingHeader.fromParams({
1312+
this._gaxModule.routingHeader.fromParams({
13001313
resource: request.resource || '',
13011314
});
13021315
this.initialize();
@@ -1393,7 +1406,7 @@ export class CloudTasksClient {
13931406
options.otherArgs = options.otherArgs || {};
13941407
options.otherArgs.headers = options.otherArgs.headers || {};
13951408
options.otherArgs.headers['x-goog-request-params'] =
1396-
gax.routingHeader.fromParams({
1409+
this._gaxModule.routingHeader.fromParams({
13971410
name: request.name || '',
13981411
});
13991412
this.initialize();
@@ -1530,7 +1543,7 @@ export class CloudTasksClient {
15301543
options.otherArgs = options.otherArgs || {};
15311544
options.otherArgs.headers = options.otherArgs.headers || {};
15321545
options.otherArgs.headers['x-goog-request-params'] =
1533-
gax.routingHeader.fromParams({
1546+
this._gaxModule.routingHeader.fromParams({
15341547
parent: request.parent || '',
15351548
});
15361549
this.initialize();
@@ -1618,7 +1631,7 @@ export class CloudTasksClient {
16181631
options.otherArgs = options.otherArgs || {};
16191632
options.otherArgs.headers = options.otherArgs.headers || {};
16201633
options.otherArgs.headers['x-goog-request-params'] =
1621-
gax.routingHeader.fromParams({
1634+
this._gaxModule.routingHeader.fromParams({
16221635
name: request.name || '',
16231636
});
16241637
this.initialize();
@@ -1738,7 +1751,7 @@ export class CloudTasksClient {
17381751
options.otherArgs = options.otherArgs || {};
17391752
options.otherArgs.headers = options.otherArgs.headers || {};
17401753
options.otherArgs.headers['x-goog-request-params'] =
1741-
gax.routingHeader.fromParams({
1754+
this._gaxModule.routingHeader.fromParams({
17421755
name: request.name || '',
17431756
});
17441757
this.initialize();
@@ -1857,7 +1870,7 @@ export class CloudTasksClient {
18571870
options.otherArgs = options.otherArgs || {};
18581871
options.otherArgs.headers = options.otherArgs.headers || {};
18591872
options.otherArgs.headers['x-goog-request-params'] =
1860-
gax.routingHeader.fromParams({
1873+
this._gaxModule.routingHeader.fromParams({
18611874
parent: request.parent || '',
18621875
});
18631876
this.initialize();
@@ -1921,7 +1934,7 @@ export class CloudTasksClient {
19211934
options.otherArgs = options.otherArgs || {};
19221935
options.otherArgs.headers = options.otherArgs.headers || {};
19231936
options.otherArgs.headers['x-goog-request-params'] =
1924-
gax.routingHeader.fromParams({
1937+
this._gaxModule.routingHeader.fromParams({
19251938
parent: request.parent || '',
19261939
});
19271940
const defaultCallSettings = this._defaults['listQueues'];
@@ -1994,7 +2007,7 @@ export class CloudTasksClient {
19942007
options.otherArgs = options.otherArgs || {};
19952008
options.otherArgs.headers = options.otherArgs.headers || {};
19962009
options.otherArgs.headers['x-goog-request-params'] =
1997-
gax.routingHeader.fromParams({
2010+
this._gaxModule.routingHeader.fromParams({
19982011
parent: request.parent || '',
19992012
});
20002013
const defaultCallSettings = this._defaults['listQueues'];
@@ -2127,7 +2140,7 @@ export class CloudTasksClient {
21272140
options.otherArgs = options.otherArgs || {};
21282141
options.otherArgs.headers = options.otherArgs.headers || {};
21292142
options.otherArgs.headers['x-goog-request-params'] =
2130-
gax.routingHeader.fromParams({
2143+
this._gaxModule.routingHeader.fromParams({
21312144
parent: request.parent || '',
21322145
});
21332146
this.initialize();
@@ -2194,7 +2207,7 @@ export class CloudTasksClient {
21942207
options.otherArgs = options.otherArgs || {};
21952208
options.otherArgs.headers = options.otherArgs.headers || {};
21962209
options.otherArgs.headers['x-goog-request-params'] =
2197-
gax.routingHeader.fromParams({
2210+
this._gaxModule.routingHeader.fromParams({
21982211
parent: request.parent || '',
21992212
});
22002213
const defaultCallSettings = this._defaults['listTasks'];
@@ -2270,7 +2283,7 @@ export class CloudTasksClient {
22702283
options.otherArgs = options.otherArgs || {};
22712284
options.otherArgs.headers = options.otherArgs.headers || {};
22722285
options.otherArgs.headers['x-goog-request-params'] =
2273-
gax.routingHeader.fromParams({
2286+
this._gaxModule.routingHeader.fromParams({
22742287
parent: request.parent || '',
22752288
});
22762289
const defaultCallSettings = this._defaults['listTasks'];

0 commit comments

Comments
 (0)