Skip to content

Commit 510022c

Browse files
fix: allow passing gax instance to client constructor (#96)
- [ ] 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 49eccf3 commit 510022c

File tree

2 files changed

+126
-90
lines changed

2 files changed

+126
-90
lines changed

packages/google-cloud-tpu/src/v1/tpu_client.ts

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

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

320333
// Add a warn function to the client constructor so it can be easily tested.
321-
this.warn = gax.warn;
334+
this.warn = this._gaxModule.warn;
322335
}
323336

324337
/**
@@ -528,7 +541,7 @@ export class TpuClient {
528541
options.otherArgs = options.otherArgs || {};
529542
options.otherArgs.headers = options.otherArgs.headers || {};
530543
options.otherArgs.headers['x-goog-request-params'] =
531-
gax.routingHeader.fromParams({
544+
this._gaxModule.routingHeader.fromParams({
532545
name: request.name || '',
533546
});
534547
this.initialize();
@@ -619,7 +632,7 @@ export class TpuClient {
619632
options.otherArgs = options.otherArgs || {};
620633
options.otherArgs.headers = options.otherArgs.headers || {};
621634
options.otherArgs.headers['x-goog-request-params'] =
622-
gax.routingHeader.fromParams({
635+
this._gaxModule.routingHeader.fromParams({
623636
name: request.name || '',
624637
});
625638
this.initialize();
@@ -704,7 +717,7 @@ export class TpuClient {
704717
options.otherArgs = options.otherArgs || {};
705718
options.otherArgs.headers = options.otherArgs.headers || {};
706719
options.otherArgs.headers['x-goog-request-params'] =
707-
gax.routingHeader.fromParams({
720+
this._gaxModule.routingHeader.fromParams({
708721
name: request.name || '',
709722
});
710723
this.initialize();
@@ -812,7 +825,7 @@ export class TpuClient {
812825
options.otherArgs = options.otherArgs || {};
813826
options.otherArgs.headers = options.otherArgs.headers || {};
814827
options.otherArgs.headers['x-goog-request-params'] =
815-
gax.routingHeader.fromParams({
828+
this._gaxModule.routingHeader.fromParams({
816829
parent: request.parent || '',
817830
});
818831
this.initialize();
@@ -838,11 +851,12 @@ export class TpuClient {
838851
protos.google.cloud.tpu.v1.OperationMetadata
839852
>
840853
> {
841-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
842-
{name}
843-
);
854+
const request =
855+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
856+
{name}
857+
);
844858
const [operation] = await this.operationsClient.getOperation(request);
845-
const decodeOperation = new gax.Operation(
859+
const decodeOperation = new this._gaxModule.Operation(
846860
operation,
847861
this.descriptors.longrunning.createNode,
848862
this._gaxModule.createDefaultBackoffSettings()
@@ -949,7 +963,7 @@ export class TpuClient {
949963
options.otherArgs = options.otherArgs || {};
950964
options.otherArgs.headers = options.otherArgs.headers || {};
951965
options.otherArgs.headers['x-goog-request-params'] =
952-
gax.routingHeader.fromParams({
966+
this._gaxModule.routingHeader.fromParams({
953967
name: request.name || '',
954968
});
955969
this.initialize();
@@ -975,11 +989,12 @@ export class TpuClient {
975989
protos.google.cloud.tpu.v1.OperationMetadata
976990
>
977991
> {
978-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
979-
{name}
980-
);
992+
const request =
993+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
994+
{name}
995+
);
981996
const [operation] = await this.operationsClient.getOperation(request);
982-
const decodeOperation = new gax.Operation(
997+
const decodeOperation = new this._gaxModule.Operation(
983998
operation,
984999
this.descriptors.longrunning.deleteNode,
9851000
this._gaxModule.createDefaultBackoffSettings()
@@ -1088,7 +1103,7 @@ export class TpuClient {
10881103
options.otherArgs = options.otherArgs || {};
10891104
options.otherArgs.headers = options.otherArgs.headers || {};
10901105
options.otherArgs.headers['x-goog-request-params'] =
1091-
gax.routingHeader.fromParams({
1106+
this._gaxModule.routingHeader.fromParams({
10921107
name: request.name || '',
10931108
});
10941109
this.initialize();
@@ -1114,11 +1129,12 @@ export class TpuClient {
11141129
protos.google.cloud.tpu.v1.OperationMetadata
11151130
>
11161131
> {
1117-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1118-
{name}
1119-
);
1132+
const request =
1133+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1134+
{name}
1135+
);
11201136
const [operation] = await this.operationsClient.getOperation(request);
1121-
const decodeOperation = new gax.Operation(
1137+
const decodeOperation = new this._gaxModule.Operation(
11221138
operation,
11231139
this.descriptors.longrunning.reimageNode,
11241140
this._gaxModule.createDefaultBackoffSettings()
@@ -1225,7 +1241,7 @@ export class TpuClient {
12251241
options.otherArgs = options.otherArgs || {};
12261242
options.otherArgs.headers = options.otherArgs.headers || {};
12271243
options.otherArgs.headers['x-goog-request-params'] =
1228-
gax.routingHeader.fromParams({
1244+
this._gaxModule.routingHeader.fromParams({
12291245
name: request.name || '',
12301246
});
12311247
this.initialize();
@@ -1251,11 +1267,12 @@ export class TpuClient {
12511267
protos.google.cloud.tpu.v1.OperationMetadata
12521268
>
12531269
> {
1254-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1255-
{name}
1256-
);
1270+
const request =
1271+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1272+
{name}
1273+
);
12571274
const [operation] = await this.operationsClient.getOperation(request);
1258-
const decodeOperation = new gax.Operation(
1275+
const decodeOperation = new this._gaxModule.Operation(
12591276
operation,
12601277
this.descriptors.longrunning.stopNode,
12611278
this._gaxModule.createDefaultBackoffSettings()
@@ -1362,7 +1379,7 @@ export class TpuClient {
13621379
options.otherArgs = options.otherArgs || {};
13631380
options.otherArgs.headers = options.otherArgs.headers || {};
13641381
options.otherArgs.headers['x-goog-request-params'] =
1365-
gax.routingHeader.fromParams({
1382+
this._gaxModule.routingHeader.fromParams({
13661383
name: request.name || '',
13671384
});
13681385
this.initialize();
@@ -1388,11 +1405,12 @@ export class TpuClient {
13881405
protos.google.cloud.tpu.v1.OperationMetadata
13891406
>
13901407
> {
1391-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1392-
{name}
1393-
);
1408+
const request =
1409+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1410+
{name}
1411+
);
13941412
const [operation] = await this.operationsClient.getOperation(request);
1395-
const decodeOperation = new gax.Operation(
1413+
const decodeOperation = new this._gaxModule.Operation(
13961414
operation,
13971415
this.descriptors.longrunning.startNode,
13981416
this._gaxModule.createDefaultBackoffSettings()
@@ -1486,7 +1504,7 @@ export class TpuClient {
14861504
options.otherArgs = options.otherArgs || {};
14871505
options.otherArgs.headers = options.otherArgs.headers || {};
14881506
options.otherArgs.headers['x-goog-request-params'] =
1489-
gax.routingHeader.fromParams({
1507+
this._gaxModule.routingHeader.fromParams({
14901508
parent: request.parent || '',
14911509
});
14921510
this.initialize();
@@ -1524,7 +1542,7 @@ export class TpuClient {
15241542
options.otherArgs = options.otherArgs || {};
15251543
options.otherArgs.headers = options.otherArgs.headers || {};
15261544
options.otherArgs.headers['x-goog-request-params'] =
1527-
gax.routingHeader.fromParams({
1545+
this._gaxModule.routingHeader.fromParams({
15281546
parent: request.parent || '',
15291547
});
15301548
const defaultCallSettings = this._defaults['listNodes'];
@@ -1571,7 +1589,7 @@ export class TpuClient {
15711589
options.otherArgs = options.otherArgs || {};
15721590
options.otherArgs.headers = options.otherArgs.headers || {};
15731591
options.otherArgs.headers['x-goog-request-params'] =
1574-
gax.routingHeader.fromParams({
1592+
this._gaxModule.routingHeader.fromParams({
15751593
parent: request.parent || '',
15761594
});
15771595
const defaultCallSettings = this._defaults['listNodes'];
@@ -1679,7 +1697,7 @@ export class TpuClient {
16791697
options.otherArgs = options.otherArgs || {};
16801698
options.otherArgs.headers = options.otherArgs.headers || {};
16811699
options.otherArgs.headers['x-goog-request-params'] =
1682-
gax.routingHeader.fromParams({
1700+
this._gaxModule.routingHeader.fromParams({
16831701
parent: request.parent || '',
16841702
});
16851703
this.initialize();
@@ -1725,7 +1743,7 @@ export class TpuClient {
17251743
options.otherArgs = options.otherArgs || {};
17261744
options.otherArgs.headers = options.otherArgs.headers || {};
17271745
options.otherArgs.headers['x-goog-request-params'] =
1728-
gax.routingHeader.fromParams({
1746+
this._gaxModule.routingHeader.fromParams({
17291747
parent: request.parent || '',
17301748
});
17311749
const defaultCallSettings = this._defaults['listTensorFlowVersions'];
@@ -1776,7 +1794,7 @@ export class TpuClient {
17761794
options.otherArgs = options.otherArgs || {};
17771795
options.otherArgs.headers = options.otherArgs.headers || {};
17781796
options.otherArgs.headers['x-goog-request-params'] =
1779-
gax.routingHeader.fromParams({
1797+
this._gaxModule.routingHeader.fromParams({
17801798
parent: request.parent || '',
17811799
});
17821800
const defaultCallSettings = this._defaults['listTensorFlowVersions'];
@@ -1884,7 +1902,7 @@ export class TpuClient {
18841902
options.otherArgs = options.otherArgs || {};
18851903
options.otherArgs.headers = options.otherArgs.headers || {};
18861904
options.otherArgs.headers['x-goog-request-params'] =
1887-
gax.routingHeader.fromParams({
1905+
this._gaxModule.routingHeader.fromParams({
18881906
parent: request.parent || '',
18891907
});
18901908
this.initialize();
@@ -1926,7 +1944,7 @@ export class TpuClient {
19261944
options.otherArgs = options.otherArgs || {};
19271945
options.otherArgs.headers = options.otherArgs.headers || {};
19281946
options.otherArgs.headers['x-goog-request-params'] =
1929-
gax.routingHeader.fromParams({
1947+
this._gaxModule.routingHeader.fromParams({
19301948
parent: request.parent || '',
19311949
});
19321950
const defaultCallSettings = this._defaults['listAcceleratorTypes'];
@@ -1977,7 +1995,7 @@ export class TpuClient {
19771995
options.otherArgs = options.otherArgs || {};
19781996
options.otherArgs.headers = options.otherArgs.headers || {};
19791997
options.otherArgs.headers['x-goog-request-params'] =
1980-
gax.routingHeader.fromParams({
1998+
this._gaxModule.routingHeader.fromParams({
19811999
parent: request.parent || '',
19822000
});
19832001
const defaultCallSettings = this._defaults['listAcceleratorTypes'];

0 commit comments

Comments
 (0)