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

Commit 9c751ed

Browse files
fix: allow passing gax instance to client constructor (#155)
- [ ] 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 d039b24 commit 9c751ed

3 files changed

Lines changed: 80 additions & 23 deletions

File tree

src/v1/quota_controller_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 './quota_controller_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -90,8 +94,18 @@ export class QuotaControllerClient {
9094
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9195
* For more information, please check the
9296
* {@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 QuotaControllerClient({fallback: 'rest'}, gax);
103+
* ```
93104
*/
94-
constructor(opts?: ClientOptions) {
105+
constructor(
106+
opts?: ClientOptions,
107+
gaxInstance?: typeof gax | typeof gax.fallback
108+
) {
95109
// Ensure that options include all the required fields.
96110
const staticMembers = this.constructor as typeof QuotaControllerClient;
97111
const servicePath =
@@ -111,8 +125,13 @@ export class QuotaControllerClient {
111125
opts['scopes'] = staticMembers.scopes;
112126
}
113127

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

117136
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
118137
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -166,7 +185,7 @@ export class QuotaControllerClient {
166185
this.innerApiCalls = {};
167186

168187
// Add a warn function to the client constructor so it can be easily tested.
169-
this.warn = gax.warn;
188+
this.warn = this._gaxModule.warn;
170189
}
171190

172191
/**
@@ -391,7 +410,7 @@ export class QuotaControllerClient {
391410
options.otherArgs = options.otherArgs || {};
392411
options.otherArgs.headers = options.otherArgs.headers || {};
393412
options.otherArgs.headers['x-goog-request-params'] =
394-
gax.routingHeader.fromParams({
413+
this._gaxModule.routingHeader.fromParams({
395414
service_name: request.serviceName || '',
396415
});
397416
this.initialize();

src/v1/service_controller_client.ts

Lines changed: 27 additions & 8 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 './service_controller_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -90,8 +94,18 @@ export class ServiceControllerClient {
9094
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9195
* For more information, please check the
9296
* {@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 ServiceControllerClient({fallback: 'rest'}, gax);
103+
* ```
93104
*/
94-
constructor(opts?: ClientOptions) {
105+
constructor(
106+
opts?: ClientOptions,
107+
gaxInstance?: typeof gax | typeof gax.fallback
108+
) {
95109
// Ensure that options include all the required fields.
96110
const staticMembers = this.constructor as typeof ServiceControllerClient;
97111
const servicePath =
@@ -111,8 +125,13 @@ export class ServiceControllerClient {
111125
opts['scopes'] = staticMembers.scopes;
112126
}
113127

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

117136
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
118137
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -166,7 +185,7 @@ export class ServiceControllerClient {
166185
this.innerApiCalls = {};
167186

168187
// Add a warn function to the client constructor so it can be easily tested.
169-
this.warn = gax.warn;
188+
this.warn = this._gaxModule.warn;
170189
}
171190

172191
/**
@@ -393,7 +412,7 @@ export class ServiceControllerClient {
393412
options.otherArgs = options.otherArgs || {};
394413
options.otherArgs.headers = options.otherArgs.headers || {};
395414
options.otherArgs.headers['x-goog-request-params'] =
396-
gax.routingHeader.fromParams({
415+
this._gaxModule.routingHeader.fromParams({
397416
service_name: request.serviceName || '',
398417
});
399418
this.initialize();
@@ -513,7 +532,7 @@ export class ServiceControllerClient {
513532
options.otherArgs = options.otherArgs || {};
514533
options.otherArgs.headers = options.otherArgs.headers || {};
515534
options.otherArgs.headers['x-goog-request-params'] =
516-
gax.routingHeader.fromParams({
535+
this._gaxModule.routingHeader.fromParams({
517536
service_name: request.serviceName || '',
518537
});
519538
this.initialize();

src/v2/service_controller_client.ts

Lines changed: 27 additions & 8 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 './service_controller_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -94,8 +98,18 @@ export class ServiceControllerClient {
9498
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9599
* For more information, please check the
96100
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
101+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
102+
* need to avoid loading the default gRPC version and want to use the fallback
103+
* HTTP implementation. Load only fallback version and pass it to the constructor:
104+
* ```
105+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
106+
* const client = new ServiceControllerClient({fallback: 'rest'}, gax);
107+
* ```
97108
*/
98-
constructor(opts?: ClientOptions) {
109+
constructor(
110+
opts?: ClientOptions,
111+
gaxInstance?: typeof gax | typeof gax.fallback
112+
) {
99113
// Ensure that options include all the required fields.
100114
const staticMembers = this.constructor as typeof ServiceControllerClient;
101115
const servicePath =
@@ -115,8 +129,13 @@ export class ServiceControllerClient {
115129
opts['scopes'] = staticMembers.scopes;
116130
}
117131

132+
// Load google-gax module synchronously if needed
133+
if (!gaxInstance) {
134+
gaxInstance = require('google-gax') as typeof gax;
135+
}
136+
118137
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
119-
this._gaxModule = opts.fallback ? gax.fallback : gax;
138+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
120139

121140
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
122141
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -170,7 +189,7 @@ export class ServiceControllerClient {
170189
this.innerApiCalls = {};
171190

172191
// Add a warn function to the client constructor so it can be easily tested.
173-
this.warn = gax.warn;
192+
this.warn = this._gaxModule.warn;
174193
}
175194

176195
/**
@@ -404,7 +423,7 @@ export class ServiceControllerClient {
404423
options.otherArgs = options.otherArgs || {};
405424
options.otherArgs.headers = options.otherArgs.headers || {};
406425
options.otherArgs.headers['x-goog-request-params'] =
407-
gax.routingHeader.fromParams({
426+
this._gaxModule.routingHeader.fromParams({
408427
service_name: request.serviceName || '',
409428
});
410429
this.initialize();
@@ -517,7 +536,7 @@ export class ServiceControllerClient {
517536
options.otherArgs = options.otherArgs || {};
518537
options.otherArgs.headers = options.otherArgs.headers || {};
519538
options.otherArgs.headers['x-goog-request-params'] =
520-
gax.routingHeader.fromParams({
539+
this._gaxModule.routingHeader.fromParams({
521540
service_name: request.serviceName || '',
522541
});
523542
this.initialize();

0 commit comments

Comments
 (0)