Skip to content

Commit 19798e2

Browse files
fix: allow passing gax instance to client constructor (#294)
- [ ] 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 1b4c34e commit 19798e2

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

packages/google-monitoring-dashboard/src/v1/dashboards_service_client.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
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,
2525
ClientOptions,
2626
PaginationCallback,
2727
GaxCall,
2828
} from 'google-gax';
29-
3029
import {Transform} from 'stream';
3130
import * as protos from '../../protos/protos';
3231
import jsonProtos = require('../../protos/protos.json');
@@ -36,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3635
* This file defines retry strategy and timeouts for all API methods in this library.
3736
*/
3837
import * as gapicConfig from './dashboards_service_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

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

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

124137
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
125138
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -199,7 +212,7 @@ export class DashboardsServiceClient {
199212
this.innerApiCalls = {};
200213

201214
// Add a warn function to the client constructor so it can be easily tested.
202-
this.warn = gax.warn;
215+
this.warn = this._gaxModule.warn;
203216
}
204217

205218
/**
@@ -424,7 +437,7 @@ export class DashboardsServiceClient {
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();
@@ -523,7 +536,7 @@ export class DashboardsServiceClient {
523536
options.otherArgs = options.otherArgs || {};
524537
options.otherArgs.headers = options.otherArgs.headers || {};
525538
options.otherArgs.headers['x-goog-request-params'] =
526-
gax.routingHeader.fromParams({
539+
this._gaxModule.routingHeader.fromParams({
527540
name: request.name || '',
528541
});
529542
this.initialize();
@@ -620,7 +633,7 @@ export class DashboardsServiceClient {
620633
options.otherArgs = options.otherArgs || {};
621634
options.otherArgs.headers = options.otherArgs.headers || {};
622635
options.otherArgs.headers['x-goog-request-params'] =
623-
gax.routingHeader.fromParams({
636+
this._gaxModule.routingHeader.fromParams({
624637
name: request.name || '',
625638
});
626639
this.initialize();
@@ -718,7 +731,7 @@ export class DashboardsServiceClient {
718731
options.otherArgs = options.otherArgs || {};
719732
options.otherArgs.headers = options.otherArgs.headers || {};
720733
options.otherArgs.headers['x-goog-request-params'] =
721-
gax.routingHeader.fromParams({
734+
this._gaxModule.routingHeader.fromParams({
722735
'dashboard.name': request.dashboard!.name || '',
723736
});
724737
this.initialize();
@@ -826,7 +839,7 @@ export class DashboardsServiceClient {
826839
options.otherArgs = options.otherArgs || {};
827840
options.otherArgs.headers = options.otherArgs.headers || {};
828841
options.otherArgs.headers['x-goog-request-params'] =
829-
gax.routingHeader.fromParams({
842+
this._gaxModule.routingHeader.fromParams({
830843
parent: request.parent || '',
831844
});
832845
this.initialize();
@@ -869,7 +882,7 @@ export class DashboardsServiceClient {
869882
options.otherArgs = options.otherArgs || {};
870883
options.otherArgs.headers = options.otherArgs.headers || {};
871884
options.otherArgs.headers['x-goog-request-params'] =
872-
gax.routingHeader.fromParams({
885+
this._gaxModule.routingHeader.fromParams({
873886
parent: request.parent || '',
874887
});
875888
const defaultCallSettings = this._defaults['listDashboards'];
@@ -921,7 +934,7 @@ export class DashboardsServiceClient {
921934
options.otherArgs = options.otherArgs || {};
922935
options.otherArgs.headers = options.otherArgs.headers || {};
923936
options.otherArgs.headers['x-goog-request-params'] =
924-
gax.routingHeader.fromParams({
937+
this._gaxModule.routingHeader.fromParams({
925938
parent: request.parent || '',
926939
});
927940
const defaultCallSettings = this._defaults['listDashboards'];

0 commit comments

Comments
 (0)