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

Commit 6ef4a52

Browse files
fix: allow passing gax instance to client constructor (#177)
- [ ] 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 1a53ab4 commit 6ef4a52

5 files changed

Lines changed: 136 additions & 53 deletions

src/v1/binauthz_management_service_v1_client.ts

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

4240
/**
@@ -102,8 +100,18 @@ export class BinauthzManagementServiceV1Client {
102100
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
103101
* For more information, please check the
104102
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
103+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
104+
* need to avoid loading the default gRPC version and want to use the fallback
105+
* HTTP implementation. Load only fallback version and pass it to the constructor:
106+
* ```
107+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
108+
* const client = new BinauthzManagementServiceV1Client({fallback: 'rest'}, gax);
109+
* ```
105110
*/
106-
constructor(opts?: ClientOptions) {
111+
constructor(
112+
opts?: ClientOptions,
113+
gaxInstance?: typeof gax | typeof gax.fallback
114+
) {
107115
// Ensure that options include all the required fields.
108116
const staticMembers = this
109117
.constructor as typeof BinauthzManagementServiceV1Client;
@@ -124,8 +132,13 @@ export class BinauthzManagementServiceV1Client {
124132
opts['scopes'] = staticMembers.scopes;
125133
}
126134

135+
// Load google-gax module synchronously if needed
136+
if (!gaxInstance) {
137+
gaxInstance = require('google-gax') as typeof gax;
138+
}
139+
127140
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
128-
this._gaxModule = opts.fallback ? gax.fallback : gax;
141+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
129142

130143
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
131144
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -208,7 +221,7 @@ export class BinauthzManagementServiceV1Client {
208221
this.innerApiCalls = {};
209222

210223
// Add a warn function to the client constructor so it can be easily tested.
211-
this.warn = gax.warn;
224+
this.warn = this._gaxModule.warn;
212225
}
213226

214227
/**
@@ -427,7 +440,7 @@ export class BinauthzManagementServiceV1Client {
427440
options.otherArgs = options.otherArgs || {};
428441
options.otherArgs.headers = options.otherArgs.headers || {};
429442
options.otherArgs.headers['x-goog-request-params'] =
430-
gax.routingHeader.fromParams({
443+
this._gaxModule.routingHeader.fromParams({
431444
name: request.name || '',
432445
});
433446
this.initialize();
@@ -530,7 +543,7 @@ export class BinauthzManagementServiceV1Client {
530543
options.otherArgs = options.otherArgs || {};
531544
options.otherArgs.headers = options.otherArgs.headers || {};
532545
options.otherArgs.headers['x-goog-request-params'] =
533-
gax.routingHeader.fromParams({
546+
this._gaxModule.routingHeader.fromParams({
534547
'policy.name': request.policy!.name || '',
535548
});
536549
this.initialize();
@@ -636,7 +649,7 @@ export class BinauthzManagementServiceV1Client {
636649
options.otherArgs = options.otherArgs || {};
637650
options.otherArgs.headers = options.otherArgs.headers || {};
638651
options.otherArgs.headers['x-goog-request-params'] =
639-
gax.routingHeader.fromParams({
652+
this._gaxModule.routingHeader.fromParams({
640653
parent: request.parent || '',
641654
});
642655
this.initialize();
@@ -735,7 +748,7 @@ export class BinauthzManagementServiceV1Client {
735748
options.otherArgs = options.otherArgs || {};
736749
options.otherArgs.headers = options.otherArgs.headers || {};
737750
options.otherArgs.headers['x-goog-request-params'] =
738-
gax.routingHeader.fromParams({
751+
this._gaxModule.routingHeader.fromParams({
739752
name: request.name || '',
740753
});
741754
this.initialize();
@@ -835,7 +848,7 @@ export class BinauthzManagementServiceV1Client {
835848
options.otherArgs = options.otherArgs || {};
836849
options.otherArgs.headers = options.otherArgs.headers || {};
837850
options.otherArgs.headers['x-goog-request-params'] =
838-
gax.routingHeader.fromParams({
851+
this._gaxModule.routingHeader.fromParams({
839852
'attestor.name': request.attestor!.name || '',
840853
});
841854
this.initialize();
@@ -934,7 +947,7 @@ export class BinauthzManagementServiceV1Client {
934947
options.otherArgs = options.otherArgs || {};
935948
options.otherArgs.headers = options.otherArgs.headers || {};
936949
options.otherArgs.headers['x-goog-request-params'] =
937-
gax.routingHeader.fromParams({
950+
this._gaxModule.routingHeader.fromParams({
938951
name: request.name || '',
939952
});
940953
this.initialize();
@@ -1038,7 +1051,7 @@ export class BinauthzManagementServiceV1Client {
10381051
options.otherArgs = options.otherArgs || {};
10391052
options.otherArgs.headers = options.otherArgs.headers || {};
10401053
options.otherArgs.headers['x-goog-request-params'] =
1041-
gax.routingHeader.fromParams({
1054+
this._gaxModule.routingHeader.fromParams({
10421055
parent: request.parent || '',
10431056
});
10441057
this.initialize();
@@ -1080,7 +1093,7 @@ export class BinauthzManagementServiceV1Client {
10801093
options.otherArgs = options.otherArgs || {};
10811094
options.otherArgs.headers = options.otherArgs.headers || {};
10821095
options.otherArgs.headers['x-goog-request-params'] =
1083-
gax.routingHeader.fromParams({
1096+
this._gaxModule.routingHeader.fromParams({
10841097
parent: request.parent || '',
10851098
});
10861099
const defaultCallSettings = this._defaults['listAttestors'];
@@ -1131,7 +1144,7 @@ export class BinauthzManagementServiceV1Client {
11311144
options.otherArgs = options.otherArgs || {};
11321145
options.otherArgs.headers = options.otherArgs.headers || {};
11331146
options.otherArgs.headers['x-goog-request-params'] =
1134-
gax.routingHeader.fromParams({
1147+
this._gaxModule.routingHeader.fromParams({
11351148
parent: request.parent || '',
11361149
});
11371150
const defaultCallSettings = this._defaults['listAttestors'];

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

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

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

115134
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
116135
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -182,7 +201,7 @@ export class SystemPolicyV1Client {
182201
this.innerApiCalls = {};
183202

184203
// Add a warn function to the client constructor so it can be easily tested.
185-
this.warn = gax.warn;
204+
this.warn = this._gaxModule.warn;
186205
}
187206

188207
/**
@@ -393,7 +412,7 @@ export class SystemPolicyV1Client {
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
name: request.name || '',
398417
});
399418
this.initialize();

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

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

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

115134
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
116135
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -182,7 +201,7 @@ export class ValidationHelperV1Client {
182201
this.innerApiCalls = {};
183202

184203
// Add a warn function to the client constructor so it can be easily tested.
185-
this.warn = gax.warn;
204+
this.warn = this._gaxModule.warn;
186205
}
187206

188207
/**
@@ -406,7 +425,7 @@ export class ValidationHelperV1Client {
406425
options.otherArgs = options.otherArgs || {};
407426
options.otherArgs.headers = options.otherArgs.headers || {};
408427
options.otherArgs.headers['x-goog-request-params'] =
409-
gax.routingHeader.fromParams({
428+
this._gaxModule.routingHeader.fromParams({
410429
attestor: request.attestor || '',
411430
});
412431
this.initialize();

0 commit comments

Comments
 (0)