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

Commit 3532064

Browse files
fix: allow passing gax instance to client constructor (#169)
- [ ] 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 56f9393 commit 3532064

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

src/v1/reservation_service_client.ts

Lines changed: 51 additions & 38 deletions
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 './reservation_service_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

4240
/**
@@ -110,8 +108,18 @@ export class ReservationServiceClient {
110108
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
111109
* For more information, please check the
112110
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
111+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
112+
* need to avoid loading the default gRPC version and want to use the fallback
113+
* HTTP implementation. Load only fallback version and pass it to the constructor:
114+
* ```
115+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
116+
* const client = new ReservationServiceClient({fallback: 'rest'}, gax);
117+
* ```
113118
*/
114-
constructor(opts?: ClientOptions) {
119+
constructor(
120+
opts?: ClientOptions,
121+
gaxInstance?: typeof gax | typeof gax.fallback
122+
) {
115123
// Ensure that options include all the required fields.
116124
const staticMembers = this.constructor as typeof ReservationServiceClient;
117125
const servicePath =
@@ -131,8 +139,13 @@ export class ReservationServiceClient {
131139
opts['scopes'] = staticMembers.scopes;
132140
}
133141

142+
// Load google-gax module synchronously if needed
143+
if (!gaxInstance) {
144+
gaxInstance = require('google-gax') as typeof gax;
145+
}
146+
134147
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
135-
this._gaxModule = opts.fallback ? gax.fallback : gax;
148+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
136149

137150
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
138151
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -241,7 +254,7 @@ export class ReservationServiceClient {
241254
this.innerApiCalls = {};
242255

243256
// Add a warn function to the client constructor so it can be easily tested.
244-
this.warn = gax.warn;
257+
this.warn = this._gaxModule.warn;
245258
}
246259

247260
/**
@@ -483,7 +496,7 @@ export class ReservationServiceClient {
483496
options.otherArgs = options.otherArgs || {};
484497
options.otherArgs.headers = options.otherArgs.headers || {};
485498
options.otherArgs.headers['x-goog-request-params'] =
486-
gax.routingHeader.fromParams({
499+
this._gaxModule.routingHeader.fromParams({
487500
parent: request.parent || '',
488501
});
489502
this.initialize();
@@ -581,7 +594,7 @@ export class ReservationServiceClient {
581594
options.otherArgs = options.otherArgs || {};
582595
options.otherArgs.headers = options.otherArgs.headers || {};
583596
options.otherArgs.headers['x-goog-request-params'] =
584-
gax.routingHeader.fromParams({
597+
this._gaxModule.routingHeader.fromParams({
585598
name: request.name || '',
586599
});
587600
this.initialize();
@@ -681,7 +694,7 @@ export class ReservationServiceClient {
681694
options.otherArgs = options.otherArgs || {};
682695
options.otherArgs.headers = options.otherArgs.headers || {};
683696
options.otherArgs.headers['x-goog-request-params'] =
684-
gax.routingHeader.fromParams({
697+
this._gaxModule.routingHeader.fromParams({
685698
name: request.name || '',
686699
});
687700
this.initialize();
@@ -780,7 +793,7 @@ export class ReservationServiceClient {
780793
options.otherArgs = options.otherArgs || {};
781794
options.otherArgs.headers = options.otherArgs.headers || {};
782795
options.otherArgs.headers['x-goog-request-params'] =
783-
gax.routingHeader.fromParams({
796+
this._gaxModule.routingHeader.fromParams({
784797
'reservation.name': request.reservation!.name || '',
785798
});
786799
this.initialize();
@@ -889,7 +902,7 @@ export class ReservationServiceClient {
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
parent: request.parent || '',
894907
});
895908
this.initialize();
@@ -991,7 +1004,7 @@ export class ReservationServiceClient {
9911004
options.otherArgs = options.otherArgs || {};
9921005
options.otherArgs.headers = options.otherArgs.headers || {};
9931006
options.otherArgs.headers['x-goog-request-params'] =
994-
gax.routingHeader.fromParams({
1007+
this._gaxModule.routingHeader.fromParams({
9951008
name: request.name || '',
9961009
});
9971010
this.initialize();
@@ -1095,7 +1108,7 @@ export class ReservationServiceClient {
10951108
options.otherArgs = options.otherArgs || {};
10961109
options.otherArgs.headers = options.otherArgs.headers || {};
10971110
options.otherArgs.headers['x-goog-request-params'] =
1098-
gax.routingHeader.fromParams({
1111+
this._gaxModule.routingHeader.fromParams({
10991112
name: request.name || '',
11001113
});
11011114
this.initialize();
@@ -1204,7 +1217,7 @@ export class ReservationServiceClient {
12041217
options.otherArgs = options.otherArgs || {};
12051218
options.otherArgs.headers = options.otherArgs.headers || {};
12061219
options.otherArgs.headers['x-goog-request-params'] =
1207-
gax.routingHeader.fromParams({
1220+
this._gaxModule.routingHeader.fromParams({
12081221
'capacity_commitment.name': request.capacityCommitment!.name || '',
12091222
});
12101223
this.initialize();
@@ -1315,7 +1328,7 @@ export class ReservationServiceClient {
13151328
options.otherArgs = options.otherArgs || {};
13161329
options.otherArgs.headers = options.otherArgs.headers || {};
13171330
options.otherArgs.headers['x-goog-request-params'] =
1318-
gax.routingHeader.fromParams({
1331+
this._gaxModule.routingHeader.fromParams({
13191332
name: request.name || '',
13201333
});
13211334
this.initialize();
@@ -1429,7 +1442,7 @@ export class ReservationServiceClient {
14291442
options.otherArgs = options.otherArgs || {};
14301443
options.otherArgs.headers = options.otherArgs.headers || {};
14311444
options.otherArgs.headers['x-goog-request-params'] =
1432-
gax.routingHeader.fromParams({
1445+
this._gaxModule.routingHeader.fromParams({
14331446
parent: request.parent || '',
14341447
});
14351448
this.initialize();
@@ -1572,7 +1585,7 @@ export class ReservationServiceClient {
15721585
options.otherArgs = options.otherArgs || {};
15731586
options.otherArgs.headers = options.otherArgs.headers || {};
15741587
options.otherArgs.headers['x-goog-request-params'] =
1575-
gax.routingHeader.fromParams({
1588+
this._gaxModule.routingHeader.fromParams({
15761589
parent: request.parent || '',
15771590
});
15781591
this.initialize();
@@ -1684,7 +1697,7 @@ export class ReservationServiceClient {
16841697
options.otherArgs = options.otherArgs || {};
16851698
options.otherArgs.headers = options.otherArgs.headers || {};
16861699
options.otherArgs.headers['x-goog-request-params'] =
1687-
gax.routingHeader.fromParams({
1700+
this._gaxModule.routingHeader.fromParams({
16881701
name: request.name || '',
16891702
});
16901703
this.initialize();
@@ -1790,7 +1803,7 @@ export class ReservationServiceClient {
17901803
options.otherArgs = options.otherArgs || {};
17911804
options.otherArgs.headers = options.otherArgs.headers || {};
17921805
options.otherArgs.headers['x-goog-request-params'] =
1793-
gax.routingHeader.fromParams({
1806+
this._gaxModule.routingHeader.fromParams({
17941807
name: request.name || '',
17951808
});
17961809
this.initialize();
@@ -1891,7 +1904,7 @@ export class ReservationServiceClient {
18911904
options.otherArgs = options.otherArgs || {};
18921905
options.otherArgs.headers = options.otherArgs.headers || {};
18931906
options.otherArgs.headers['x-goog-request-params'] =
1894-
gax.routingHeader.fromParams({
1907+
this._gaxModule.routingHeader.fromParams({
18951908
'assignment.name': request.assignment!.name || '',
18961909
});
18971910
this.initialize();
@@ -1989,7 +2002,7 @@ export class ReservationServiceClient {
19892002
options.otherArgs = options.otherArgs || {};
19902003
options.otherArgs.headers = options.otherArgs.headers || {};
19912004
options.otherArgs.headers['x-goog-request-params'] =
1992-
gax.routingHeader.fromParams({
2005+
this._gaxModule.routingHeader.fromParams({
19932006
name: request.name || '',
19942007
});
19952008
this.initialize();
@@ -2095,7 +2108,7 @@ export class ReservationServiceClient {
20952108
options.otherArgs = options.otherArgs || {};
20962109
options.otherArgs.headers = options.otherArgs.headers || {};
20972110
options.otherArgs.headers['x-goog-request-params'] =
2098-
gax.routingHeader.fromParams({
2111+
this._gaxModule.routingHeader.fromParams({
20992112
'bi_reservation.name': request.biReservation!.name || '',
21002113
});
21012114
this.initialize();
@@ -2195,7 +2208,7 @@ export class ReservationServiceClient {
21952208
options.otherArgs = options.otherArgs || {};
21962209
options.otherArgs.headers = options.otherArgs.headers || {};
21972210
options.otherArgs.headers['x-goog-request-params'] =
2198-
gax.routingHeader.fromParams({
2211+
this._gaxModule.routingHeader.fromParams({
21992212
parent: request.parent || '',
22002213
});
22012214
this.initialize();
@@ -2234,7 +2247,7 @@ export class ReservationServiceClient {
22342247
options.otherArgs = options.otherArgs || {};
22352248
options.otherArgs.headers = options.otherArgs.headers || {};
22362249
options.otherArgs.headers['x-goog-request-params'] =
2237-
gax.routingHeader.fromParams({
2250+
this._gaxModule.routingHeader.fromParams({
22382251
parent: request.parent || '',
22392252
});
22402253
const defaultCallSettings = this._defaults['listReservations'];
@@ -2282,7 +2295,7 @@ export class ReservationServiceClient {
22822295
options.otherArgs = options.otherArgs || {};
22832296
options.otherArgs.headers = options.otherArgs.headers || {};
22842297
options.otherArgs.headers['x-goog-request-params'] =
2285-
gax.routingHeader.fromParams({
2298+
this._gaxModule.routingHeader.fromParams({
22862299
parent: request.parent || '',
22872300
});
22882301
const defaultCallSettings = this._defaults['listReservations'];
@@ -2387,7 +2400,7 @@ export class ReservationServiceClient {
23872400
options.otherArgs = options.otherArgs || {};
23882401
options.otherArgs.headers = options.otherArgs.headers || {};
23892402
options.otherArgs.headers['x-goog-request-params'] =
2390-
gax.routingHeader.fromParams({
2403+
this._gaxModule.routingHeader.fromParams({
23912404
parent: request.parent || '',
23922405
});
23932406
this.initialize();
@@ -2430,7 +2443,7 @@ export class ReservationServiceClient {
24302443
options.otherArgs = options.otherArgs || {};
24312444
options.otherArgs.headers = options.otherArgs.headers || {};
24322445
options.otherArgs.headers['x-goog-request-params'] =
2433-
gax.routingHeader.fromParams({
2446+
this._gaxModule.routingHeader.fromParams({
24342447
parent: request.parent || '',
24352448
});
24362449
const defaultCallSettings = this._defaults['listCapacityCommitments'];
@@ -2478,7 +2491,7 @@ export class ReservationServiceClient {
24782491
options.otherArgs = options.otherArgs || {};
24792492
options.otherArgs.headers = options.otherArgs.headers || {};
24802493
options.otherArgs.headers['x-goog-request-params'] =
2481-
gax.routingHeader.fromParams({
2494+
this._gaxModule.routingHeader.fromParams({
24822495
parent: request.parent || '',
24832496
});
24842497
const defaultCallSettings = this._defaults['listCapacityCommitments'];
@@ -2608,7 +2621,7 @@ export class ReservationServiceClient {
26082621
options.otherArgs = options.otherArgs || {};
26092622
options.otherArgs.headers = options.otherArgs.headers || {};
26102623
options.otherArgs.headers['x-goog-request-params'] =
2611-
gax.routingHeader.fromParams({
2624+
this._gaxModule.routingHeader.fromParams({
26122625
parent: request.parent || '',
26132626
});
26142627
this.initialize();
@@ -2652,7 +2665,7 @@ export class ReservationServiceClient {
26522665
options.otherArgs = options.otherArgs || {};
26532666
options.otherArgs.headers = options.otherArgs.headers || {};
26542667
options.otherArgs.headers['x-goog-request-params'] =
2655-
gax.routingHeader.fromParams({
2668+
this._gaxModule.routingHeader.fromParams({
26562669
parent: request.parent || '',
26572670
});
26582671
const defaultCallSettings = this._defaults['listAssignments'];
@@ -2705,7 +2718,7 @@ export class ReservationServiceClient {
27052718
options.otherArgs = options.otherArgs || {};
27062719
options.otherArgs.headers = options.otherArgs.headers || {};
27072720
options.otherArgs.headers['x-goog-request-params'] =
2708-
gax.routingHeader.fromParams({
2721+
this._gaxModule.routingHeader.fromParams({
27092722
parent: request.parent || '',
27102723
});
27112724
const defaultCallSettings = this._defaults['listAssignments'];
@@ -2842,7 +2855,7 @@ export class ReservationServiceClient {
28422855
options.otherArgs = options.otherArgs || {};
28432856
options.otherArgs.headers = options.otherArgs.headers || {};
28442857
options.otherArgs.headers['x-goog-request-params'] =
2845-
gax.routingHeader.fromParams({
2858+
this._gaxModule.routingHeader.fromParams({
28462859
parent: request.parent || '',
28472860
});
28482861
this.initialize();
@@ -2896,7 +2909,7 @@ export class ReservationServiceClient {
28962909
options.otherArgs = options.otherArgs || {};
28972910
options.otherArgs.headers = options.otherArgs.headers || {};
28982911
options.otherArgs.headers['x-goog-request-params'] =
2899-
gax.routingHeader.fromParams({
2912+
this._gaxModule.routingHeader.fromParams({
29002913
parent: request.parent || '',
29012914
});
29022915
const defaultCallSettings = this._defaults['searchAssignments'];
@@ -2959,7 +2972,7 @@ export class ReservationServiceClient {
29592972
options.otherArgs = options.otherArgs || {};
29602973
options.otherArgs.headers = options.otherArgs.headers || {};
29612974
options.otherArgs.headers['x-goog-request-params'] =
2962-
gax.routingHeader.fromParams({
2975+
this._gaxModule.routingHeader.fromParams({
29632976
parent: request.parent || '',
29642977
});
29652978
const defaultCallSettings = this._defaults['searchAssignments'];
@@ -3097,7 +3110,7 @@ export class ReservationServiceClient {
30973110
options.otherArgs = options.otherArgs || {};
30983111
options.otherArgs.headers = options.otherArgs.headers || {};
30993112
options.otherArgs.headers['x-goog-request-params'] =
3100-
gax.routingHeader.fromParams({
3113+
this._gaxModule.routingHeader.fromParams({
31013114
parent: request.parent || '',
31023115
});
31033116
this.initialize();
@@ -3145,7 +3158,7 @@ export class ReservationServiceClient {
31453158
options.otherArgs = options.otherArgs || {};
31463159
options.otherArgs.headers = options.otherArgs.headers || {};
31473160
options.otherArgs.headers['x-goog-request-params'] =
3148-
gax.routingHeader.fromParams({
3161+
this._gaxModule.routingHeader.fromParams({
31493162
parent: request.parent || '',
31503163
});
31513164
const defaultCallSettings = this._defaults['searchAllAssignments'];
@@ -3202,7 +3215,7 @@ export class ReservationServiceClient {
32023215
options.otherArgs = options.otherArgs || {};
32033216
options.otherArgs.headers = options.otherArgs.headers || {};
32043217
options.otherArgs.headers['x-goog-request-params'] =
3205-
gax.routingHeader.fromParams({
3218+
this._gaxModule.routingHeader.fromParams({
32063219
parent: request.parent || '',
32073220
});
32083221
const defaultCallSettings = this._defaults['searchAllAssignments'];

0 commit comments

Comments
 (0)