Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Commit ddc93f9

Browse files
fix: allow passing gax instance to client constructor (#814)
* 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: https://github.com/googleapis/googleapis-gen/commit/e97a1ac204ead4fe7341f91e72db7c6ac6016341 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: use _gaxModule when accessing gax for bundling PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: https://github.com/googleapis/googleapis-gen/commit/f16a1d224f00a630ea43d6a9a1a31f566f45cdea Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZjE2YTFkMjI0ZjAwYTYzMGVhNDNkNmE5YTFhMzFmNTY2ZjQ1Y2RlYSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: point http2spy to gax since it's now loaded dynamically * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: make it work * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Alexander Fenster <[email protected]>
1 parent fc9b38d commit ddc93f9

File tree

3 files changed

+116
-80
lines changed

3 files changed

+116
-80
lines changed

src/v3/translation_service_client.ts

+52-35
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 './translation_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 TranslationServiceClient {
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 TranslationServiceClient({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.constructor as typeof TranslationServiceClient;
106114
const servicePath =
@@ -120,8 +128,13 @@ export class TranslationServiceClient {
120128
opts['scopes'] = staticMembers.scopes;
121129
}
122130

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

126139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
127140
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -296,7 +309,7 @@ export class TranslationServiceClient {
296309
this.innerApiCalls = {};
297310

298311
// Add a warn function to the client constructor so it can be easily tested.
299-
this.warn = gax.warn;
312+
this.warn = this._gaxModule.warn;
300313
}
301314

302315
/**
@@ -577,7 +590,7 @@ export class TranslationServiceClient {
577590
options.otherArgs = options.otherArgs || {};
578591
options.otherArgs.headers = options.otherArgs.headers || {};
579592
options.otherArgs.headers['x-goog-request-params'] =
580-
gax.routingHeader.fromParams({
593+
this._gaxModule.routingHeader.fromParams({
581594
parent: request.parent || '',
582595
});
583596
this.initialize();
@@ -703,7 +716,7 @@ export class TranslationServiceClient {
703716
options.otherArgs = options.otherArgs || {};
704717
options.otherArgs.headers = options.otherArgs.headers || {};
705718
options.otherArgs.headers['x-goog-request-params'] =
706-
gax.routingHeader.fromParams({
719+
this._gaxModule.routingHeader.fromParams({
707720
parent: request.parent || '',
708721
});
709722
this.initialize();
@@ -830,7 +843,7 @@ export class TranslationServiceClient {
830843
options.otherArgs = options.otherArgs || {};
831844
options.otherArgs.headers = options.otherArgs.headers || {};
832845
options.otherArgs.headers['x-goog-request-params'] =
833-
gax.routingHeader.fromParams({
846+
this._gaxModule.routingHeader.fromParams({
834847
parent: request.parent || '',
835848
});
836849
this.initialize();
@@ -978,7 +991,7 @@ export class TranslationServiceClient {
978991
options.otherArgs = options.otherArgs || {};
979992
options.otherArgs.headers = options.otherArgs.headers || {};
980993
options.otherArgs.headers['x-goog-request-params'] =
981-
gax.routingHeader.fromParams({
994+
this._gaxModule.routingHeader.fromParams({
982995
parent: request.parent || '',
983996
});
984997
this.initialize();
@@ -1064,7 +1077,7 @@ export class TranslationServiceClient {
10641077
options.otherArgs = options.otherArgs || {};
10651078
options.otherArgs.headers = options.otherArgs.headers || {};
10661079
options.otherArgs.headers['x-goog-request-params'] =
1067-
gax.routingHeader.fromParams({
1080+
this._gaxModule.routingHeader.fromParams({
10681081
name: request.name || '',
10691082
});
10701083
this.initialize();
@@ -1224,7 +1237,7 @@ export class TranslationServiceClient {
12241237
options.otherArgs = options.otherArgs || {};
12251238
options.otherArgs.headers = options.otherArgs.headers || {};
12261239
options.otherArgs.headers['x-goog-request-params'] =
1227-
gax.routingHeader.fromParams({
1240+
this._gaxModule.routingHeader.fromParams({
12281241
parent: request.parent || '',
12291242
});
12301243
this.initialize();
@@ -1250,11 +1263,12 @@ export class TranslationServiceClient {
12501263
protos.google.cloud.translation.v3.BatchTranslateMetadata
12511264
>
12521265
> {
1253-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1254-
{name}
1255-
);
1266+
const request =
1267+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1268+
{name}
1269+
);
12561270
const [operation] = await this.operationsClient.getOperation(request);
1257-
const decodeOperation = new gax.Operation(
1271+
const decodeOperation = new this._gaxModule.Operation(
12581272
operation,
12591273
this.descriptors.longrunning.batchTranslateText,
12601274
this._gaxModule.createDefaultBackoffSettings()
@@ -1420,7 +1434,7 @@ export class TranslationServiceClient {
14201434
options.otherArgs = options.otherArgs || {};
14211435
options.otherArgs.headers = options.otherArgs.headers || {};
14221436
options.otherArgs.headers['x-goog-request-params'] =
1423-
gax.routingHeader.fromParams({
1437+
this._gaxModule.routingHeader.fromParams({
14241438
parent: request.parent || '',
14251439
});
14261440
this.initialize();
@@ -1450,11 +1464,12 @@ export class TranslationServiceClient {
14501464
protos.google.cloud.translation.v3.BatchTranslateDocumentMetadata
14511465
>
14521466
> {
1453-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1454-
{name}
1455-
);
1467+
const request =
1468+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1469+
{name}
1470+
);
14561471
const [operation] = await this.operationsClient.getOperation(request);
1457-
const decodeOperation = new gax.Operation(
1472+
const decodeOperation = new this._gaxModule.Operation(
14581473
operation,
14591474
this.descriptors.longrunning.batchTranslateDocument,
14601475
this._gaxModule.createDefaultBackoffSettings()
@@ -1564,7 +1579,7 @@ export class TranslationServiceClient {
15641579
options.otherArgs = options.otherArgs || {};
15651580
options.otherArgs.headers = options.otherArgs.headers || {};
15661581
options.otherArgs.headers['x-goog-request-params'] =
1567-
gax.routingHeader.fromParams({
1582+
this._gaxModule.routingHeader.fromParams({
15681583
parent: request.parent || '',
15691584
});
15701585
this.initialize();
@@ -1590,11 +1605,12 @@ export class TranslationServiceClient {
15901605
protos.google.cloud.translation.v3.CreateGlossaryMetadata
15911606
>
15921607
> {
1593-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1594-
{name}
1595-
);
1608+
const request =
1609+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1610+
{name}
1611+
);
15961612
const [operation] = await this.operationsClient.getOperation(request);
1597-
const decodeOperation = new gax.Operation(
1613+
const decodeOperation = new this._gaxModule.Operation(
15981614
operation,
15991615
this.descriptors.longrunning.createGlossary,
16001616
this._gaxModule.createDefaultBackoffSettings()
@@ -1703,7 +1719,7 @@ export class TranslationServiceClient {
17031719
options.otherArgs = options.otherArgs || {};
17041720
options.otherArgs.headers = options.otherArgs.headers || {};
17051721
options.otherArgs.headers['x-goog-request-params'] =
1706-
gax.routingHeader.fromParams({
1722+
this._gaxModule.routingHeader.fromParams({
17071723
name: request.name || '',
17081724
});
17091725
this.initialize();
@@ -1729,11 +1745,12 @@ export class TranslationServiceClient {
17291745
protos.google.cloud.translation.v3.DeleteGlossaryMetadata
17301746
>
17311747
> {
1732-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
1733-
{name}
1734-
);
1748+
const request =
1749+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
1750+
{name}
1751+
);
17351752
const [operation] = await this.operationsClient.getOperation(request);
1736-
const decodeOperation = new gax.Operation(
1753+
const decodeOperation = new this._gaxModule.Operation(
17371754
operation,
17381755
this.descriptors.longrunning.deleteGlossary,
17391756
this._gaxModule.createDefaultBackoffSettings()
@@ -1857,7 +1874,7 @@ export class TranslationServiceClient {
18571874
options.otherArgs = options.otherArgs || {};
18581875
options.otherArgs.headers = options.otherArgs.headers || {};
18591876
options.otherArgs.headers['x-goog-request-params'] =
1860-
gax.routingHeader.fromParams({
1877+
this._gaxModule.routingHeader.fromParams({
18611878
parent: request.parent || '',
18621879
});
18631880
this.initialize();
@@ -1916,7 +1933,7 @@ export class TranslationServiceClient {
19161933
options.otherArgs = options.otherArgs || {};
19171934
options.otherArgs.headers = options.otherArgs.headers || {};
19181935
options.otherArgs.headers['x-goog-request-params'] =
1919-
gax.routingHeader.fromParams({
1936+
this._gaxModule.routingHeader.fromParams({
19201937
parent: request.parent || '',
19211938
});
19221939
const defaultCallSettings = this._defaults['listGlossaries'];
@@ -1984,7 +2001,7 @@ export class TranslationServiceClient {
19842001
options.otherArgs = options.otherArgs || {};
19852002
options.otherArgs.headers = options.otherArgs.headers || {};
19862003
options.otherArgs.headers['x-goog-request-params'] =
1987-
gax.routingHeader.fromParams({
2004+
this._gaxModule.routingHeader.fromParams({
19882005
parent: request.parent || '',
19892006
});
19902007
const defaultCallSettings = this._defaults['listGlossaries'];

0 commit comments

Comments
 (0)