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

Commit 91e2ac5

Browse files
fix: allow passing gax instance to client constructor (#24)
- [ ] 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 acda49f commit 91e2ac5

2 files changed

Lines changed: 75 additions & 45 deletions

File tree

src/v2/revisions_client.ts

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

4442
/**
@@ -99,8 +97,18 @@ export class RevisionsClient {
9997
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
10098
* For more information, please check the
10199
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
100+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
101+
* need to avoid loading the default gRPC version and want to use the fallback
102+
* HTTP implementation. Load only fallback version and pass it to the constructor:
103+
* ```
104+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
105+
* const client = new RevisionsClient({fallback: 'rest'}, gax);
106+
* ```
102107
*/
103-
constructor(opts?: ClientOptions) {
108+
constructor(
109+
opts?: ClientOptions,
110+
gaxInstance?: typeof gax | typeof gax.fallback
111+
) {
104112
// Ensure that options include all the required fields.
105113
const staticMembers = this.constructor as typeof RevisionsClient;
106114
const servicePath =
@@ -120,8 +128,13 @@ export class RevisionsClient {
120128
opts['scopes'] = staticMembers.scopes;
121129
}
122130

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

126139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
127140
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -250,7 +263,7 @@ export class RevisionsClient {
250263
this.innerApiCalls = {};
251264

252265
// Add a warn function to the client constructor so it can be easily tested.
253-
this.warn = gax.warn;
266+
this.warn = this._gaxModule.warn;
254267
}
255268

256269
/**
@@ -467,7 +480,7 @@ export class RevisionsClient {
467480
}
468481
}
469482
options.otherArgs.headers['x-goog-request-params'] =
470-
gax.routingHeader.fromParams(routingParameter);
483+
this._gaxModule.routingHeader.fromParams(routingParameter);
471484
this.initialize();
472485
return this.innerApiCalls.getRevision(request, options, callback);
473486
}
@@ -590,7 +603,7 @@ export class RevisionsClient {
590603
}
591604
}
592605
options.otherArgs.headers['x-goog-request-params'] =
593-
gax.routingHeader.fromParams(routingParameter);
606+
this._gaxModule.routingHeader.fromParams(routingParameter);
594607
this.initialize();
595608
return this.innerApiCalls.deleteRevision(request, options, callback);
596609
}
@@ -614,11 +627,12 @@ export class RevisionsClient {
614627
protos.google.cloud.run.v2.Revision
615628
>
616629
> {
617-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
618-
{name}
619-
);
630+
const request =
631+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
632+
{name}
633+
);
620634
const [operation] = await this.operationsClient.getOperation(request);
621-
const decodeOperation = new gax.Operation(
635+
const decodeOperation = new this._gaxModule.Operation(
622636
operation,
623637
this.descriptors.longrunning.deleteRevision,
624638
this._gaxModule.createDefaultBackoffSettings()
@@ -731,7 +745,7 @@ export class RevisionsClient {
731745
}
732746
}
733747
options.otherArgs.headers['x-goog-request-params'] =
734-
gax.routingHeader.fromParams(routingParameter);
748+
this._gaxModule.routingHeader.fromParams(routingParameter);
735749
this.initialize();
736750
return this.innerApiCalls.listRevisions(request, options, callback);
737751
}
@@ -786,7 +800,7 @@ export class RevisionsClient {
786800
}
787801
}
788802
options.otherArgs.headers['x-goog-request-params'] =
789-
gax.routingHeader.fromParams(routingParameter);
803+
this._gaxModule.routingHeader.fromParams(routingParameter);
790804
const defaultCallSettings = this._defaults['listRevisions'];
791805
const callSettings = defaultCallSettings.merge(options);
792806
this.initialize();
@@ -850,7 +864,7 @@ export class RevisionsClient {
850864
}
851865
}
852866
options.otherArgs.headers['x-goog-request-params'] =
853-
gax.routingHeader.fromParams(routingParameter);
867+
this._gaxModule.routingHeader.fromParams(routingParameter);
854868
const defaultCallSettings = this._defaults['listRevisions'];
855869
const callSettings = defaultCallSettings.merge(options);
856870
this.initialize();

src/v2/services_client.ts

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

4442
/**
@@ -99,8 +97,18 @@ export class ServicesClient {
9997
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
10098
* For more information, please check the
10199
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
100+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
101+
* need to avoid loading the default gRPC version and want to use the fallback
102+
* HTTP implementation. Load only fallback version and pass it to the constructor:
103+
* ```
104+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
105+
* const client = new ServicesClient({fallback: 'rest'}, gax);
106+
* ```
102107
*/
103-
constructor(opts?: ClientOptions) {
108+
constructor(
109+
opts?: ClientOptions,
110+
gaxInstance?: typeof gax | typeof gax.fallback
111+
) {
104112
// Ensure that options include all the required fields.
105113
const staticMembers = this.constructor as typeof ServicesClient;
106114
const servicePath =
@@ -120,8 +128,13 @@ export class ServicesClient {
120128
opts['scopes'] = staticMembers.scopes;
121129
}
122130

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

126139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
127140
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -269,7 +282,7 @@ export class ServicesClient {
269282
this.innerApiCalls = {};
270283

271284
// Add a warn function to the client constructor so it can be easily tested.
272-
this.warn = gax.warn;
285+
this.warn = this._gaxModule.warn;
273286
}
274287

275288
/**
@@ -490,7 +503,7 @@ export class ServicesClient {
490503
}
491504
}
492505
options.otherArgs.headers['x-goog-request-params'] =
493-
gax.routingHeader.fromParams(routingParameter);
506+
this._gaxModule.routingHeader.fromParams(routingParameter);
494507
this.initialize();
495508
return this.innerApiCalls.getService(request, options, callback);
496509
}
@@ -576,7 +589,7 @@ export class ServicesClient {
576589
options.otherArgs = options.otherArgs || {};
577590
options.otherArgs.headers = options.otherArgs.headers || {};
578591
options.otherArgs.headers['x-goog-request-params'] =
579-
gax.routingHeader.fromParams({
592+
this._gaxModule.routingHeader.fromParams({
580593
resource: request.resource || '',
581594
});
582595
this.initialize();
@@ -672,7 +685,7 @@ export class ServicesClient {
672685
options.otherArgs = options.otherArgs || {};
673686
options.otherArgs.headers = options.otherArgs.headers || {};
674687
options.otherArgs.headers['x-goog-request-params'] =
675-
gax.routingHeader.fromParams({
688+
this._gaxModule.routingHeader.fromParams({
676689
resource: request.resource || '',
677690
});
678691
this.initialize();
@@ -763,7 +776,7 @@ export class ServicesClient {
763776
options.otherArgs = options.otherArgs || {};
764777
options.otherArgs.headers = options.otherArgs.headers || {};
765778
options.otherArgs.headers['x-goog-request-params'] =
766-
gax.routingHeader.fromParams({
779+
this._gaxModule.routingHeader.fromParams({
767780
resource: request.resource || '',
768781
});
769782
this.initialize();
@@ -889,7 +902,7 @@ export class ServicesClient {
889902
}
890903
}
891904
options.otherArgs.headers['x-goog-request-params'] =
892-
gax.routingHeader.fromParams(routingParameter);
905+
this._gaxModule.routingHeader.fromParams(routingParameter);
893906
this.initialize();
894907
return this.innerApiCalls.createService(request, options, callback);
895908
}
@@ -913,11 +926,12 @@ export class ServicesClient {
913926
protos.google.cloud.run.v2.Service
914927
>
915928
> {
916-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
917-
{name}
918-
);
929+
const request =
930+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
931+
{name}
932+
);
919933
const [operation] = await this.operationsClient.getOperation(request);
920-
const decodeOperation = new gax.Operation(
934+
const decodeOperation = new this._gaxModule.Operation(
921935
operation,
922936
this.descriptors.longrunning.createService,
923937
this._gaxModule.createDefaultBackoffSettings()
@@ -1044,7 +1058,7 @@ export class ServicesClient {
10441058
}
10451059
}
10461060
options.otherArgs.headers['x-goog-request-params'] =
1047-
gax.routingHeader.fromParams(routingParameter);
1061+
this._gaxModule.routingHeader.fromParams(routingParameter);
10481062
this.initialize();
10491063
return this.innerApiCalls.updateService(request, options, callback);
10501064
}
@@ -1068,11 +1082,12 @@ export class ServicesClient {
10681082
protos.google.cloud.run.v2.Service
10691083
>
10701084
> {
1071-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1072-
{name}
1073-
);
1085+
const request =
1086+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1087+
{name}
1088+
);
10741089
const [operation] = await this.operationsClient.getOperation(request);
1075-
const decodeOperation = new gax.Operation(
1090+
const decodeOperation = new this._gaxModule.Operation(
10761091
operation,
10771092
this.descriptors.longrunning.updateService,
10781093
this._gaxModule.createDefaultBackoffSettings()
@@ -1201,7 +1216,7 @@ export class ServicesClient {
12011216
}
12021217
}
12031218
options.otherArgs.headers['x-goog-request-params'] =
1204-
gax.routingHeader.fromParams(routingParameter);
1219+
this._gaxModule.routingHeader.fromParams(routingParameter);
12051220
this.initialize();
12061221
return this.innerApiCalls.deleteService(request, options, callback);
12071222
}
@@ -1225,11 +1240,12 @@ export class ServicesClient {
12251240
protos.google.cloud.run.v2.Service
12261241
>
12271242
> {
1228-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1229-
{name}
1230-
);
1243+
const request =
1244+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1245+
{name}
1246+
);
12311247
const [operation] = await this.operationsClient.getOperation(request);
1232-
const decodeOperation = new gax.Operation(
1248+
const decodeOperation = new this._gaxModule.Operation(
12331249
operation,
12341250
this.descriptors.longrunning.deleteService,
12351251
this._gaxModule.createDefaultBackoffSettings()
@@ -1341,7 +1357,7 @@ export class ServicesClient {
13411357
}
13421358
}
13431359
options.otherArgs.headers['x-goog-request-params'] =
1344-
gax.routingHeader.fromParams(routingParameter);
1360+
this._gaxModule.routingHeader.fromParams(routingParameter);
13451361
this.initialize();
13461362
return this.innerApiCalls.listServices(request, options, callback);
13471363
}
@@ -1395,7 +1411,7 @@ export class ServicesClient {
13951411
}
13961412
}
13971413
options.otherArgs.headers['x-goog-request-params'] =
1398-
gax.routingHeader.fromParams(routingParameter);
1414+
this._gaxModule.routingHeader.fromParams(routingParameter);
13991415
const defaultCallSettings = this._defaults['listServices'];
14001416
const callSettings = defaultCallSettings.merge(options);
14011417
this.initialize();
@@ -1458,7 +1474,7 @@ export class ServicesClient {
14581474
}
14591475
}
14601476
options.otherArgs.headers['x-goog-request-params'] =
1461-
gax.routingHeader.fromParams(routingParameter);
1477+
this._gaxModule.routingHeader.fromParams(routingParameter);
14621478
const defaultCallSettings = this._defaults['listServices'];
14631479
const callSettings = defaultCallSettings.merge(options);
14641480
this.initialize();

0 commit comments

Comments
 (0)