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

Commit 5eb33cd

Browse files
fix: allow passing gax instance to client constructor (#310)
- [ ] 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 844c8f7 commit 5eb33cd

32 files changed

+1222
-678
lines changed

src/v3/agents_client.ts

+46-28
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,
@@ -30,7 +30,6 @@ import {
3030
LocationsClient,
3131
LocationProtos,
3232
} from 'google-gax';
33-
3433
import {Transform} from 'stream';
3534
import * as protos from '../../protos/protos';
3635
import jsonProtos = require('../../protos/protos.json');
@@ -40,7 +39,6 @@ import jsonProtos = require('../../protos/protos.json');
4039
* This file defines retry strategy and timeouts for all API methods in this library.
4140
*/
4241
import * as gapicConfig from './agents_client_config.json';
43-
import {operationsProtos} from 'google-gax';
4442
const version = require('../../../package.json').version;
4543

4644
/**
@@ -102,8 +100,18 @@ export class AgentsClient {
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}.
105-
*/
106-
constructor(opts?: ClientOptions) {
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 AgentsClient({fallback: 'rest'}, gax);
109+
* ```
110+
*/
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.constructor as typeof AgentsClient;
109117
const servicePath =
@@ -123,8 +131,13 @@ export class AgentsClient {
123131
opts['scopes'] = staticMembers.scopes;
124132
}
125133

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

129142
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
130143
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -145,7 +158,10 @@ export class AgentsClient {
145158
if (servicePath === staticMembers.servicePath) {
146159
this.auth.defaultScopes = staticMembers.scopes;
147160
}
148-
this.locationsClient = new LocationsClient(this._gaxGrpc, opts);
161+
this.locationsClient = new this._gaxModule.LocationsClient(
162+
this._gaxGrpc,
163+
opts
164+
);
149165

150166
// Determine the client header string.
151167
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -335,7 +351,7 @@ export class AgentsClient {
335351
this.innerApiCalls = {};
336352

337353
// Add a warn function to the client constructor so it can be easily tested.
338-
this.warn = gax.warn;
354+
this.warn = this._gaxModule.warn;
339355
}
340356

341357
/**
@@ -549,7 +565,7 @@ export class AgentsClient {
549565
options.otherArgs = options.otherArgs || {};
550566
options.otherArgs.headers = options.otherArgs.headers || {};
551567
options.otherArgs.headers['x-goog-request-params'] =
552-
gax.routingHeader.fromParams({
568+
this._gaxModule.routingHeader.fromParams({
553569
name: request.name || '',
554570
});
555571
this.initialize();
@@ -647,7 +663,7 @@ export class AgentsClient {
647663
options.otherArgs = options.otherArgs || {};
648664
options.otherArgs.headers = options.otherArgs.headers || {};
649665
options.otherArgs.headers['x-goog-request-params'] =
650-
gax.routingHeader.fromParams({
666+
this._gaxModule.routingHeader.fromParams({
651667
parent: request.parent || '',
652668
});
653669
this.initialize();
@@ -745,7 +761,7 @@ export class AgentsClient {
745761
options.otherArgs = options.otherArgs || {};
746762
options.otherArgs.headers = options.otherArgs.headers || {};
747763
options.otherArgs.headers['x-goog-request-params'] =
748-
gax.routingHeader.fromParams({
764+
this._gaxModule.routingHeader.fromParams({
749765
'agent.name': request.agent!.name || '',
750766
});
751767
this.initialize();
@@ -837,7 +853,7 @@ export class AgentsClient {
837853
options.otherArgs = options.otherArgs || {};
838854
options.otherArgs.headers = options.otherArgs.headers || {};
839855
options.otherArgs.headers['x-goog-request-params'] =
840-
gax.routingHeader.fromParams({
856+
this._gaxModule.routingHeader.fromParams({
841857
name: request.name || '',
842858
});
843859
this.initialize();
@@ -933,7 +949,7 @@ export class AgentsClient {
933949
options.otherArgs = options.otherArgs || {};
934950
options.otherArgs.headers = options.otherArgs.headers || {};
935951
options.otherArgs.headers['x-goog-request-params'] =
936-
gax.routingHeader.fromParams({
952+
this._gaxModule.routingHeader.fromParams({
937953
name: request.name || '',
938954
});
939955
this.initialize();
@@ -1035,7 +1051,7 @@ export class AgentsClient {
10351051
options.otherArgs = options.otherArgs || {};
10361052
options.otherArgs.headers = options.otherArgs.headers || {};
10371053
options.otherArgs.headers['x-goog-request-params'] =
1038-
gax.routingHeader.fromParams({
1054+
this._gaxModule.routingHeader.fromParams({
10391055
name: request.name || '',
10401056
});
10411057
this.initialize();
@@ -1169,7 +1185,7 @@ export class AgentsClient {
11691185
options.otherArgs = options.otherArgs || {};
11701186
options.otherArgs.headers = options.otherArgs.headers || {};
11711187
options.otherArgs.headers['x-goog-request-params'] =
1172-
gax.routingHeader.fromParams({
1188+
this._gaxModule.routingHeader.fromParams({
11731189
name: request.name || '',
11741190
});
11751191
this.initialize();
@@ -1195,11 +1211,12 @@ export class AgentsClient {
11951211
protos.google.protobuf.Struct
11961212
>
11971213
> {
1198-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1199-
{name}
1200-
);
1214+
const request =
1215+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1216+
{name}
1217+
);
12011218
const [operation] = await this.operationsClient.getOperation(request);
1202-
const decodeOperation = new gax.Operation(
1219+
const decodeOperation = new this._gaxModule.Operation(
12031220
operation,
12041221
this.descriptors.longrunning.exportAgent,
12051222
this._gaxModule.createDefaultBackoffSettings()
@@ -1337,7 +1354,7 @@ export class AgentsClient {
13371354
options.otherArgs = options.otherArgs || {};
13381355
options.otherArgs.headers = options.otherArgs.headers || {};
13391356
options.otherArgs.headers['x-goog-request-params'] =
1340-
gax.routingHeader.fromParams({
1357+
this._gaxModule.routingHeader.fromParams({
13411358
name: request.name || '',
13421359
});
13431360
this.initialize();
@@ -1360,11 +1377,12 @@ export class AgentsClient {
13601377
): Promise<
13611378
LROperation<protos.google.protobuf.Empty, protos.google.protobuf.Struct>
13621379
> {
1363-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1364-
{name}
1365-
);
1380+
const request =
1381+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1382+
{name}
1383+
);
13661384
const [operation] = await this.operationsClient.getOperation(request);
1367-
const decodeOperation = new gax.Operation(
1385+
const decodeOperation = new this._gaxModule.Operation(
13681386
operation,
13691387
this.descriptors.longrunning.restoreAgent,
13701388
this._gaxModule.createDefaultBackoffSettings()
@@ -1468,7 +1486,7 @@ export class AgentsClient {
14681486
options.otherArgs = options.otherArgs || {};
14691487
options.otherArgs.headers = options.otherArgs.headers || {};
14701488
options.otherArgs.headers['x-goog-request-params'] =
1471-
gax.routingHeader.fromParams({
1489+
this._gaxModule.routingHeader.fromParams({
14721490
parent: request.parent || '',
14731491
});
14741492
this.initialize();
@@ -1508,7 +1526,7 @@ export class AgentsClient {
15081526
options.otherArgs = options.otherArgs || {};
15091527
options.otherArgs.headers = options.otherArgs.headers || {};
15101528
options.otherArgs.headers['x-goog-request-params'] =
1511-
gax.routingHeader.fromParams({
1529+
this._gaxModule.routingHeader.fromParams({
15121530
parent: request.parent || '',
15131531
});
15141532
const defaultCallSettings = this._defaults['listAgents'];
@@ -1557,7 +1575,7 @@ export class AgentsClient {
15571575
options.otherArgs = options.otherArgs || {};
15581576
options.otherArgs.headers = options.otherArgs.headers || {};
15591577
options.otherArgs.headers['x-goog-request-params'] =
1560-
gax.routingHeader.fromParams({
1578+
this._gaxModule.routingHeader.fromParams({
15611579
parent: request.parent || '',
15621580
});
15631581
const defaultCallSettings = this._defaults['listAgents'];

src/v3/changelogs_client.ts

+29-13
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,
@@ -29,7 +29,6 @@ import {
2929
LocationsClient,
3030
LocationProtos,
3131
} from 'google-gax';
32-
3332
import {Transform} from 'stream';
3433
import * as protos from '../../protos/protos';
3534
import jsonProtos = require('../../protos/protos.json');
@@ -39,7 +38,6 @@ import jsonProtos = require('../../protos/protos.json');
3938
* This file defines retry strategy and timeouts for all API methods in this library.
4039
*/
4140
import * as gapicConfig from './changelogs_client_config.json';
42-
4341
const version = require('../../../package.json').version;
4442

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

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

128141
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
129142
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -144,7 +157,10 @@ export class ChangelogsClient {
144157
if (servicePath === staticMembers.servicePath) {
145158
this.auth.defaultScopes = staticMembers.scopes;
146159
}
147-
this.locationsClient = new LocationsClient(this._gaxGrpc, opts);
160+
this.locationsClient = new this._gaxModule.LocationsClient(
161+
this._gaxGrpc,
162+
opts
163+
);
148164

149165
// Determine the client header string.
150166
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -311,7 +327,7 @@ export class ChangelogsClient {
311327
this.innerApiCalls = {};
312328

313329
// Add a warn function to the client constructor so it can be easily tested.
314-
this.warn = gax.warn;
330+
this.warn = this._gaxModule.warn;
315331
}
316332

317333
/**
@@ -519,7 +535,7 @@ export class ChangelogsClient {
519535
options.otherArgs = options.otherArgs || {};
520536
options.otherArgs.headers = options.otherArgs.headers || {};
521537
options.otherArgs.headers['x-goog-request-params'] =
522-
gax.routingHeader.fromParams({
538+
this._gaxModule.routingHeader.fromParams({
523539
name: request.name || '',
524540
});
525541
this.initialize();
@@ -641,7 +657,7 @@ export class ChangelogsClient {
641657
options.otherArgs = options.otherArgs || {};
642658
options.otherArgs.headers = options.otherArgs.headers || {};
643659
options.otherArgs.headers['x-goog-request-params'] =
644-
gax.routingHeader.fromParams({
660+
this._gaxModule.routingHeader.fromParams({
645661
parent: request.parent || '',
646662
});
647663
this.initialize();
@@ -702,7 +718,7 @@ export class ChangelogsClient {
702718
options.otherArgs = options.otherArgs || {};
703719
options.otherArgs.headers = options.otherArgs.headers || {};
704720
options.otherArgs.headers['x-goog-request-params'] =
705-
gax.routingHeader.fromParams({
721+
this._gaxModule.routingHeader.fromParams({
706722
parent: request.parent || '',
707723
});
708724
const defaultCallSettings = this._defaults['listChangelogs'];
@@ -772,7 +788,7 @@ export class ChangelogsClient {
772788
options.otherArgs = options.otherArgs || {};
773789
options.otherArgs.headers = options.otherArgs.headers || {};
774790
options.otherArgs.headers['x-goog-request-params'] =
775-
gax.routingHeader.fromParams({
791+
this._gaxModule.routingHeader.fromParams({
776792
parent: request.parent || '',
777793
});
778794
const defaultCallSettings = this._defaults['listChangelogs'];

0 commit comments

Comments
 (0)