Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.

Commit 4bbcd42

Browse files
fix: allow passing gax instance to client constructor (#584)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: https://github.com/googleapis/googleapis-gen/commit/f16a1d224f00a630ea43d6a9a1a31f566f45cdea 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: https://github.com/googleapis/googleapis-gen/commit/e97a1ac204ead4fe7341f91e72db7c6ac6016341 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
1 parent 24729a5 commit 4bbcd42

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

src/v1/device_manager_client.ts

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

4240
/**
@@ -96,8 +94,18 @@ export class DeviceManagerClient {
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 DeviceManagerClient({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 DeviceManagerClient;
103111
const servicePath =
@@ -117,8 +125,13 @@ export class DeviceManagerClient {
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);
@@ -203,7 +216,7 @@ export class DeviceManagerClient {
203216
this.innerApiCalls = {};
204217

205218
// Add a warn function to the client constructor so it can be easily tested.
206-
this.warn = gax.warn;
219+
this.warn = this._gaxModule.warn;
207220
}
208221

209222
/**
@@ -434,7 +447,7 @@ export class DeviceManagerClient {
434447
options.otherArgs = options.otherArgs || {};
435448
options.otherArgs.headers = options.otherArgs.headers || {};
436449
options.otherArgs.headers['x-goog-request-params'] =
437-
gax.routingHeader.fromParams({
450+
this._gaxModule.routingHeader.fromParams({
438451
parent: request.parent || '',
439452
});
440453
this.initialize();
@@ -520,7 +533,7 @@ export class DeviceManagerClient {
520533
options.otherArgs = options.otherArgs || {};
521534
options.otherArgs.headers = options.otherArgs.headers || {};
522535
options.otherArgs.headers['x-goog-request-params'] =
523-
gax.routingHeader.fromParams({
536+
this._gaxModule.routingHeader.fromParams({
524537
name: request.name || '',
525538
});
526539
this.initialize();
@@ -619,7 +632,7 @@ export class DeviceManagerClient {
619632
options.otherArgs = options.otherArgs || {};
620633
options.otherArgs.headers = options.otherArgs.headers || {};
621634
options.otherArgs.headers['x-goog-request-params'] =
622-
gax.routingHeader.fromParams({
635+
this._gaxModule.routingHeader.fromParams({
623636
'device_registry.name': request.deviceRegistry!.name || '',
624637
});
625638
this.initialize();
@@ -711,7 +724,7 @@ export class DeviceManagerClient {
711724
options.otherArgs = options.otherArgs || {};
712725
options.otherArgs.headers = options.otherArgs.headers || {};
713726
options.otherArgs.headers['x-goog-request-params'] =
714-
gax.routingHeader.fromParams({
727+
this._gaxModule.routingHeader.fromParams({
715728
name: request.name || '',
716729
});
717730
this.initialize();
@@ -800,7 +813,7 @@ export class DeviceManagerClient {
800813
options.otherArgs = options.otherArgs || {};
801814
options.otherArgs.headers = options.otherArgs.headers || {};
802815
options.otherArgs.headers['x-goog-request-params'] =
803-
gax.routingHeader.fromParams({
816+
this._gaxModule.routingHeader.fromParams({
804817
parent: request.parent || '',
805818
});
806819
this.initialize();
@@ -889,7 +902,7 @@ export class DeviceManagerClient {
889902
options.otherArgs = options.otherArgs || {};
890903
options.otherArgs.headers = options.otherArgs.headers || {};
891904
options.otherArgs.headers['x-goog-request-params'] =
892-
gax.routingHeader.fromParams({
905+
this._gaxModule.routingHeader.fromParams({
893906
name: request.name || '',
894907
});
895908
this.initialize();
@@ -980,7 +993,7 @@ export class DeviceManagerClient {
980993
options.otherArgs = options.otherArgs || {};
981994
options.otherArgs.headers = options.otherArgs.headers || {};
982995
options.otherArgs.headers['x-goog-request-params'] =
983-
gax.routingHeader.fromParams({
996+
this._gaxModule.routingHeader.fromParams({
984997
'device.name': request.device!.name || '',
985998
});
986999
this.initialize();
@@ -1065,7 +1078,7 @@ export class DeviceManagerClient {
10651078
options.otherArgs = options.otherArgs || {};
10661079
options.otherArgs.headers = options.otherArgs.headers || {};
10671080
options.otherArgs.headers['x-goog-request-params'] =
1068-
gax.routingHeader.fromParams({
1081+
this._gaxModule.routingHeader.fromParams({
10691082
name: request.name || '',
10701083
});
10711084
this.initialize();
@@ -1168,7 +1181,7 @@ export class DeviceManagerClient {
11681181
options.otherArgs = options.otherArgs || {};
11691182
options.otherArgs.headers = options.otherArgs.headers || {};
11701183
options.otherArgs.headers['x-goog-request-params'] =
1171-
gax.routingHeader.fromParams({
1184+
this._gaxModule.routingHeader.fromParams({
11721185
name: request.name || '',
11731186
});
11741187
this.initialize();
@@ -1270,7 +1283,7 @@ export class DeviceManagerClient {
12701283
options.otherArgs = options.otherArgs || {};
12711284
options.otherArgs.headers = options.otherArgs.headers || {};
12721285
options.otherArgs.headers['x-goog-request-params'] =
1273-
gax.routingHeader.fromParams({
1286+
this._gaxModule.routingHeader.fromParams({
12741287
name: request.name || '',
12751288
});
12761289
this.initialize();
@@ -1366,7 +1379,7 @@ export class DeviceManagerClient {
13661379
options.otherArgs = options.otherArgs || {};
13671380
options.otherArgs.headers = options.otherArgs.headers || {};
13681381
options.otherArgs.headers['x-goog-request-params'] =
1369-
gax.routingHeader.fromParams({
1382+
this._gaxModule.routingHeader.fromParams({
13701383
name: request.name || '',
13711384
});
13721385
this.initialize();
@@ -1462,7 +1475,7 @@ export class DeviceManagerClient {
14621475
options.otherArgs = options.otherArgs || {};
14631476
options.otherArgs.headers = options.otherArgs.headers || {};
14641477
options.otherArgs.headers['x-goog-request-params'] =
1465-
gax.routingHeader.fromParams({
1478+
this._gaxModule.routingHeader.fromParams({
14661479
resource: request.resource || '',
14671480
});
14681481
this.initialize();
@@ -1551,7 +1564,7 @@ export class DeviceManagerClient {
15511564
options.otherArgs = options.otherArgs || {};
15521565
options.otherArgs.headers = options.otherArgs.headers || {};
15531566
options.otherArgs.headers['x-goog-request-params'] =
1554-
gax.routingHeader.fromParams({
1567+
this._gaxModule.routingHeader.fromParams({
15551568
resource: request.resource || '',
15561569
});
15571570
this.initialize();
@@ -1642,7 +1655,7 @@ export class DeviceManagerClient {
16421655
options.otherArgs = options.otherArgs || {};
16431656
options.otherArgs.headers = options.otherArgs.headers || {};
16441657
options.otherArgs.headers['x-goog-request-params'] =
1645-
gax.routingHeader.fromParams({
1658+
this._gaxModule.routingHeader.fromParams({
16461659
resource: request.resource || '',
16471660
});
16481661
this.initialize();
@@ -1749,7 +1762,7 @@ export class DeviceManagerClient {
17491762
options.otherArgs = options.otherArgs || {};
17501763
options.otherArgs.headers = options.otherArgs.headers || {};
17511764
options.otherArgs.headers['x-goog-request-params'] =
1752-
gax.routingHeader.fromParams({
1765+
this._gaxModule.routingHeader.fromParams({
17531766
name: request.name || '',
17541767
});
17551768
this.initialize();
@@ -1842,7 +1855,7 @@ export class DeviceManagerClient {
18421855
options.otherArgs = options.otherArgs || {};
18431856
options.otherArgs.headers = options.otherArgs.headers || {};
18441857
options.otherArgs.headers['x-goog-request-params'] =
1845-
gax.routingHeader.fromParams({
1858+
this._gaxModule.routingHeader.fromParams({
18461859
parent: request.parent || '',
18471860
});
18481861
this.initialize();
@@ -1941,7 +1954,7 @@ export class DeviceManagerClient {
19411954
options.otherArgs = options.otherArgs || {};
19421955
options.otherArgs.headers = options.otherArgs.headers || {};
19431956
options.otherArgs.headers['x-goog-request-params'] =
1944-
gax.routingHeader.fromParams({
1957+
this._gaxModule.routingHeader.fromParams({
19451958
parent: request.parent || '',
19461959
});
19471960
this.initialize();
@@ -2050,7 +2063,7 @@ export class DeviceManagerClient {
20502063
options.otherArgs = options.otherArgs || {};
20512064
options.otherArgs.headers = options.otherArgs.headers || {};
20522065
options.otherArgs.headers['x-goog-request-params'] =
2053-
gax.routingHeader.fromParams({
2066+
this._gaxModule.routingHeader.fromParams({
20542067
parent: request.parent || '',
20552068
});
20562069
this.initialize();
@@ -2094,7 +2107,7 @@ export class DeviceManagerClient {
20942107
options.otherArgs = options.otherArgs || {};
20952108
options.otherArgs.headers = options.otherArgs.headers || {};
20962109
options.otherArgs.headers['x-goog-request-params'] =
2097-
gax.routingHeader.fromParams({
2110+
this._gaxModule.routingHeader.fromParams({
20982111
parent: request.parent || '',
20992112
});
21002113
const defaultCallSettings = this._defaults['listDeviceRegistries'];
@@ -2147,7 +2160,7 @@ export class DeviceManagerClient {
21472160
options.otherArgs = options.otherArgs || {};
21482161
options.otherArgs.headers = options.otherArgs.headers || {};
21492162
options.otherArgs.headers['x-goog-request-params'] =
2150-
gax.routingHeader.fromParams({
2163+
this._gaxModule.routingHeader.fromParams({
21512164
parent: request.parent || '',
21522165
});
21532166
const defaultCallSettings = this._defaults['listDeviceRegistries'];
@@ -2262,7 +2275,7 @@ export class DeviceManagerClient {
22622275
options.otherArgs = options.otherArgs || {};
22632276
options.otherArgs.headers = options.otherArgs.headers || {};
22642277
options.otherArgs.headers['x-goog-request-params'] =
2265-
gax.routingHeader.fromParams({
2278+
this._gaxModule.routingHeader.fromParams({
22662279
parent: request.parent || '',
22672280
});
22682281
this.initialize();
@@ -2319,7 +2332,7 @@ export class DeviceManagerClient {
23192332
options.otherArgs = options.otherArgs || {};
23202333
options.otherArgs.headers = options.otherArgs.headers || {};
23212334
options.otherArgs.headers['x-goog-request-params'] =
2322-
gax.routingHeader.fromParams({
2335+
this._gaxModule.routingHeader.fromParams({
23232336
parent: request.parent || '',
23242337
});
23252338
const defaultCallSettings = this._defaults['listDevices'];
@@ -2385,7 +2398,7 @@ export class DeviceManagerClient {
23852398
options.otherArgs = options.otherArgs || {};
23862399
options.otherArgs.headers = options.otherArgs.headers || {};
23872400
options.otherArgs.headers['x-goog-request-params'] =
2388-
gax.routingHeader.fromParams({
2401+
this._gaxModule.routingHeader.fromParams({
23892402
parent: request.parent || '',
23902403
});
23912404
const defaultCallSettings = this._defaults['listDevices'];

0 commit comments

Comments
 (0)