Skip to content

Commit 392444e

Browse files
fix: allow passing gax instance to client constructor (#548)
- [ ] 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 ff7307d commit 392444e

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

packages/google-cloud-oslogin/src/v1/os_login_service_client.ts

+31-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './os_login_service_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -91,8 +95,18 @@ export class OsLoginServiceClient {
9195
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9296
* For more information, please check the
9397
* {@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 OsLoginServiceClient({fallback: 'rest'}, gax);
104+
* ```
94105
*/
95-
constructor(opts?: ClientOptions) {
106+
constructor(
107+
opts?: ClientOptions,
108+
gaxInstance?: typeof gax | typeof gax.fallback
109+
) {
96110
// Ensure that options include all the required fields.
97111
const staticMembers = this.constructor as typeof OsLoginServiceClient;
98112
const servicePath =
@@ -112,8 +126,13 @@ export class OsLoginServiceClient {
112126
opts['scopes'] = staticMembers.scopes;
113127
}
114128

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

118137
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
119138
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -180,7 +199,7 @@ export class OsLoginServiceClient {
180199
this.innerApiCalls = {};
181200

182201
// Add a warn function to the client constructor so it can be easily tested.
183-
this.warn = gax.warn;
202+
this.warn = this._gaxModule.warn;
184203
}
185204

186205
/**
@@ -395,7 +414,7 @@ export class OsLoginServiceClient {
395414
options.otherArgs = options.otherArgs || {};
396415
options.otherArgs.headers = options.otherArgs.headers || {};
397416
options.otherArgs.headers['x-goog-request-params'] =
398-
gax.routingHeader.fromParams({
417+
this._gaxModule.routingHeader.fromParams({
399418
name: request.name || '',
400419
});
401420
this.initialize();
@@ -488,7 +507,7 @@ export class OsLoginServiceClient {
488507
options.otherArgs = options.otherArgs || {};
489508
options.otherArgs.headers = options.otherArgs.headers || {};
490509
options.otherArgs.headers['x-goog-request-params'] =
491-
gax.routingHeader.fromParams({
510+
this._gaxModule.routingHeader.fromParams({
492511
name: request.name || '',
493512
});
494513
this.initialize();
@@ -578,7 +597,7 @@ export class OsLoginServiceClient {
578597
options.otherArgs = options.otherArgs || {};
579598
options.otherArgs.headers = options.otherArgs.headers || {};
580599
options.otherArgs.headers['x-goog-request-params'] =
581-
gax.routingHeader.fromParams({
600+
this._gaxModule.routingHeader.fromParams({
582601
name: request.name || '',
583602
});
584603
this.initialize();
@@ -665,7 +684,7 @@ export class OsLoginServiceClient {
665684
options.otherArgs = options.otherArgs || {};
666685
options.otherArgs.headers = options.otherArgs.headers || {};
667686
options.otherArgs.headers['x-goog-request-params'] =
668-
gax.routingHeader.fromParams({
687+
this._gaxModule.routingHeader.fromParams({
669688
name: request.name || '',
670689
});
671690
this.initialize();
@@ -762,7 +781,7 @@ export class OsLoginServiceClient {
762781
options.otherArgs = options.otherArgs || {};
763782
options.otherArgs.headers = options.otherArgs.headers || {};
764783
options.otherArgs.headers['x-goog-request-params'] =
765-
gax.routingHeader.fromParams({
784+
this._gaxModule.routingHeader.fromParams({
766785
parent: request.parent || '',
767786
});
768787
this.initialize();
@@ -860,7 +879,7 @@ export class OsLoginServiceClient {
860879
options.otherArgs = options.otherArgs || {};
861880
options.otherArgs.headers = options.otherArgs.headers || {};
862881
options.otherArgs.headers['x-goog-request-params'] =
863-
gax.routingHeader.fromParams({
882+
this._gaxModule.routingHeader.fromParams({
864883
name: request.name || '',
865884
});
866885
this.initialize();

packages/google-cloud-oslogin/src/v1beta/os_login_service_client.ts

+31-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './os_login_service_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -91,8 +95,18 @@ export class OsLoginServiceClient {
9195
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9296
* For more information, please check the
9397
* {@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 OsLoginServiceClient({fallback: 'rest'}, gax);
104+
* ```
94105
*/
95-
constructor(opts?: ClientOptions) {
106+
constructor(
107+
opts?: ClientOptions,
108+
gaxInstance?: typeof gax | typeof gax.fallback
109+
) {
96110
// Ensure that options include all the required fields.
97111
const staticMembers = this.constructor as typeof OsLoginServiceClient;
98112
const servicePath =
@@ -112,8 +126,13 @@ export class OsLoginServiceClient {
112126
opts['scopes'] = staticMembers.scopes;
113127
}
114128

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

118137
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
119138
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -180,7 +199,7 @@ export class OsLoginServiceClient {
180199
this.innerApiCalls = {};
181200

182201
// Add a warn function to the client constructor so it can be easily tested.
183-
this.warn = gax.warn;
202+
this.warn = this._gaxModule.warn;
184203
}
185204

186205
/**
@@ -397,7 +416,7 @@ export class OsLoginServiceClient {
397416
options.otherArgs = options.otherArgs || {};
398417
options.otherArgs.headers = options.otherArgs.headers || {};
399418
options.otherArgs.headers['x-goog-request-params'] =
400-
gax.routingHeader.fromParams({
419+
this._gaxModule.routingHeader.fromParams({
401420
name: request.name || '',
402421
});
403422
this.initialize();
@@ -490,7 +509,7 @@ export class OsLoginServiceClient {
490509
options.otherArgs = options.otherArgs || {};
491510
options.otherArgs.headers = options.otherArgs.headers || {};
492511
options.otherArgs.headers['x-goog-request-params'] =
493-
gax.routingHeader.fromParams({
512+
this._gaxModule.routingHeader.fromParams({
494513
name: request.name || '',
495514
});
496515
this.initialize();
@@ -586,7 +605,7 @@ export class OsLoginServiceClient {
586605
options.otherArgs = options.otherArgs || {};
587606
options.otherArgs.headers = options.otherArgs.headers || {};
588607
options.otherArgs.headers['x-goog-request-params'] =
589-
gax.routingHeader.fromParams({
608+
this._gaxModule.routingHeader.fromParams({
590609
name: request.name || '',
591610
});
592611
this.initialize();
@@ -679,7 +698,7 @@ export class OsLoginServiceClient {
679698
options.otherArgs = options.otherArgs || {};
680699
options.otherArgs.headers = options.otherArgs.headers || {};
681700
options.otherArgs.headers['x-goog-request-params'] =
682-
gax.routingHeader.fromParams({
701+
this._gaxModule.routingHeader.fromParams({
683702
name: request.name || '',
684703
});
685704
this.initialize();
@@ -776,7 +795,7 @@ export class OsLoginServiceClient {
776795
options.otherArgs = options.otherArgs || {};
777796
options.otherArgs.headers = options.otherArgs.headers || {};
778797
options.otherArgs.headers['x-goog-request-params'] =
779-
gax.routingHeader.fromParams({
798+
this._gaxModule.routingHeader.fromParams({
780799
parent: request.parent || '',
781800
});
782801
this.initialize();
@@ -874,7 +893,7 @@ export class OsLoginServiceClient {
874893
options.otherArgs = options.otherArgs || {};
875894
options.otherArgs.headers = options.otherArgs.headers || {};
876895
options.otherArgs.headers['x-goog-request-params'] =
877-
gax.routingHeader.fromParams({
896+
this._gaxModule.routingHeader.fromParams({
878897
name: request.name || '',
879898
});
880899
this.initialize();

0 commit comments

Comments
 (0)