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

Commit 9054e83

Browse files
fix: allow passing gax instance to client constructor (#80)
- [ ] 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 a0924da commit 9054e83

6 files changed

Lines changed: 162 additions & 66 deletions

src/v1beta3/flex_templates_service_client.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './flex_templates_service_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -87,8 +91,18 @@ export class FlexTemplatesServiceClient {
8791
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
8892
* For more information, please check the
8993
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
94+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
95+
* need to avoid loading the default gRPC version and want to use the fallback
96+
* HTTP implementation. Load only fallback version and pass it to the constructor:
97+
* ```
98+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
99+
* const client = new FlexTemplatesServiceClient({fallback: 'rest'}, gax);
100+
* ```
90101
*/
91-
constructor(opts?: ClientOptions) {
102+
constructor(
103+
opts?: ClientOptions,
104+
gaxInstance?: typeof gax | typeof gax.fallback
105+
) {
92106
// Ensure that options include all the required fields.
93107
const staticMembers = this.constructor as typeof FlexTemplatesServiceClient;
94108
const servicePath =
@@ -108,8 +122,13 @@ export class FlexTemplatesServiceClient {
108122
opts['scopes'] = staticMembers.scopes;
109123
}
110124

125+
// Load google-gax module synchronously if needed
126+
if (!gaxInstance) {
127+
gaxInstance = require('google-gax') as typeof gax;
128+
}
129+
111130
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
112-
this._gaxModule = opts.fallback ? gax.fallback : gax;
131+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
113132

114133
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
115134
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -163,7 +182,7 @@ export class FlexTemplatesServiceClient {
163182
this.innerApiCalls = {};
164183

165184
// Add a warn function to the client constructor so it can be easily tested.
166-
this.warn = gax.warn;
185+
this.warn = this._gaxModule.warn;
167186
}
168187

169188
/**
@@ -380,7 +399,7 @@ export class FlexTemplatesServiceClient {
380399
options.otherArgs = options.otherArgs || {};
381400
options.otherArgs.headers = options.otherArgs.headers || {};
382401
options.otherArgs.headers['x-goog-request-params'] =
383-
gax.routingHeader.fromParams({
402+
this._gaxModule.routingHeader.fromParams({
384403
project_id: request.projectId || '',
385404
location: request.location || '',
386405
});

src/v1beta3/jobs_v1_beta3_client.ts

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

4240
/**
@@ -96,8 +94,18 @@ export class JobsV1Beta3Client {
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 JobsV1Beta3Client({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 JobsV1Beta3Client;
103111
const servicePath =
@@ -117,8 +125,13 @@ export class JobsV1Beta3Client {
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);
@@ -188,7 +201,7 @@ export class JobsV1Beta3Client {
188201
this.innerApiCalls = {};
189202

190203
// Add a warn function to the client constructor so it can be easily tested.
191-
this.warn = gax.warn;
204+
this.warn = this._gaxModule.warn;
192205
}
193206

194207
/**
@@ -412,7 +425,7 @@ export class JobsV1Beta3Client {
412425
options.otherArgs = options.otherArgs || {};
413426
options.otherArgs.headers = options.otherArgs.headers || {};
414427
options.otherArgs.headers['x-goog-request-params'] =
415-
gax.routingHeader.fromParams({
428+
this._gaxModule.routingHeader.fromParams({
416429
project_id: request.projectId || '',
417430
location: request.location || '',
418431
});
@@ -510,7 +523,7 @@ export class JobsV1Beta3Client {
510523
options.otherArgs = options.otherArgs || {};
511524
options.otherArgs.headers = options.otherArgs.headers || {};
512525
options.otherArgs.headers['x-goog-request-params'] =
513-
gax.routingHeader.fromParams({
526+
this._gaxModule.routingHeader.fromParams({
514527
project_id: request.projectId || '',
515528
location: request.location || '',
516529
job_id: request.jobId || '',
@@ -610,7 +623,7 @@ export class JobsV1Beta3Client {
610623
options.otherArgs = options.otherArgs || {};
611624
options.otherArgs.headers = options.otherArgs.headers || {};
612625
options.otherArgs.headers['x-goog-request-params'] =
613-
gax.routingHeader.fromParams({
626+
this._gaxModule.routingHeader.fromParams({
614627
project_id: request.projectId || '',
615628
location: request.location || '',
616629
job_id: request.jobId || '',
@@ -786,7 +799,7 @@ export class JobsV1Beta3Client {
786799
options.otherArgs = options.otherArgs || {};
787800
options.otherArgs.headers = options.otherArgs.headers || {};
788801
options.otherArgs.headers['x-goog-request-params'] =
789-
gax.routingHeader.fromParams({
802+
this._gaxModule.routingHeader.fromParams({
790803
project_id: request.projectId || '',
791804
location: request.location || '',
792805
job_id: request.jobId || '',
@@ -898,7 +911,7 @@ export class JobsV1Beta3Client {
898911
options.otherArgs = options.otherArgs || {};
899912
options.otherArgs.headers = options.otherArgs.headers || {};
900913
options.otherArgs.headers['x-goog-request-params'] =
901-
gax.routingHeader.fromParams({
914+
this._gaxModule.routingHeader.fromParams({
902915
project_id: request.projectId || '',
903916
location: request.location || '',
904917
});
@@ -949,7 +962,7 @@ export class JobsV1Beta3Client {
949962
options.otherArgs = options.otherArgs || {};
950963
options.otherArgs.headers = options.otherArgs.headers || {};
951964
options.otherArgs.headers['x-goog-request-params'] =
952-
gax.routingHeader.fromParams({
965+
this._gaxModule.routingHeader.fromParams({
953966
project_id: request.projectId || '',
954967
location: request.location || '',
955968
});
@@ -1009,7 +1022,7 @@ export class JobsV1Beta3Client {
10091022
options.otherArgs = options.otherArgs || {};
10101023
options.otherArgs.headers = options.otherArgs.headers || {};
10111024
options.otherArgs.headers['x-goog-request-params'] =
1012-
gax.routingHeader.fromParams({
1025+
this._gaxModule.routingHeader.fromParams({
10131026
project_id: request.projectId || '',
10141027
location: request.location || '',
10151028
});
@@ -1118,7 +1131,7 @@ export class JobsV1Beta3Client {
11181131
options.otherArgs = options.otherArgs || {};
11191132
options.otherArgs.headers = options.otherArgs.headers || {};
11201133
options.otherArgs.headers['x-goog-request-params'] =
1121-
gax.routingHeader.fromParams({
1134+
this._gaxModule.routingHeader.fromParams({
11221135
project_id: request.projectId || '',
11231136
});
11241137
this.initialize();
@@ -1168,7 +1181,7 @@ export class JobsV1Beta3Client {
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
project_id: request.projectId || '',
11731186
});
11741187
const defaultCallSettings = this._defaults['aggregatedListJobs'];
@@ -1227,7 +1240,7 @@ export class JobsV1Beta3Client {
12271240
options.otherArgs = options.otherArgs || {};
12281241
options.otherArgs.headers = options.otherArgs.headers || {};
12291242
options.otherArgs.headers['x-goog-request-params'] =
1230-
gax.routingHeader.fromParams({
1243+
this._gaxModule.routingHeader.fromParams({
12311244
project_id: request.projectId || '',
12321245
});
12331246
const defaultCallSettings = this._defaults['aggregatedListJobs'];

src/v1beta3/messages_v1_beta3_client.ts

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

4240
/**
@@ -96,8 +94,18 @@ export class MessagesV1Beta3Client {
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 MessagesV1Beta3Client({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 MessagesV1Beta3Client;
103111
const servicePath =
@@ -117,8 +125,13 @@ export class MessagesV1Beta3Client {
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);
@@ -183,7 +196,7 @@ export class MessagesV1Beta3Client {
183196
this.innerApiCalls = {};
184197

185198
// Add a warn function to the client constructor so it can be easily tested.
186-
this.warn = gax.warn;
199+
this.warn = this._gaxModule.warn;
187200
}
188201

189202
/**
@@ -423,7 +436,7 @@ export class MessagesV1Beta3Client {
423436
options.otherArgs = options.otherArgs || {};
424437
options.otherArgs.headers = options.otherArgs.headers || {};
425438
options.otherArgs.headers['x-goog-request-params'] =
426-
gax.routingHeader.fromParams({
439+
this._gaxModule.routingHeader.fromParams({
427440
project_id: request.projectId || '',
428441
location: request.location || '',
429442
job_id: request.jobId || '',
@@ -481,7 +494,7 @@ export class MessagesV1Beta3Client {
481494
options.otherArgs = options.otherArgs || {};
482495
options.otherArgs.headers = options.otherArgs.headers || {};
483496
options.otherArgs.headers['x-goog-request-params'] =
484-
gax.routingHeader.fromParams({
497+
this._gaxModule.routingHeader.fromParams({
485498
project_id: request.projectId || '',
486499
location: request.location || '',
487500
job_id: request.jobId || '',
@@ -548,7 +561,7 @@ export class MessagesV1Beta3Client {
548561
options.otherArgs = options.otherArgs || {};
549562
options.otherArgs.headers = options.otherArgs.headers || {};
550563
options.otherArgs.headers['x-goog-request-params'] =
551-
gax.routingHeader.fromParams({
564+
this._gaxModule.routingHeader.fromParams({
552565
project_id: request.projectId || '',
553566
location: request.location || '',
554567
job_id: request.jobId || '',

0 commit comments

Comments
 (0)