Skip to content

Commit 4067666

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: 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 a6361ce commit 4067666

8 files changed

+350
-222
lines changed

packages/google-cloud-gaming/src/v1/game_server_clusters_service_client.ts

Lines changed: 45 additions & 29 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,
@@ -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 './game_server_clusters_service_client_config.json';
41-
import {operationsProtos} from 'google-gax';
4240
const version = require('../../../package.json').version;
4341

4442
/**
@@ -100,8 +98,18 @@ export class GameServerClustersServiceClient {
10098
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
10199
* For more information, please check the
102100
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
101+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
102+
* need to avoid loading the default gRPC version and want to use the fallback
103+
* HTTP implementation. Load only fallback version and pass it to the constructor:
104+
* ```
105+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
106+
* const client = new GameServerClustersServiceClient({fallback: 'rest'}, gax);
107+
* ```
103108
*/
104-
constructor(opts?: ClientOptions) {
109+
constructor(
110+
opts?: ClientOptions,
111+
gaxInstance?: typeof gax | typeof gax.fallback
112+
) {
105113
// Ensure that options include all the required fields.
106114
const staticMembers = this
107115
.constructor as typeof GameServerClustersServiceClient;
@@ -122,8 +130,13 @@ export class GameServerClustersServiceClient {
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);
@@ -313,7 +326,7 @@ export class GameServerClustersServiceClient {
313326
this.innerApiCalls = {};
314327

315328
// Add a warn function to the client constructor so it can be easily tested.
316-
this.warn = gax.warn;
329+
this.warn = this._gaxModule.warn;
317330
}
318331

319332
/**
@@ -537,7 +550,7 @@ export class GameServerClustersServiceClient {
537550
options.otherArgs = options.otherArgs || {};
538551
options.otherArgs.headers = options.otherArgs.headers || {};
539552
options.otherArgs.headers['x-goog-request-params'] =
540-
gax.routingHeader.fromParams({
553+
this._gaxModule.routingHeader.fromParams({
541554
name: request.name || '',
542555
});
543556
this.initialize();
@@ -645,7 +658,7 @@ export class GameServerClustersServiceClient {
645658
options.otherArgs = options.otherArgs || {};
646659
options.otherArgs.headers = options.otherArgs.headers || {};
647660
options.otherArgs.headers['x-goog-request-params'] =
648-
gax.routingHeader.fromParams({
661+
this._gaxModule.routingHeader.fromParams({
649662
parent: request.parent || '',
650663
});
651664
this.initialize();
@@ -749,7 +762,7 @@ export class GameServerClustersServiceClient {
749762
options.otherArgs = options.otherArgs || {};
750763
options.otherArgs.headers = options.otherArgs.headers || {};
751764
options.otherArgs.headers['x-goog-request-params'] =
752-
gax.routingHeader.fromParams({
765+
this._gaxModule.routingHeader.fromParams({
753766
name: request.name || '',
754767
});
755768
this.initialize();
@@ -857,7 +870,7 @@ export class GameServerClustersServiceClient {
857870
options.otherArgs = options.otherArgs || {};
858871
options.otherArgs.headers = options.otherArgs.headers || {};
859872
options.otherArgs.headers['x-goog-request-params'] =
860-
gax.routingHeader.fromParams({
873+
this._gaxModule.routingHeader.fromParams({
861874
'game_server_cluster.name': request.gameServerCluster!.name || '',
862875
});
863876
this.initialize();
@@ -970,7 +983,7 @@ export class GameServerClustersServiceClient {
970983
options.otherArgs = options.otherArgs || {};
971984
options.otherArgs.headers = options.otherArgs.headers || {};
972985
options.otherArgs.headers['x-goog-request-params'] =
973-
gax.routingHeader.fromParams({
986+
this._gaxModule.routingHeader.fromParams({
974987
parent: request.parent || '',
975988
});
976989
this.initialize();
@@ -1000,11 +1013,12 @@ export class GameServerClustersServiceClient {
10001013
protos.google.cloud.gaming.v1.OperationMetadata
10011014
>
10021015
> {
1003-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1004-
{name}
1005-
);
1016+
const request =
1017+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1018+
{name}
1019+
);
10061020
const [operation] = await this.operationsClient.getOperation(request);
1007-
const decodeOperation = new gax.Operation(
1021+
const decodeOperation = new this._gaxModule.Operation(
10081022
operation,
10091023
this.descriptors.longrunning.createGameServerCluster,
10101024
this._gaxModule.createDefaultBackoffSettings()
@@ -1112,7 +1126,7 @@ export class GameServerClustersServiceClient {
11121126
options.otherArgs = options.otherArgs || {};
11131127
options.otherArgs.headers = options.otherArgs.headers || {};
11141128
options.otherArgs.headers['x-goog-request-params'] =
1115-
gax.routingHeader.fromParams({
1129+
this._gaxModule.routingHeader.fromParams({
11161130
name: request.name || '',
11171131
});
11181132
this.initialize();
@@ -1142,11 +1156,12 @@ export class GameServerClustersServiceClient {
11421156
protos.google.cloud.gaming.v1.OperationMetadata
11431157
>
11441158
> {
1145-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1146-
{name}
1147-
);
1159+
const request =
1160+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1161+
{name}
1162+
);
11481163
const [operation] = await this.operationsClient.getOperation(request);
1149-
const decodeOperation = new gax.Operation(
1164+
const decodeOperation = new this._gaxModule.Operation(
11501165
operation,
11511166
this.descriptors.longrunning.deleteGameServerCluster,
11521167
this._gaxModule.createDefaultBackoffSettings()
@@ -1258,7 +1273,7 @@ export class GameServerClustersServiceClient {
12581273
options.otherArgs = options.otherArgs || {};
12591274
options.otherArgs.headers = options.otherArgs.headers || {};
12601275
options.otherArgs.headers['x-goog-request-params'] =
1261-
gax.routingHeader.fromParams({
1276+
this._gaxModule.routingHeader.fromParams({
12621277
'game_server_cluster.name': request.gameServerCluster!.name || '',
12631278
});
12641279
this.initialize();
@@ -1288,11 +1303,12 @@ export class GameServerClustersServiceClient {
12881303
protos.google.cloud.gaming.v1.OperationMetadata
12891304
>
12901305
> {
1291-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1292-
{name}
1293-
);
1306+
const request =
1307+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1308+
{name}
1309+
);
12941310
const [operation] = await this.operationsClient.getOperation(request);
1295-
const decodeOperation = new gax.Operation(
1311+
const decodeOperation = new this._gaxModule.Operation(
12961312
operation,
12971313
this.descriptors.longrunning.updateGameServerCluster,
12981314
this._gaxModule.createDefaultBackoffSettings()
@@ -1411,7 +1427,7 @@ export class GameServerClustersServiceClient {
14111427
options.otherArgs = options.otherArgs || {};
14121428
options.otherArgs.headers = options.otherArgs.headers || {};
14131429
options.otherArgs.headers['x-goog-request-params'] =
1414-
gax.routingHeader.fromParams({
1430+
this._gaxModule.routingHeader.fromParams({
14151431
parent: request.parent || '',
14161432
});
14171433
this.initialize();
@@ -1470,7 +1486,7 @@ export class GameServerClustersServiceClient {
14701486
options.otherArgs = options.otherArgs || {};
14711487
options.otherArgs.headers = options.otherArgs.headers || {};
14721488
options.otherArgs.headers['x-goog-request-params'] =
1473-
gax.routingHeader.fromParams({
1489+
this._gaxModule.routingHeader.fromParams({
14741490
parent: request.parent || '',
14751491
});
14761492
const defaultCallSettings = this._defaults['listGameServerClusters'];
@@ -1534,7 +1550,7 @@ export class GameServerClustersServiceClient {
15341550
options.otherArgs = options.otherArgs || {};
15351551
options.otherArgs.headers = options.otherArgs.headers || {};
15361552
options.otherArgs.headers['x-goog-request-params'] =
1537-
gax.routingHeader.fromParams({
1553+
this._gaxModule.routingHeader.fromParams({
15381554
parent: request.parent || '',
15391555
});
15401556
const defaultCallSettings = this._defaults['listGameServerClusters'];

packages/google-cloud-gaming/src/v1/game_server_configs_service_client.ts

Lines changed: 36 additions & 21 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,
@@ -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 './game_server_configs_service_client_config.json';
41-
import {operationsProtos} from 'google-gax';
4240
const version = require('../../../package.json').version;
4341

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

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

127140
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
128141
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -297,7 +310,7 @@ export class GameServerConfigsServiceClient {
297310
this.innerApiCalls = {};
298311

299312
// Add a warn function to the client constructor so it can be easily tested.
300-
this.warn = gax.warn;
313+
this.warn = this._gaxModule.warn;
301314
}
302315

303316
/**
@@ -509,7 +522,7 @@ export class GameServerConfigsServiceClient {
509522
options.otherArgs = options.otherArgs || {};
510523
options.otherArgs.headers = options.otherArgs.headers || {};
511524
options.otherArgs.headers['x-goog-request-params'] =
512-
gax.routingHeader.fromParams({
525+
this._gaxModule.routingHeader.fromParams({
513526
name: request.name || '',
514527
});
515528
this.initialize();
@@ -620,7 +633,7 @@ export class GameServerConfigsServiceClient {
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
parent: request.parent || '',
625638
});
626639
this.initialize();
@@ -650,11 +663,12 @@ export class GameServerConfigsServiceClient {
650663
protos.google.cloud.gaming.v1.OperationMetadata
651664
>
652665
> {
653-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
654-
{name}
655-
);
666+
const request =
667+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
668+
{name}
669+
);
656670
const [operation] = await this.operationsClient.getOperation(request);
657-
const decodeOperation = new gax.Operation(
671+
const decodeOperation = new this._gaxModule.Operation(
658672
operation,
659673
this.descriptors.longrunning.createGameServerConfig,
660674
this._gaxModule.createDefaultBackoffSettings()
@@ -763,7 +777,7 @@ export class GameServerConfigsServiceClient {
763777
options.otherArgs = options.otherArgs || {};
764778
options.otherArgs.headers = options.otherArgs.headers || {};
765779
options.otherArgs.headers['x-goog-request-params'] =
766-
gax.routingHeader.fromParams({
780+
this._gaxModule.routingHeader.fromParams({
767781
name: request.name || '',
768782
});
769783
this.initialize();
@@ -793,11 +807,12 @@ export class GameServerConfigsServiceClient {
793807
protos.google.cloud.gaming.v1.OperationMetadata
794808
>
795809
> {
796-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
797-
{name}
798-
);
810+
const request =
811+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
812+
{name}
813+
);
799814
const [operation] = await this.operationsClient.getOperation(request);
800-
const decodeOperation = new gax.Operation(
815+
const decodeOperation = new this._gaxModule.Operation(
801816
operation,
802817
this.descriptors.longrunning.deleteGameServerConfig,
803818
this._gaxModule.createDefaultBackoffSettings()
@@ -910,7 +925,7 @@ export class GameServerConfigsServiceClient {
910925
options.otherArgs = options.otherArgs || {};
911926
options.otherArgs.headers = options.otherArgs.headers || {};
912927
options.otherArgs.headers['x-goog-request-params'] =
913-
gax.routingHeader.fromParams({
928+
this._gaxModule.routingHeader.fromParams({
914929
parent: request.parent || '',
915930
});
916931
this.initialize();
@@ -958,7 +973,7 @@ export class GameServerConfigsServiceClient {
958973
options.otherArgs = options.otherArgs || {};
959974
options.otherArgs.headers = options.otherArgs.headers || {};
960975
options.otherArgs.headers['x-goog-request-params'] =
961-
gax.routingHeader.fromParams({
976+
this._gaxModule.routingHeader.fromParams({
962977
parent: request.parent || '',
963978
});
964979
const defaultCallSettings = this._defaults['listGameServerConfigs'];
@@ -1015,7 +1030,7 @@ export class GameServerConfigsServiceClient {
10151030
options.otherArgs = options.otherArgs || {};
10161031
options.otherArgs.headers = options.otherArgs.headers || {};
10171032
options.otherArgs.headers['x-goog-request-params'] =
1018-
gax.routingHeader.fromParams({
1033+
this._gaxModule.routingHeader.fromParams({
10191034
parent: request.parent || '',
10201035
});
10211036
const defaultCallSettings = this._defaults['listGameServerConfigs'];

0 commit comments

Comments
 (0)