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

Commit 2de7b16

Browse files
fix: allow passing gax instance to client constructor (#168)
- [ ] 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 4843067 commit 2de7b16

5 files changed

Lines changed: 243 additions & 157 deletions

File tree

src/v1/gke_hub_client.ts

Lines changed: 65 additions & 46 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 './gke_hub_client_config.json';
41-
import {operationsProtos} from 'google-gax';
4240
const version = require('../../../package.json').version;
4341

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

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

139152
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
140153
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -364,7 +377,7 @@ export class GkeHubClient {
364377
this.innerApiCalls = {};
365378

366379
// Add a warn function to the client constructor so it can be easily tested.
367-
this.warn = gax.warn;
380+
this.warn = this._gaxModule.warn;
368381
}
369382

370383
/**
@@ -577,7 +590,7 @@ export class GkeHubClient {
577590
options.otherArgs = options.otherArgs || {};
578591
options.otherArgs.headers = options.otherArgs.headers || {};
579592
options.otherArgs.headers['x-goog-request-params'] =
580-
gax.routingHeader.fromParams({
593+
this._gaxModule.routingHeader.fromParams({
581594
name: request.name || '',
582595
});
583596
this.initialize();
@@ -661,7 +674,7 @@ export class GkeHubClient {
661674
options.otherArgs = options.otherArgs || {};
662675
options.otherArgs.headers = options.otherArgs.headers || {};
663676
options.otherArgs.headers['x-goog-request-params'] =
664-
gax.routingHeader.fromParams({
677+
this._gaxModule.routingHeader.fromParams({
665678
name: request.name || '',
666679
});
667680
this.initialize();
@@ -778,7 +791,7 @@ export class GkeHubClient {
778791
options.otherArgs = options.otherArgs || {};
779792
options.otherArgs.headers = options.otherArgs.headers || {};
780793
options.otherArgs.headers['x-goog-request-params'] =
781-
gax.routingHeader.fromParams({
794+
this._gaxModule.routingHeader.fromParams({
782795
name: request.name || '',
783796
});
784797
this.initialize();
@@ -917,7 +930,7 @@ export class GkeHubClient {
917930
options.otherArgs = options.otherArgs || {};
918931
options.otherArgs.headers = options.otherArgs.headers || {};
919932
options.otherArgs.headers['x-goog-request-params'] =
920-
gax.routingHeader.fromParams({
933+
this._gaxModule.routingHeader.fromParams({
921934
parent: request.parent || '',
922935
});
923936
this.initialize();
@@ -943,11 +956,12 @@ export class GkeHubClient {
943956
protos.google.cloud.gkehub.v1.OperationMetadata
944957
>
945958
> {
946-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
947-
{name}
948-
);
959+
const request =
960+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
961+
{name}
962+
);
949963
const [operation] = await this.operationsClient.getOperation(request);
950-
const decodeOperation = new gax.Operation(
964+
const decodeOperation = new this._gaxModule.Operation(
951965
operation,
952966
this.descriptors.longrunning.createMembership,
953967
this._gaxModule.createDefaultBackoffSettings()
@@ -1073,7 +1087,7 @@ export class GkeHubClient {
10731087
options.otherArgs = options.otherArgs || {};
10741088
options.otherArgs.headers = options.otherArgs.headers || {};
10751089
options.otherArgs.headers['x-goog-request-params'] =
1076-
gax.routingHeader.fromParams({
1090+
this._gaxModule.routingHeader.fromParams({
10771091
parent: request.parent || '',
10781092
});
10791093
this.initialize();
@@ -1099,11 +1113,12 @@ export class GkeHubClient {
10991113
protos.google.cloud.gkehub.v1.OperationMetadata
11001114
>
11011115
> {
1102-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1103-
{name}
1104-
);
1116+
const request =
1117+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1118+
{name}
1119+
);
11051120
const [operation] = await this.operationsClient.getOperation(request);
1106-
const decodeOperation = new gax.Operation(
1121+
const decodeOperation = new this._gaxModule.Operation(
11071122
operation,
11081123
this.descriptors.longrunning.createFeature,
11091124
this._gaxModule.createDefaultBackoffSettings()
@@ -1229,7 +1244,7 @@ export class GkeHubClient {
12291244
options.otherArgs = options.otherArgs || {};
12301245
options.otherArgs.headers = options.otherArgs.headers || {};
12311246
options.otherArgs.headers['x-goog-request-params'] =
1232-
gax.routingHeader.fromParams({
1247+
this._gaxModule.routingHeader.fromParams({
12331248
name: request.name || '',
12341249
});
12351250
this.initialize();
@@ -1255,11 +1270,12 @@ export class GkeHubClient {
12551270
protos.google.cloud.gkehub.v1.OperationMetadata
12561271
>
12571272
> {
1258-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1259-
{name}
1260-
);
1273+
const request =
1274+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1275+
{name}
1276+
);
12611277
const [operation] = await this.operationsClient.getOperation(request);
1262-
const decodeOperation = new gax.Operation(
1278+
const decodeOperation = new this._gaxModule.Operation(
12631279
operation,
12641280
this.descriptors.longrunning.deleteMembership,
12651281
this._gaxModule.createDefaultBackoffSettings()
@@ -1385,7 +1401,7 @@ export class GkeHubClient {
13851401
options.otherArgs = options.otherArgs || {};
13861402
options.otherArgs.headers = options.otherArgs.headers || {};
13871403
options.otherArgs.headers['x-goog-request-params'] =
1388-
gax.routingHeader.fromParams({
1404+
this._gaxModule.routingHeader.fromParams({
13891405
name: request.name || '',
13901406
});
13911407
this.initialize();
@@ -1411,11 +1427,12 @@ export class GkeHubClient {
14111427
protos.google.cloud.gkehub.v1.OperationMetadata
14121428
>
14131429
> {
1414-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1415-
{name}
1416-
);
1430+
const request =
1431+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1432+
{name}
1433+
);
14171434
const [operation] = await this.operationsClient.getOperation(request);
1418-
const decodeOperation = new gax.Operation(
1435+
const decodeOperation = new this._gaxModule.Operation(
14191436
operation,
14201437
this.descriptors.longrunning.deleteFeature,
14211438
this._gaxModule.createDefaultBackoffSettings()
@@ -1548,7 +1565,7 @@ export class GkeHubClient {
15481565
options.otherArgs = options.otherArgs || {};
15491566
options.otherArgs.headers = options.otherArgs.headers || {};
15501567
options.otherArgs.headers['x-goog-request-params'] =
1551-
gax.routingHeader.fromParams({
1568+
this._gaxModule.routingHeader.fromParams({
15521569
name: request.name || '',
15531570
});
15541571
this.initialize();
@@ -1574,11 +1591,12 @@ export class GkeHubClient {
15741591
protos.google.cloud.gkehub.v1.OperationMetadata
15751592
>
15761593
> {
1577-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1578-
{name}
1579-
);
1594+
const request =
1595+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1596+
{name}
1597+
);
15801598
const [operation] = await this.operationsClient.getOperation(request);
1581-
const decodeOperation = new gax.Operation(
1599+
const decodeOperation = new this._gaxModule.Operation(
15821600
operation,
15831601
this.descriptors.longrunning.updateMembership,
15841602
this._gaxModule.createDefaultBackoffSettings()
@@ -1711,7 +1729,7 @@ export class GkeHubClient {
17111729
options.otherArgs = options.otherArgs || {};
17121730
options.otherArgs.headers = options.otherArgs.headers || {};
17131731
options.otherArgs.headers['x-goog-request-params'] =
1714-
gax.routingHeader.fromParams({
1732+
this._gaxModule.routingHeader.fromParams({
17151733
name: request.name || '',
17161734
});
17171735
this.initialize();
@@ -1737,11 +1755,12 @@ export class GkeHubClient {
17371755
protos.google.cloud.gkehub.v1.OperationMetadata
17381756
>
17391757
> {
1740-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1741-
{name}
1742-
);
1758+
const request =
1759+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1760+
{name}
1761+
);
17431762
const [operation] = await this.operationsClient.getOperation(request);
1744-
const decodeOperation = new gax.Operation(
1763+
const decodeOperation = new this._gaxModule.Operation(
17451764
operation,
17461765
this.descriptors.longrunning.updateFeature,
17471766
this._gaxModule.createDefaultBackoffSettings()
@@ -1866,7 +1885,7 @@ export class GkeHubClient {
18661885
options.otherArgs = options.otherArgs || {};
18671886
options.otherArgs.headers = options.otherArgs.headers || {};
18681887
options.otherArgs.headers['x-goog-request-params'] =
1869-
gax.routingHeader.fromParams({
1888+
this._gaxModule.routingHeader.fromParams({
18701889
parent: request.parent || '',
18711890
});
18721891
this.initialize();
@@ -1933,7 +1952,7 @@ export class GkeHubClient {
19331952
options.otherArgs = options.otherArgs || {};
19341953
options.otherArgs.headers = options.otherArgs.headers || {};
19351954
options.otherArgs.headers['x-goog-request-params'] =
1936-
gax.routingHeader.fromParams({
1955+
this._gaxModule.routingHeader.fromParams({
19371956
parent: request.parent || '',
19381957
});
19391958
const defaultCallSettings = this._defaults['listMemberships'];
@@ -2009,7 +2028,7 @@ export class GkeHubClient {
20092028
options.otherArgs = options.otherArgs || {};
20102029
options.otherArgs.headers = options.otherArgs.headers || {};
20112030
options.otherArgs.headers['x-goog-request-params'] =
2012-
gax.routingHeader.fromParams({
2031+
this._gaxModule.routingHeader.fromParams({
20132032
parent: request.parent || '',
20142033
});
20152034
const defaultCallSettings = this._defaults['listMemberships'];
@@ -2132,7 +2151,7 @@ export class GkeHubClient {
21322151
options.otherArgs = options.otherArgs || {};
21332152
options.otherArgs.headers = options.otherArgs.headers || {};
21342153
options.otherArgs.headers['x-goog-request-params'] =
2135-
gax.routingHeader.fromParams({
2154+
this._gaxModule.routingHeader.fromParams({
21362155
parent: request.parent || '',
21372156
});
21382157
this.initialize();
@@ -2195,7 +2214,7 @@ export class GkeHubClient {
21952214
options.otherArgs = options.otherArgs || {};
21962215
options.otherArgs.headers = options.otherArgs.headers || {};
21972216
options.otherArgs.headers['x-goog-request-params'] =
2198-
gax.routingHeader.fromParams({
2217+
this._gaxModule.routingHeader.fromParams({
21992218
parent: request.parent || '',
22002219
});
22012220
const defaultCallSettings = this._defaults['listFeatures'];
@@ -2267,7 +2286,7 @@ export class GkeHubClient {
22672286
options.otherArgs = options.otherArgs || {};
22682287
options.otherArgs.headers = options.otherArgs.headers || {};
22692288
options.otherArgs.headers['x-goog-request-params'] =
2270-
gax.routingHeader.fromParams({
2289+
this._gaxModule.routingHeader.fromParams({
22712290
parent: request.parent || '',
22722291
});
22732292
const defaultCallSettings = this._defaults['listFeatures'];

0 commit comments

Comments
 (0)