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

Commit

Permalink
fix: allow passing gax instance to client constructor (#814)
Browse files Browse the repository at this point in the history
* 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

* 🦉 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: googleapis/googleapis-gen@f16a1d2
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 <fenster@google.com>
  • Loading branch information
3 people committed Sep 8, 2022
1 parent fc9b38d commit ddc93f9
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 80 deletions.
87 changes: 52 additions & 35 deletions src/v3/translation_service_client.ts
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

0 comments on commit ddc93f9

Please sign in to comment.