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

fix: allow passing gax instance to client constructor #814

Merged
merged 10 commits into from
Sep 8, 2022
87 changes: 52 additions & 35 deletions src/v3/translation_service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// ** All changes to this file may be overwritten. **

/* global window */
import * as gax from 'google-gax';
import {
import type * as gax from 'google-gax';
import type {
Callback,
CallOptions,
Descriptors,
Expand All @@ -28,7 +28,6 @@ import {
PaginationCallback,
GaxCall,
} from 'google-gax';

import {Transform} from 'stream';
import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');
Expand All @@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json');
* This file defines retry strategy and timeouts for all API methods in this library.
*/
import * as gapicConfig from './translation_service_client_config.json';
import {operationsProtos} from 'google-gax';
const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -99,8 +97,18 @@ export class TranslationServiceClient {
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
* For more information, please check the
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
* need to avoid loading the default gRPC version and want to use the fallback
* HTTP implementation. Load only fallback version and pass it to the constructor:
* ```
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
* const client = new TranslationServiceClient({fallback: 'rest'}, gax);
* ```
*/
constructor(opts?: ClientOptions) {
constructor(
opts?: ClientOptions,
gaxInstance?: typeof gax | typeof gax.fallback
) {
// Ensure that options include all the required fields.
const staticMembers = this.constructor as typeof TranslationServiceClient;
const servicePath =
Expand All @@ -120,8 +128,13 @@ export class TranslationServiceClient {
opts['scopes'] = staticMembers.scopes;
}

// Load google-gax module synchronously if needed
if (!gaxInstance) {
gaxInstance = require('google-gax') as typeof gax;
}

// Choose either gRPC or proto-over-HTTP implementation of google-gax.
this._gaxModule = opts.fallback ? gax.fallback : gax;
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;

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

// Add a warn function to the client constructor so it can be easily tested.
this.warn = gax.warn;
this.warn = this._gaxModule.warn;
}

/**
Expand Down Expand Up @@ -577,7 +590,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -703,7 +716,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -830,7 +843,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -978,7 +991,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -1064,7 +1077,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
name: request.name || '',
});
this.initialize();
Expand Down Expand Up @@ -1224,7 +1237,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand All @@ -1250,11 +1263,12 @@ export class TranslationServiceClient {
protos.google.cloud.translation.v3.BatchTranslateMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const request =
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
const decodeOperation = new this._gaxModule.Operation(
operation,
this.descriptors.longrunning.batchTranslateText,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1420,7 +1434,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -1450,11 +1464,12 @@ export class TranslationServiceClient {
protos.google.cloud.translation.v3.BatchTranslateDocumentMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const request =
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
const decodeOperation = new this._gaxModule.Operation(
operation,
this.descriptors.longrunning.batchTranslateDocument,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1564,7 +1579,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand All @@ -1590,11 +1605,12 @@ export class TranslationServiceClient {
protos.google.cloud.translation.v3.CreateGlossaryMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const request =
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
const decodeOperation = new this._gaxModule.Operation(
operation,
this.descriptors.longrunning.createGlossary,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1703,7 +1719,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
name: request.name || '',
});
this.initialize();
Expand All @@ -1729,11 +1745,12 @@ export class TranslationServiceClient {
protos.google.cloud.translation.v3.DeleteGlossaryMetadata
>
> {
const request = new operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const request =
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
{name}
);
const [operation] = await this.operationsClient.getOperation(request);
const decodeOperation = new gax.Operation(
const decodeOperation = new this._gaxModule.Operation(
operation,
this.descriptors.longrunning.deleteGlossary,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1857,7 +1874,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
Expand Down Expand Up @@ -1916,7 +1933,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
const defaultCallSettings = this._defaults['listGlossaries'];
Expand Down Expand Up @@ -1984,7 +2001,7 @@ export class TranslationServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
});
const defaultCallSettings = this._defaults['listGlossaries'];
Expand Down
Loading