Skip to content

Commit

Permalink
fix: allow passing gax instance to client constructor (#249)
Browse files Browse the repository at this point in the history
- [ ] 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
  • Loading branch information
gcf-owl-bot[bot] authored Sep 1, 2022
1 parent dcb7f5f commit 35d6bee
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
// ** 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,
ClientOptions,
GoogleError,
} from 'google-gax';

import {PassThrough} from 'stream';
import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');
Expand All @@ -35,7 +33,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 './speech_translation_service_client_config.json';

const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -94,8 +91,18 @@ export class SpeechTranslationServiceClient {
* 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 SpeechTranslationServiceClient({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 SpeechTranslationServiceClient;
Expand All @@ -116,8 +123,13 @@ export class SpeechTranslationServiceClient {
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 @@ -161,7 +173,7 @@ export class SpeechTranslationServiceClient {
// Provide descriptors for these.
this.descriptors.stream = {
streamingTranslateSpeech: new this._gaxModule.StreamDescriptor(
gax.StreamType.BIDI_STREAMING,
this._gaxModule.StreamType.BIDI_STREAMING,
opts.fallback === 'rest'
),
};
Expand All @@ -180,7 +192,7 @@ export class SpeechTranslationServiceClient {
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 @@ -227,7 +239,9 @@ export class SpeechTranslationServiceClient {
setImmediate(() => {
stream.emit(
'error',
new GoogleError('The client has already been closed.')
new this._gaxModule.GoogleError(
'The client has already been closed.'
)
);
});
return stream;
Expand Down

0 comments on commit 35d6bee

Please sign in to comment.