Skip to content

Commit de0e5c1

Browse files
gcf-owl-bot[bot]Benjamin E. Coe
andauthored
feat: accept google-gax instance as a parameter (#3306)
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: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJhdGNoLy5Pd2xCb3QueWFtbCIsImgiOiJlOTdhMWFjMjA0ZWFkNGZlNzM0MWY5MWU3MmRiN2M2YWM2MDE2MzQxIn0= Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJleW9uZGNvcnAtYXBwY29ubmVjdGlvbnMvLk93bEJvdC55YW1sIiwiaCI6ImU5N2ExYWMyMDRlYWQ0ZmU3MzQxZjkxZTcyZGI3YzZhYzYwMTYzNDEifQ== Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJleW9uZGNvcnAtYXBwY29ubmVjdG9ycy8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJleW9uZGNvcnAtYXBwZ2F0ZXdheXMvLk93bEJvdC55YW1sIiwiaCI6ImU5N2ExYWMyMDRlYWQ0ZmU3MzQxZjkxZTcyZGI3YzZhYzYwMTYzNDEifQ== Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJleW9uZGNvcnAtY2xpZW50Y29ubmVjdG9yc2VydmljZXMvLk93bEJvdC55YW1sIiwiaCI6ImU5N2ExYWMyMDRlYWQ0ZmU3MzQxZjkxZTcyZGI3YzZhYzYwMTYzNDEifQ== Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWJleW9uZGNvcnAtY2xpZW50Z2F0ZXdheXMvLk93bEJvdC55YW1sIiwiaCI6ImU5N2ExYWMyMDRlYWQ0ZmU3MzQxZjkxZTcyZGI3YzZhYzYwMTYzNDEifQ== See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent 502e3f7 commit de0e5c1

11 files changed

Lines changed: 329 additions & 224 deletions

File tree

packages/google-cloud-batch/src/v1/batch_service_client.ts

Lines changed: 40 additions & 23 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,
@@ -32,7 +32,6 @@ import {
3232
LocationsClient,
3333
LocationProtos,
3434
} from 'google-gax';
35-
3635
import {Transform} from 'stream';
3736
import * as protos from '../../protos/protos';
3837
import jsonProtos = require('../../protos/protos.json');
@@ -42,7 +41,6 @@ import jsonProtos = require('../../protos/protos.json');
4241
* This file defines retry strategy and timeouts for all API methods in this library.
4342
*/
4443
import * as gapicConfig from './batch_service_client_config.json';
45-
import {operationsProtos} from 'google-gax';
4644
const version = require('../../../package.json').version;
4745

4846
/**
@@ -107,8 +105,18 @@ export class BatchServiceClient {
107105
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
108106
* For more information, please check the
109107
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
108+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
109+
* need to avoid loading the default gRPC version and want to use the fallback
110+
* HTTP implementation. Load only fallback version and pass it to the constructor:
111+
* ```
112+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
113+
* const client = new BatchServiceClient({fallback: 'rest'}, gax);
114+
* ```
110115
*/
111-
constructor(opts?: ClientOptions) {
116+
constructor(
117+
opts?: ClientOptions,
118+
gaxInstance?: typeof gax | typeof gax.fallback
119+
) {
112120
// Ensure that options include all the required fields.
113121
const staticMembers = this.constructor as typeof BatchServiceClient;
114122
const servicePath =
@@ -128,8 +136,13 @@ export class BatchServiceClient {
128136
opts['scopes'] = staticMembers.scopes;
129137
}
130138

139+
// Load google-gax module synchronously if needed
140+
if (!gaxInstance) {
141+
gaxInstance = require('google-gax') as typeof gax;
142+
}
143+
131144
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
132-
this._gaxModule = opts.fallback ? gax.fallback : gax;
145+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
133146

134147
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
135148
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -150,9 +163,12 @@ export class BatchServiceClient {
150163
if (servicePath === staticMembers.servicePath) {
151164
this.auth.defaultScopes = staticMembers.scopes;
152165
}
153-
this.iamClient = new IamClient(this._gaxGrpc, opts);
166+
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
154167

155-
this.locationsClient = new LocationsClient(this._gaxGrpc, opts);
168+
this.locationsClient = new this._gaxModule.LocationsClient(
169+
this._gaxGrpc,
170+
opts
171+
);
156172

157173
// Determine the client header string.
158174
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -317,7 +333,7 @@ export class BatchServiceClient {
317333
this.innerApiCalls = {};
318334

319335
// Add a warn function to the client constructor so it can be easily tested.
320-
this.warn = gax.warn;
336+
this.warn = this._gaxModule.warn;
321337
}
322338

323339
/**
@@ -548,7 +564,7 @@ export class BatchServiceClient {
548564
options.otherArgs = options.otherArgs || {};
549565
options.otherArgs.headers = options.otherArgs.headers || {};
550566
options.otherArgs.headers['x-goog-request-params'] =
551-
gax.routingHeader.fromParams({
567+
this._gaxModule.routingHeader.fromParams({
552568
parent: request.parent || '',
553569
});
554570
this.initialize();
@@ -631,7 +647,7 @@ export class BatchServiceClient {
631647
options.otherArgs = options.otherArgs || {};
632648
options.otherArgs.headers = options.otherArgs.headers || {};
633649
options.otherArgs.headers['x-goog-request-params'] =
634-
gax.routingHeader.fromParams({
650+
this._gaxModule.routingHeader.fromParams({
635651
name: request.name || '',
636652
});
637653
this.initialize();
@@ -714,7 +730,7 @@ export class BatchServiceClient {
714730
options.otherArgs = options.otherArgs || {};
715731
options.otherArgs.headers = options.otherArgs.headers || {};
716732
options.otherArgs.headers['x-goog-request-params'] =
717-
gax.routingHeader.fromParams({
733+
this._gaxModule.routingHeader.fromParams({
718734
name: request.name || '',
719735
});
720736
this.initialize();
@@ -834,7 +850,7 @@ export class BatchServiceClient {
834850
options.otherArgs = options.otherArgs || {};
835851
options.otherArgs.headers = options.otherArgs.headers || {};
836852
options.otherArgs.headers['x-goog-request-params'] =
837-
gax.routingHeader.fromParams({
853+
this._gaxModule.routingHeader.fromParams({
838854
name: request.name || '',
839855
});
840856
this.initialize();
@@ -860,11 +876,12 @@ export class BatchServiceClient {
860876
protos.google.cloud.batch.v1.OperationMetadata
861877
>
862878
> {
863-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
864-
{name}
865-
);
879+
const request =
880+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
881+
{name}
882+
);
866883
const [operation] = await this.operationsClient.getOperation(request);
867-
const decodeOperation = new gax.Operation(
884+
const decodeOperation = new this._gaxModule.Operation(
868885
operation,
869886
this.descriptors.longrunning.deleteJob,
870887
this._gaxModule.createDefaultBackoffSettings()
@@ -960,7 +977,7 @@ export class BatchServiceClient {
960977
options.otherArgs = options.otherArgs || {};
961978
options.otherArgs.headers = options.otherArgs.headers || {};
962979
options.otherArgs.headers['x-goog-request-params'] =
963-
gax.routingHeader.fromParams({
980+
this._gaxModule.routingHeader.fromParams({
964981
parent: request.parent || '',
965982
});
966983
this.initialize();
@@ -1000,7 +1017,7 @@ export class BatchServiceClient {
10001017
options.otherArgs = options.otherArgs || {};
10011018
options.otherArgs.headers = options.otherArgs.headers || {};
10021019
options.otherArgs.headers['x-goog-request-params'] =
1003-
gax.routingHeader.fromParams({
1020+
this._gaxModule.routingHeader.fromParams({
10041021
parent: request.parent || '',
10051022
});
10061023
const defaultCallSettings = this._defaults['listJobs'];
@@ -1049,7 +1066,7 @@ export class BatchServiceClient {
10491066
options.otherArgs = options.otherArgs || {};
10501067
options.otherArgs.headers = options.otherArgs.headers || {};
10511068
options.otherArgs.headers['x-goog-request-params'] =
1052-
gax.routingHeader.fromParams({
1069+
this._gaxModule.routingHeader.fromParams({
10531070
parent: request.parent || '',
10541071
});
10551072
const defaultCallSettings = this._defaults['listJobs'];
@@ -1151,7 +1168,7 @@ export class BatchServiceClient {
11511168
options.otherArgs = options.otherArgs || {};
11521169
options.otherArgs.headers = options.otherArgs.headers || {};
11531170
options.otherArgs.headers['x-goog-request-params'] =
1154-
gax.routingHeader.fromParams({
1171+
this._gaxModule.routingHeader.fromParams({
11551172
parent: request.parent || '',
11561173
});
11571174
this.initialize();
@@ -1195,7 +1212,7 @@ export class BatchServiceClient {
11951212
options.otherArgs = options.otherArgs || {};
11961213
options.otherArgs.headers = options.otherArgs.headers || {};
11971214
options.otherArgs.headers['x-goog-request-params'] =
1198-
gax.routingHeader.fromParams({
1215+
this._gaxModule.routingHeader.fromParams({
11991216
parent: request.parent || '',
12001217
});
12011218
const defaultCallSettings = this._defaults['listTasks'];
@@ -1248,7 +1265,7 @@ export class BatchServiceClient {
12481265
options.otherArgs = options.otherArgs || {};
12491266
options.otherArgs.headers = options.otherArgs.headers || {};
12501267
options.otherArgs.headers['x-goog-request-params'] =
1251-
gax.routingHeader.fromParams({
1268+
this._gaxModule.routingHeader.fromParams({
12521269
parent: request.parent || '',
12531270
});
12541271
const defaultCallSettings = this._defaults['listTasks'];

packages/google-cloud-batch/src/v1alpha/batch_service_client.ts

Lines changed: 40 additions & 23 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,
@@ -32,7 +32,6 @@ import {
3232
LocationsClient,
3333
LocationProtos,
3434
} from 'google-gax';
35-
3635
import {Transform} from 'stream';
3736
import * as protos from '../../protos/protos';
3837
import jsonProtos = require('../../protos/protos.json');
@@ -42,7 +41,6 @@ import jsonProtos = require('../../protos/protos.json');
4241
* This file defines retry strategy and timeouts for all API methods in this library.
4342
*/
4443
import * as gapicConfig from './batch_service_client_config.json';
45-
import {operationsProtos} from 'google-gax';
4644
const version = require('../../../package.json').version;
4745

4846
/**
@@ -107,8 +105,18 @@ export class BatchServiceClient {
107105
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
108106
* For more information, please check the
109107
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
108+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
109+
* need to avoid loading the default gRPC version and want to use the fallback
110+
* HTTP implementation. Load only fallback version and pass it to the constructor:
111+
* ```
112+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
113+
* const client = new BatchServiceClient({fallback: 'rest'}, gax);
114+
* ```
110115
*/
111-
constructor(opts?: ClientOptions) {
116+
constructor(
117+
opts?: ClientOptions,
118+
gaxInstance?: typeof gax | typeof gax.fallback
119+
) {
112120
// Ensure that options include all the required fields.
113121
const staticMembers = this.constructor as typeof BatchServiceClient;
114122
const servicePath =
@@ -128,8 +136,13 @@ export class BatchServiceClient {
128136
opts['scopes'] = staticMembers.scopes;
129137
}
130138

139+
// Load google-gax module synchronously if needed
140+
if (!gaxInstance) {
141+
gaxInstance = require('google-gax') as typeof gax;
142+
}
143+
131144
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
132-
this._gaxModule = opts.fallback ? gax.fallback : gax;
145+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
133146

134147
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
135148
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -150,9 +163,12 @@ export class BatchServiceClient {
150163
if (servicePath === staticMembers.servicePath) {
151164
this.auth.defaultScopes = staticMembers.scopes;
152165
}
153-
this.iamClient = new IamClient(this._gaxGrpc, opts);
166+
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);
154167

155-
this.locationsClient = new LocationsClient(this._gaxGrpc, opts);
168+
this.locationsClient = new this._gaxModule.LocationsClient(
169+
this._gaxGrpc,
170+
opts
171+
);
156172

157173
// Determine the client header string.
158174
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
@@ -321,7 +337,7 @@ export class BatchServiceClient {
321337
this.innerApiCalls = {};
322338

323339
// Add a warn function to the client constructor so it can be easily tested.
324-
this.warn = gax.warn;
340+
this.warn = this._gaxModule.warn;
325341
}
326342

327343
/**
@@ -554,7 +570,7 @@ export class BatchServiceClient {
554570
options.otherArgs = options.otherArgs || {};
555571
options.otherArgs.headers = options.otherArgs.headers || {};
556572
options.otherArgs.headers['x-goog-request-params'] =
557-
gax.routingHeader.fromParams({
573+
this._gaxModule.routingHeader.fromParams({
558574
parent: request.parent || '',
559575
});
560576
this.initialize();
@@ -637,7 +653,7 @@ export class BatchServiceClient {
637653
options.otherArgs = options.otherArgs || {};
638654
options.otherArgs.headers = options.otherArgs.headers || {};
639655
options.otherArgs.headers['x-goog-request-params'] =
640-
gax.routingHeader.fromParams({
656+
this._gaxModule.routingHeader.fromParams({
641657
name: request.name || '',
642658
});
643659
this.initialize();
@@ -720,7 +736,7 @@ export class BatchServiceClient {
720736
options.otherArgs = options.otherArgs || {};
721737
options.otherArgs.headers = options.otherArgs.headers || {};
722738
options.otherArgs.headers['x-goog-request-params'] =
723-
gax.routingHeader.fromParams({
739+
this._gaxModule.routingHeader.fromParams({
724740
name: request.name || '',
725741
});
726742
this.initialize();
@@ -840,7 +856,7 @@ export class BatchServiceClient {
840856
options.otherArgs = options.otherArgs || {};
841857
options.otherArgs.headers = options.otherArgs.headers || {};
842858
options.otherArgs.headers['x-goog-request-params'] =
843-
gax.routingHeader.fromParams({
859+
this._gaxModule.routingHeader.fromParams({
844860
name: request.name || '',
845861
});
846862
this.initialize();
@@ -866,11 +882,12 @@ export class BatchServiceClient {
866882
protos.google.cloud.batch.v1alpha.OperationMetadata
867883
>
868884
> {
869-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
870-
{name}
871-
);
885+
const request =
886+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
887+
{name}
888+
);
872889
const [operation] = await this.operationsClient.getOperation(request);
873-
const decodeOperation = new gax.Operation(
890+
const decodeOperation = new this._gaxModule.Operation(
874891
operation,
875892
this.descriptors.longrunning.deleteJob,
876893
this._gaxModule.createDefaultBackoffSettings()
@@ -968,7 +985,7 @@ export class BatchServiceClient {
968985
options.otherArgs = options.otherArgs || {};
969986
options.otherArgs.headers = options.otherArgs.headers || {};
970987
options.otherArgs.headers['x-goog-request-params'] =
971-
gax.routingHeader.fromParams({
988+
this._gaxModule.routingHeader.fromParams({
972989
parent: request.parent || '',
973990
});
974991
this.initialize();
@@ -1008,7 +1025,7 @@ export class BatchServiceClient {
10081025
options.otherArgs = options.otherArgs || {};
10091026
options.otherArgs.headers = options.otherArgs.headers || {};
10101027
options.otherArgs.headers['x-goog-request-params'] =
1011-
gax.routingHeader.fromParams({
1028+
this._gaxModule.routingHeader.fromParams({
10121029
parent: request.parent || '',
10131030
});
10141031
const defaultCallSettings = this._defaults['listJobs'];
@@ -1057,7 +1074,7 @@ export class BatchServiceClient {
10571074
options.otherArgs = options.otherArgs || {};
10581075
options.otherArgs.headers = options.otherArgs.headers || {};
10591076
options.otherArgs.headers['x-goog-request-params'] =
1060-
gax.routingHeader.fromParams({
1077+
this._gaxModule.routingHeader.fromParams({
10611078
parent: request.parent || '',
10621079
});
10631080
const defaultCallSettings = this._defaults['listJobs'];
@@ -1161,7 +1178,7 @@ export class BatchServiceClient {
11611178
options.otherArgs = options.otherArgs || {};
11621179
options.otherArgs.headers = options.otherArgs.headers || {};
11631180
options.otherArgs.headers['x-goog-request-params'] =
1164-
gax.routingHeader.fromParams({
1181+
this._gaxModule.routingHeader.fromParams({
11651182
parent: request.parent || '',
11661183
});
11671184
this.initialize();
@@ -1205,7 +1222,7 @@ export class BatchServiceClient {
12051222
options.otherArgs = options.otherArgs || {};
12061223
options.otherArgs.headers = options.otherArgs.headers || {};
12071224
options.otherArgs.headers['x-goog-request-params'] =
1208-
gax.routingHeader.fromParams({
1225+
this._gaxModule.routingHeader.fromParams({
12091226
parent: request.parent || '',
12101227
});
12111228
const defaultCallSettings = this._defaults['listTasks'];
@@ -1258,7 +1275,7 @@ export class BatchServiceClient {
12581275
options.otherArgs = options.otherArgs || {};
12591276
options.otherArgs.headers = options.otherArgs.headers || {};
12601277
options.otherArgs.headers['x-goog-request-params'] =
1261-
gax.routingHeader.fromParams({
1278+
this._gaxModule.routingHeader.fromParams({
12621279
parent: request.parent || '',
12631280
});
12641281
const defaultCallSettings = this._defaults['listTasks'];

0 commit comments

Comments
 (0)