Skip to content

Commit 79467a8

Browse files
fix: allow passing gax instance to client constructor (#150)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: googleapis/googleapis-gen@f16a1d2 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: googleapis/googleapis-gen@e97a1ac Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
1 parent b6db61d commit 79467a8

File tree

2 files changed

+134
-94
lines changed

2 files changed

+134
-94
lines changed

packages/google-cloud-managedidentities/src/v1/managed_identities_service_client.ts

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
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,
@@ -28,7 +28,6 @@ import {
2828
PaginationCallback,
2929
GaxCall,
3030
} from 'google-gax';
31-
3231
import {Transform} from 'stream';
3332
import * as protos from '../../protos/protos';
3433
import jsonProtos = require('../../protos/protos.json');
@@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json');
3837
* This file defines retry strategy and timeouts for all API methods in this library.
3938
*/
4039
import * as gapicConfig from './managed_identities_service_client_config.json';
41-
import {operationsProtos} from 'google-gax';
4240
const version = require('../../../package.json').version;
4341

4442
/**
@@ -130,8 +128,18 @@ export class ManagedIdentitiesServiceClient {
130128
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
131129
* For more information, please check the
132130
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
131+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
132+
* need to avoid loading the default gRPC version and want to use the fallback
133+
* HTTP implementation. Load only fallback version and pass it to the constructor:
134+
* ```
135+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
136+
* const client = new ManagedIdentitiesServiceClient({fallback: 'rest'}, gax);
137+
* ```
133138
*/
134-
constructor(opts?: ClientOptions) {
139+
constructor(
140+
opts?: ClientOptions,
141+
gaxInstance?: typeof gax | typeof gax.fallback
142+
) {
135143
// Ensure that options include all the required fields.
136144
const staticMembers = this
137145
.constructor as typeof ManagedIdentitiesServiceClient;
@@ -152,8 +160,13 @@ export class ManagedIdentitiesServiceClient {
152160
opts['scopes'] = staticMembers.scopes;
153161
}
154162

163+
// Load google-gax module synchronously if needed
164+
if (!gaxInstance) {
165+
gaxInstance = require('google-gax') as typeof gax;
166+
}
167+
155168
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
156-
this._gaxModule = opts.fallback ? gax.fallback : gax;
169+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
157170

158171
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
159172
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -401,7 +414,7 @@ export class ManagedIdentitiesServiceClient {
401414
this.innerApiCalls = {};
402415

403416
// Add a warn function to the client constructor so it can be easily tested.
404-
this.warn = gax.warn;
417+
this.warn = this._gaxModule.warn;
405418
}
406419

407420
/**
@@ -626,7 +639,7 @@ export class ManagedIdentitiesServiceClient {
626639
options.otherArgs = options.otherArgs || {};
627640
options.otherArgs.headers = options.otherArgs.headers || {};
628641
options.otherArgs.headers['x-goog-request-params'] =
629-
gax.routingHeader.fromParams({
642+
this._gaxModule.routingHeader.fromParams({
630643
name: request.name || '',
631644
});
632645
this.initialize();
@@ -718,7 +731,7 @@ export class ManagedIdentitiesServiceClient {
718731
options.otherArgs = options.otherArgs || {};
719732
options.otherArgs.headers = options.otherArgs.headers || {};
720733
options.otherArgs.headers['x-goog-request-params'] =
721-
gax.routingHeader.fromParams({
734+
this._gaxModule.routingHeader.fromParams({
722735
name: request.name || '',
723736
});
724737
this.initialize();
@@ -838,7 +851,7 @@ export class ManagedIdentitiesServiceClient {
838851
options.otherArgs = options.otherArgs || {};
839852
options.otherArgs.headers = options.otherArgs.headers || {};
840853
options.otherArgs.headers['x-goog-request-params'] =
841-
gax.routingHeader.fromParams({
854+
this._gaxModule.routingHeader.fromParams({
842855
parent: request.parent || '',
843856
});
844857
this.initialize();
@@ -868,11 +881,12 @@ export class ManagedIdentitiesServiceClient {
868881
protos.google.cloud.managedidentities.v1.OpMetadata
869882
>
870883
> {
871-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
872-
{name}
873-
);
884+
const request =
885+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
886+
{name}
887+
);
874888
const [operation] = await this.operationsClient.getOperation(request);
875-
const decodeOperation = new gax.Operation(
889+
const decodeOperation = new this._gaxModule.Operation(
876890
operation,
877891
this.descriptors.longrunning.createMicrosoftAdDomain,
878892
this._gaxModule.createDefaultBackoffSettings()
@@ -987,7 +1001,7 @@ export class ManagedIdentitiesServiceClient {
9871001
options.otherArgs = options.otherArgs || {};
9881002
options.otherArgs.headers = options.otherArgs.headers || {};
9891003
options.otherArgs.headers['x-goog-request-params'] =
990-
gax.routingHeader.fromParams({
1004+
this._gaxModule.routingHeader.fromParams({
9911005
'domain.name': request.domain!.name || '',
9921006
});
9931007
this.initialize();
@@ -1013,11 +1027,12 @@ export class ManagedIdentitiesServiceClient {
10131027
protos.google.cloud.managedidentities.v1.OpMetadata
10141028
>
10151029
> {
1016-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1017-
{name}
1018-
);
1030+
const request =
1031+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1032+
{name}
1033+
);
10191034
const [operation] = await this.operationsClient.getOperation(request);
1020-
const decodeOperation = new gax.Operation(
1035+
const decodeOperation = new this._gaxModule.Operation(
10211036
operation,
10221037
this.descriptors.longrunning.updateDomain,
10231038
this._gaxModule.createDefaultBackoffSettings()
@@ -1125,7 +1140,7 @@ export class ManagedIdentitiesServiceClient {
11251140
options.otherArgs = options.otherArgs || {};
11261141
options.otherArgs.headers = options.otherArgs.headers || {};
11271142
options.otherArgs.headers['x-goog-request-params'] =
1128-
gax.routingHeader.fromParams({
1143+
this._gaxModule.routingHeader.fromParams({
11291144
name: request.name || '',
11301145
});
11311146
this.initialize();
@@ -1151,11 +1166,12 @@ export class ManagedIdentitiesServiceClient {
11511166
protos.google.cloud.managedidentities.v1.OpMetadata
11521167
>
11531168
> {
1154-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1155-
{name}
1156-
);
1169+
const request =
1170+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1171+
{name}
1172+
);
11571173
const [operation] = await this.operationsClient.getOperation(request);
1158-
const decodeOperation = new gax.Operation(
1174+
const decodeOperation = new this._gaxModule.Operation(
11591175
operation,
11601176
this.descriptors.longrunning.deleteDomain,
11611177
this._gaxModule.createDefaultBackoffSettings()
@@ -1265,7 +1281,7 @@ export class ManagedIdentitiesServiceClient {
12651281
options.otherArgs = options.otherArgs || {};
12661282
options.otherArgs.headers = options.otherArgs.headers || {};
12671283
options.otherArgs.headers['x-goog-request-params'] =
1268-
gax.routingHeader.fromParams({
1284+
this._gaxModule.routingHeader.fromParams({
12691285
name: request.name || '',
12701286
});
12711287
this.initialize();
@@ -1291,11 +1307,12 @@ export class ManagedIdentitiesServiceClient {
12911307
protos.google.cloud.managedidentities.v1.OpMetadata
12921308
>
12931309
> {
1294-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1295-
{name}
1296-
);
1310+
const request =
1311+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1312+
{name}
1313+
);
12971314
const [operation] = await this.operationsClient.getOperation(request);
1298-
const decodeOperation = new gax.Operation(
1315+
const decodeOperation = new this._gaxModule.Operation(
12991316
operation,
13001317
this.descriptors.longrunning.attachTrust,
13011318
this._gaxModule.createDefaultBackoffSettings()
@@ -1409,7 +1426,7 @@ export class ManagedIdentitiesServiceClient {
14091426
options.otherArgs = options.otherArgs || {};
14101427
options.otherArgs.headers = options.otherArgs.headers || {};
14111428
options.otherArgs.headers['x-goog-request-params'] =
1412-
gax.routingHeader.fromParams({
1429+
this._gaxModule.routingHeader.fromParams({
14131430
name: request.name || '',
14141431
});
14151432
this.initialize();
@@ -1435,11 +1452,12 @@ export class ManagedIdentitiesServiceClient {
14351452
protos.google.cloud.managedidentities.v1.OpMetadata
14361453
>
14371454
> {
1438-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1439-
{name}
1440-
);
1455+
const request =
1456+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1457+
{name}
1458+
);
14411459
const [operation] = await this.operationsClient.getOperation(request);
1442-
const decodeOperation = new gax.Operation(
1460+
const decodeOperation = new this._gaxModule.Operation(
14431461
operation,
14441462
this.descriptors.longrunning.reconfigureTrust,
14451463
this._gaxModule.createDefaultBackoffSettings()
@@ -1549,7 +1567,7 @@ export class ManagedIdentitiesServiceClient {
15491567
options.otherArgs = options.otherArgs || {};
15501568
options.otherArgs.headers = options.otherArgs.headers || {};
15511569
options.otherArgs.headers['x-goog-request-params'] =
1552-
gax.routingHeader.fromParams({
1570+
this._gaxModule.routingHeader.fromParams({
15531571
name: request.name || '',
15541572
});
15551573
this.initialize();
@@ -1575,11 +1593,12 @@ export class ManagedIdentitiesServiceClient {
15751593
protos.google.cloud.managedidentities.v1.OpMetadata
15761594
>
15771595
> {
1578-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1579-
{name}
1580-
);
1596+
const request =
1597+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1598+
{name}
1599+
);
15811600
const [operation] = await this.operationsClient.getOperation(request);
1582-
const decodeOperation = new gax.Operation(
1601+
const decodeOperation = new this._gaxModule.Operation(
15831602
operation,
15841603
this.descriptors.longrunning.detachTrust,
15851604
this._gaxModule.createDefaultBackoffSettings()
@@ -1690,7 +1709,7 @@ export class ManagedIdentitiesServiceClient {
16901709
options.otherArgs = options.otherArgs || {};
16911710
options.otherArgs.headers = options.otherArgs.headers || {};
16921711
options.otherArgs.headers['x-goog-request-params'] =
1693-
gax.routingHeader.fromParams({
1712+
this._gaxModule.routingHeader.fromParams({
16941713
name: request.name || '',
16951714
});
16961715
this.initialize();
@@ -1716,11 +1735,12 @@ export class ManagedIdentitiesServiceClient {
17161735
protos.google.cloud.managedidentities.v1.OpMetadata
17171736
>
17181737
> {
1719-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1720-
{name}
1721-
);
1738+
const request =
1739+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1740+
{name}
1741+
);
17221742
const [operation] = await this.operationsClient.getOperation(request);
1723-
const decodeOperation = new gax.Operation(
1743+
const decodeOperation = new this._gaxModule.Operation(
17241744
operation,
17251745
this.descriptors.longrunning.validateTrust,
17261746
this._gaxModule.createDefaultBackoffSettings()
@@ -1837,7 +1857,7 @@ export class ManagedIdentitiesServiceClient {
18371857
options.otherArgs = options.otherArgs || {};
18381858
options.otherArgs.headers = options.otherArgs.headers || {};
18391859
options.otherArgs.headers['x-goog-request-params'] =
1840-
gax.routingHeader.fromParams({
1860+
this._gaxModule.routingHeader.fromParams({
18411861
parent: request.parent || '',
18421862
});
18431863
this.initialize();
@@ -1890,7 +1910,7 @@ export class ManagedIdentitiesServiceClient {
18901910
options.otherArgs = options.otherArgs || {};
18911911
options.otherArgs.headers = options.otherArgs.headers || {};
18921912
options.otherArgs.headers['x-goog-request-params'] =
1893-
gax.routingHeader.fromParams({
1913+
this._gaxModule.routingHeader.fromParams({
18941914
parent: request.parent || '',
18951915
});
18961916
const defaultCallSettings = this._defaults['listDomains'];
@@ -1952,7 +1972,7 @@ export class ManagedIdentitiesServiceClient {
19521972
options.otherArgs = options.otherArgs || {};
19531973
options.otherArgs.headers = options.otherArgs.headers || {};
19541974
options.otherArgs.headers['x-goog-request-params'] =
1955-
gax.routingHeader.fromParams({
1975+
this._gaxModule.routingHeader.fromParams({
19561976
parent: request.parent || '',
19571977
});
19581978
const defaultCallSettings = this._defaults['listDomains'];

0 commit comments

Comments
 (0)