Skip to content

Commit eda55d3

Browse files
fix: allow passing gax instance to client constructor (#110)
- [ ] 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 5b95a56 commit eda55d3

File tree

2 files changed

+150
-109
lines changed

2 files changed

+150
-109
lines changed

packages/google-api-serviceusage/src/v1/service_usage_client.ts

+43-27
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 './service_usage_client_config.json';
41-
import {operationsProtos} from 'google-gax';
4240
const version = require('../../../package.json').version;
4341

4442
/**
@@ -102,8 +100,18 @@ export class ServiceUsageClient {
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}.
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 ServiceUsageClient({fallback: 'rest'}, gax);
109+
* ```
105110
*/
106-
constructor(opts?: ClientOptions) {
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 ServiceUsageClient;
109117
const servicePath =
@@ -123,8 +131,13 @@ export class ServiceUsageClient {
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);
@@ -250,7 +263,7 @@ export class ServiceUsageClient {
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
/**
@@ -465,7 +478,7 @@ export class ServiceUsageClient {
465478
options.otherArgs = options.otherArgs || {};
466479
options.otherArgs.headers = options.otherArgs.headers || {};
467480
options.otherArgs.headers['x-goog-request-params'] =
468-
gax.routingHeader.fromParams({
481+
this._gaxModule.routingHeader.fromParams({
469482
name: request.name || '',
470483
});
471484
this.initialize();
@@ -568,7 +581,7 @@ export class ServiceUsageClient {
568581
options.otherArgs = options.otherArgs || {};
569582
options.otherArgs.headers = options.otherArgs.headers || {};
570583
options.otherArgs.headers['x-goog-request-params'] =
571-
gax.routingHeader.fromParams({
584+
this._gaxModule.routingHeader.fromParams({
572585
parent: request.parent || '',
573586
});
574587
this.initialize();
@@ -682,7 +695,7 @@ export class ServiceUsageClient {
682695
options.otherArgs = options.otherArgs || {};
683696
options.otherArgs.headers = options.otherArgs.headers || {};
684697
options.otherArgs.headers['x-goog-request-params'] =
685-
gax.routingHeader.fromParams({
698+
this._gaxModule.routingHeader.fromParams({
686699
name: request.name || '',
687700
});
688701
this.initialize();
@@ -708,11 +721,12 @@ export class ServiceUsageClient {
708721
protos.google.api.serviceusage.v1.OperationMetadata
709722
>
710723
> {
711-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
712-
{name}
713-
);
724+
const request =
725+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
726+
{name}
727+
);
714728
const [operation] = await this.operationsClient.getOperation(request);
715-
const decodeOperation = new gax.Operation(
729+
const decodeOperation = new this._gaxModule.Operation(
716730
operation,
717731
this.descriptors.longrunning.enableService,
718732
this._gaxModule.createDefaultBackoffSettings()
@@ -839,7 +853,7 @@ export class ServiceUsageClient {
839853
options.otherArgs = options.otherArgs || {};
840854
options.otherArgs.headers = options.otherArgs.headers || {};
841855
options.otherArgs.headers['x-goog-request-params'] =
842-
gax.routingHeader.fromParams({
856+
this._gaxModule.routingHeader.fromParams({
843857
name: request.name || '',
844858
});
845859
this.initialize();
@@ -865,11 +879,12 @@ export class ServiceUsageClient {
865879
protos.google.api.serviceusage.v1.OperationMetadata
866880
>
867881
> {
868-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
869-
{name}
870-
);
882+
const request =
883+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
884+
{name}
885+
);
871886
const [operation] = await this.operationsClient.getOperation(request);
872-
const decodeOperation = new gax.Operation(
887+
const decodeOperation = new this._gaxModule.Operation(
873888
operation,
874889
this.descriptors.longrunning.disableService,
875890
this._gaxModule.createDefaultBackoffSettings()
@@ -995,7 +1010,7 @@ export class ServiceUsageClient {
9951010
options.otherArgs = options.otherArgs || {};
9961011
options.otherArgs.headers = options.otherArgs.headers || {};
9971012
options.otherArgs.headers['x-goog-request-params'] =
998-
gax.routingHeader.fromParams({
1013+
this._gaxModule.routingHeader.fromParams({
9991014
parent: request.parent || '',
10001015
});
10011016
this.initialize();
@@ -1021,11 +1036,12 @@ export class ServiceUsageClient {
10211036
protos.google.api.serviceusage.v1.OperationMetadata
10221037
>
10231038
> {
1024-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1025-
{name}
1026-
);
1039+
const request =
1040+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1041+
{name}
1042+
);
10271043
const [operation] = await this.operationsClient.getOperation(request);
1028-
const decodeOperation = new gax.Operation(
1044+
const decodeOperation = new this._gaxModule.Operation(
10291045
operation,
10301046
this.descriptors.longrunning.batchEnableServices,
10311047
this._gaxModule.createDefaultBackoffSettings()
@@ -1148,7 +1164,7 @@ export class ServiceUsageClient {
11481164
options.otherArgs = options.otherArgs || {};
11491165
options.otherArgs.headers = options.otherArgs.headers || {};
11501166
options.otherArgs.headers['x-goog-request-params'] =
1151-
gax.routingHeader.fromParams({
1167+
this._gaxModule.routingHeader.fromParams({
11521168
parent: request.parent || '',
11531169
});
11541170
this.initialize();
@@ -1195,7 +1211,7 @@ export class ServiceUsageClient {
11951211
options.otherArgs = options.otherArgs || {};
11961212
options.otherArgs.headers = options.otherArgs.headers || {};
11971213
options.otherArgs.headers['x-goog-request-params'] =
1198-
gax.routingHeader.fromParams({
1214+
this._gaxModule.routingHeader.fromParams({
11991215
parent: request.parent || '',
12001216
});
12011217
const defaultCallSettings = this._defaults['listServices'];
@@ -1251,7 +1267,7 @@ export class ServiceUsageClient {
12511267
options.otherArgs = options.otherArgs || {};
12521268
options.otherArgs.headers = options.otherArgs.headers || {};
12531269
options.otherArgs.headers['x-goog-request-params'] =
1254-
gax.routingHeader.fromParams({
1270+
this._gaxModule.routingHeader.fromParams({
12551271
parent: request.parent || '',
12561272
});
12571273
const defaultCallSettings = this._defaults['listServices'];

0 commit comments

Comments
 (0)