Skip to content

Commit 8eabe38

Browse files
fix: allow passing gax instance to client constructor (#1617)
- [ ] 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 31c7fa2 commit 8eabe38

File tree

3 files changed

+111
-72
lines changed

3 files changed

+111
-72
lines changed

src/v1/publisher_client.ts

Lines changed: 37 additions & 24 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
IamClient,
2929
IamProtos,
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 './publisher_client_config.json';
41-
4240
const version = require('../../../package.json').version;
4341

4442
/**
@@ -100,8 +98,18 @@ export class PublisherClient {
10098
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
10199
* For more information, please check the
102100
* {@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 PublisherClient({fallback: 'rest'}, gax);
107+
* ```
103108
*/
104-
constructor(opts?: ClientOptions) {
109+
constructor(
110+
opts?: ClientOptions,
111+
gaxInstance?: typeof gax | typeof gax.fallback
112+
) {
105113
// Ensure that options include all the required fields.
106114
const staticMembers = this.constructor as typeof PublisherClient;
107115
const servicePath =
@@ -121,8 +129,13 @@ export class PublisherClient {
121129
opts['scopes'] = staticMembers.scopes;
122130
}
123131

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

127140
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
128141
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -143,7 +156,7 @@ export class PublisherClient {
143156
if (servicePath === staticMembers.servicePath) {
144157
this.auth.defaultScopes = staticMembers.scopes;
145158
}
146-
this.iamClient = new IamClient(this._gaxGrpc, opts);
159+
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
147160

148161
// Determine the client header string.
149162
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -214,7 +227,7 @@ export class PublisherClient {
214227
'messages',
215228
['topic'],
216229
'message_ids',
217-
gax.createByteLengthFunction(
230+
this._gaxModule.GrpcClient.createByteLengthFunction(
218231
// eslint-disable-next-line @typescript-eslint/no-explicit-any
219232
protoFilesRoot.lookupType('google.pubsub.v1.PubsubMessage') as any
220233
)
@@ -235,7 +248,7 @@ export class PublisherClient {
235248
this.innerApiCalls = {};
236249

237250
// Add a warn function to the client constructor so it can be easily tested.
238-
this.warn = gax.warn;
251+
this.warn = this._gaxModule.warn;
239252
}
240253

241254
/**
@@ -476,7 +489,7 @@ export class PublisherClient {
476489
options.otherArgs = options.otherArgs || {};
477490
options.otherArgs.headers = options.otherArgs.headers || {};
478491
options.otherArgs.headers['x-goog-request-params'] =
479-
gax.routingHeader.fromParams({
492+
this._gaxModule.routingHeader.fromParams({
480493
name: request.name || '',
481494
});
482495
this.initialize();
@@ -564,7 +577,7 @@ export class PublisherClient {
564577
options.otherArgs = options.otherArgs || {};
565578
options.otherArgs.headers = options.otherArgs.headers || {};
566579
options.otherArgs.headers['x-goog-request-params'] =
567-
gax.routingHeader.fromParams({
580+
this._gaxModule.routingHeader.fromParams({
568581
'topic.name': request.topic!.name || '',
569582
});
570583
this.initialize();
@@ -649,7 +662,7 @@ export class PublisherClient {
649662
options.otherArgs = options.otherArgs || {};
650663
options.otherArgs.headers = options.otherArgs.headers || {};
651664
options.otherArgs.headers['x-goog-request-params'] =
652-
gax.routingHeader.fromParams({
665+
this._gaxModule.routingHeader.fromParams({
653666
topic: request.topic || '',
654667
});
655668
this.initialize();
@@ -731,7 +744,7 @@ export class PublisherClient {
731744
options.otherArgs = options.otherArgs || {};
732745
options.otherArgs.headers = options.otherArgs.headers || {};
733746
options.otherArgs.headers['x-goog-request-params'] =
734-
gax.routingHeader.fromParams({
747+
this._gaxModule.routingHeader.fromParams({
735748
topic: request.topic || '',
736749
});
737750
this.initialize();
@@ -817,7 +830,7 @@ export class PublisherClient {
817830
options.otherArgs = options.otherArgs || {};
818831
options.otherArgs.headers = options.otherArgs.headers || {};
819832
options.otherArgs.headers['x-goog-request-params'] =
820-
gax.routingHeader.fromParams({
833+
this._gaxModule.routingHeader.fromParams({
821834
topic: request.topic || '',
822835
});
823836
this.initialize();
@@ -902,7 +915,7 @@ export class PublisherClient {
902915
options.otherArgs = options.otherArgs || {};
903916
options.otherArgs.headers = options.otherArgs.headers || {};
904917
options.otherArgs.headers['x-goog-request-params'] =
905-
gax.routingHeader.fromParams({
918+
this._gaxModule.routingHeader.fromParams({
906919
subscription: request.subscription || '',
907920
});
908921
this.initialize();
@@ -996,7 +1009,7 @@ export class PublisherClient {
9961009
options.otherArgs = options.otherArgs || {};
9971010
options.otherArgs.headers = options.otherArgs.headers || {};
9981011
options.otherArgs.headers['x-goog-request-params'] =
999-
gax.routingHeader.fromParams({
1012+
this._gaxModule.routingHeader.fromParams({
10001013
project: request.project || '',
10011014
});
10021015
this.initialize();
@@ -1037,7 +1050,7 @@ export class PublisherClient {
10371050
options.otherArgs = options.otherArgs || {};
10381051
options.otherArgs.headers = options.otherArgs.headers || {};
10391052
options.otherArgs.headers['x-goog-request-params'] =
1040-
gax.routingHeader.fromParams({
1053+
this._gaxModule.routingHeader.fromParams({
10411054
project: request.project || '',
10421055
});
10431056
const defaultCallSettings = this._defaults['listTopics'];
@@ -1085,7 +1098,7 @@ export class PublisherClient {
10851098
options.otherArgs = options.otherArgs || {};
10861099
options.otherArgs.headers = options.otherArgs.headers || {};
10871100
options.otherArgs.headers['x-goog-request-params'] =
1088-
gax.routingHeader.fromParams({
1101+
this._gaxModule.routingHeader.fromParams({
10891102
project: request.project || '',
10901103
});
10911104
const defaultCallSettings = this._defaults['listTopics'];
@@ -1192,7 +1205,7 @@ export class PublisherClient {
11921205
options.otherArgs = options.otherArgs || {};
11931206
options.otherArgs.headers = options.otherArgs.headers || {};
11941207
options.otherArgs.headers['x-goog-request-params'] =
1195-
gax.routingHeader.fromParams({
1208+
this._gaxModule.routingHeader.fromParams({
11961209
topic: request.topic || '',
11971210
});
11981211
this.initialize();
@@ -1237,7 +1250,7 @@ export class PublisherClient {
12371250
options.otherArgs = options.otherArgs || {};
12381251
options.otherArgs.headers = options.otherArgs.headers || {};
12391252
options.otherArgs.headers['x-goog-request-params'] =
1240-
gax.routingHeader.fromParams({
1253+
this._gaxModule.routingHeader.fromParams({
12411254
topic: request.topic || '',
12421255
});
12431256
const defaultCallSettings = this._defaults['listTopicSubscriptions'];
@@ -1285,7 +1298,7 @@ export class PublisherClient {
12851298
options.otherArgs = options.otherArgs || {};
12861299
options.otherArgs.headers = options.otherArgs.headers || {};
12871300
options.otherArgs.headers['x-goog-request-params'] =
1288-
gax.routingHeader.fromParams({
1301+
this._gaxModule.routingHeader.fromParams({
12891302
topic: request.topic || '',
12901303
});
12911304
const defaultCallSettings = this._defaults['listTopicSubscriptions'];
@@ -1390,7 +1403,7 @@ export class PublisherClient {
13901403
options.otherArgs = options.otherArgs || {};
13911404
options.otherArgs.headers = options.otherArgs.headers || {};
13921405
options.otherArgs.headers['x-goog-request-params'] =
1393-
gax.routingHeader.fromParams({
1406+
this._gaxModule.routingHeader.fromParams({
13941407
topic: request.topic || '',
13951408
});
13961409
this.initialize();
@@ -1431,7 +1444,7 @@ export class PublisherClient {
14311444
options.otherArgs = options.otherArgs || {};
14321445
options.otherArgs.headers = options.otherArgs.headers || {};
14331446
options.otherArgs.headers['x-goog-request-params'] =
1434-
gax.routingHeader.fromParams({
1447+
this._gaxModule.routingHeader.fromParams({
14351448
topic: request.topic || '',
14361449
});
14371450
const defaultCallSettings = this._defaults['listTopicSnapshots'];
@@ -1479,7 +1492,7 @@ export class PublisherClient {
14791492
options.otherArgs = options.otherArgs || {};
14801493
options.otherArgs.headers = options.otherArgs.headers || {};
14811494
options.otherArgs.headers['x-goog-request-params'] =
1482-
gax.routingHeader.fromParams({
1495+
this._gaxModule.routingHeader.fromParams({
14831496
topic: request.topic || '',
14841497
});
14851498
const defaultCallSettings = this._defaults['listTopicSnapshots'];

src/v1/schema_service_client.ts

Lines changed: 29 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
IamClient,
2929
IamProtos,
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 './schema_service_client_config.json';
41-
4240
const version = require('../../../package.json').version;
4341

4442
/**
@@ -99,8 +97,18 @@ export class SchemaServiceClient {
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 SchemaServiceClient({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 SchemaServiceClient;
106114
const servicePath =
@@ -120,8 +128,13 @@ export class SchemaServiceClient {
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);
@@ -142,7 +155,7 @@ export class SchemaServiceClient {
142155
if (servicePath === staticMembers.servicePath) {
143156
this.auth.defaultScopes = staticMembers.scopes;
144157
}
145-
this.iamClient = new IamClient(this._gaxGrpc, opts);
158+
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
146159

147160
// Determine the client header string.
148161
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -208,7 +221,7 @@ export class SchemaServiceClient {
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
/**
@@ -424,7 +437,7 @@ export class SchemaServiceClient {
424437
options.otherArgs = options.otherArgs || {};
425438
options.otherArgs.headers = options.otherArgs.headers || {};
426439
options.otherArgs.headers['x-goog-request-params'] =
427-
gax.routingHeader.fromParams({
440+
this._gaxModule.routingHeader.fromParams({
428441
parent: request.parent || '',
429442
});
430443
this.initialize();
@@ -510,7 +523,7 @@ export class SchemaServiceClient {
510523
options.otherArgs = options.otherArgs || {};
511524
options.otherArgs.headers = options.otherArgs.headers || {};
512525
options.otherArgs.headers['x-goog-request-params'] =
513-
gax.routingHeader.fromParams({
526+
this._gaxModule.routingHeader.fromParams({
514527
name: request.name || '',
515528
});
516529
this.initialize();
@@ -592,7 +605,7 @@ export class SchemaServiceClient {
592605
options.otherArgs = options.otherArgs || {};
593606
options.otherArgs.headers = options.otherArgs.headers || {};
594607
options.otherArgs.headers['x-goog-request-params'] =
595-
gax.routingHeader.fromParams({
608+
this._gaxModule.routingHeader.fromParams({
596609
name: request.name || '',
597610
});
598611
this.initialize();
@@ -676,7 +689,7 @@ export class SchemaServiceClient {
676689
options.otherArgs = options.otherArgs || {};
677690
options.otherArgs.headers = options.otherArgs.headers || {};
678691
options.otherArgs.headers['x-goog-request-params'] =
679-
gax.routingHeader.fromParams({
692+
this._gaxModule.routingHeader.fromParams({
680693
parent: request.parent || '',
681694
});
682695
this.initialize();
@@ -768,7 +781,7 @@ export class SchemaServiceClient {
768781
options.otherArgs = options.otherArgs || {};
769782
options.otherArgs.headers = options.otherArgs.headers || {};
770783
options.otherArgs.headers['x-goog-request-params'] =
771-
gax.routingHeader.fromParams({
784+
this._gaxModule.routingHeader.fromParams({
772785
parent: request.parent || '',
773786
});
774787
this.initialize();
@@ -866,7 +879,7 @@ export class SchemaServiceClient {
866879
options.otherArgs = options.otherArgs || {};
867880
options.otherArgs.headers = options.otherArgs.headers || {};
868881
options.otherArgs.headers['x-goog-request-params'] =
869-
gax.routingHeader.fromParams({
882+
this._gaxModule.routingHeader.fromParams({
870883
parent: request.parent || '',
871884
});
872885
this.initialize();
@@ -911,7 +924,7 @@ export class SchemaServiceClient {
911924
options.otherArgs = options.otherArgs || {};
912925
options.otherArgs.headers = options.otherArgs.headers || {};
913926
options.otherArgs.headers['x-goog-request-params'] =
914-
gax.routingHeader.fromParams({
927+
this._gaxModule.routingHeader.fromParams({
915928
parent: request.parent || '',
916929
});
917930
const defaultCallSettings = this._defaults['listSchemas'];
@@ -963,7 +976,7 @@ export class SchemaServiceClient {
963976
options.otherArgs = options.otherArgs || {};
964977
options.otherArgs.headers = options.otherArgs.headers || {};
965978
options.otherArgs.headers['x-goog-request-params'] =
966-
gax.routingHeader.fromParams({
979+
this._gaxModule.routingHeader.fromParams({
967980
parent: request.parent || '',
968981
});
969982
const defaultCallSettings = this._defaults['listSchemas'];

0 commit comments

Comments
 (0)