Skip to content

Commit

Permalink
fix: allow passing gax instance to client constructor (googleapis#1617)
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 and feywind committed Sep 7, 2022
1 parent b8bf243 commit 103c7db
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 72 deletions.
61 changes: 37 additions & 24 deletions src/v1/publisher_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 {
IamClient,
IamProtos,
} 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 './publisher_client_config.json';

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

/**
Expand Down Expand Up @@ -100,8 +98,18 @@ export class PublisherClient {
* 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 PublisherClient({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 PublisherClient;
const servicePath =
Expand All @@ -121,8 +129,13 @@ export class PublisherClient {
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 All @@ -143,7 +156,7 @@ export class PublisherClient {
if (servicePath === staticMembers.servicePath) {
this.auth.defaultScopes = staticMembers.scopes;
}
this.iamClient = new IamClient(this._gaxGrpc, opts);
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);

// Determine the client header string.
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
Expand Down Expand Up @@ -214,7 +227,7 @@ export class PublisherClient {
'messages',
['topic'],
'message_ids',
gax.createByteLengthFunction(
this._gaxModule.GrpcClient.createByteLengthFunction(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protoFilesRoot.lookupType('google.pubsub.v1.PubsubMessage') as any
)
Expand All @@ -235,7 +248,7 @@ export class PublisherClient {
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 @@ -476,7 +489,7 @@ export class PublisherClient {
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 @@ -564,7 +577,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
'topic.name': request.topic!.name || '',
});
this.initialize();
Expand Down Expand Up @@ -649,7 +662,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
this.initialize();
Expand Down Expand Up @@ -731,7 +744,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
this.initialize();
Expand Down Expand Up @@ -817,7 +830,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
this.initialize();
Expand Down Expand Up @@ -902,7 +915,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
subscription: request.subscription || '',
});
this.initialize();
Expand Down Expand Up @@ -996,7 +1009,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
project: request.project || '',
});
this.initialize();
Expand Down Expand Up @@ -1037,7 +1050,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
project: request.project || '',
});
const defaultCallSettings = this._defaults['listTopics'];
Expand Down Expand Up @@ -1085,7 +1098,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
project: request.project || '',
});
const defaultCallSettings = this._defaults['listTopics'];
Expand Down Expand Up @@ -1192,7 +1205,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
this.initialize();
Expand Down Expand Up @@ -1237,7 +1250,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
const defaultCallSettings = this._defaults['listTopicSubscriptions'];
Expand Down Expand Up @@ -1285,7 +1298,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
const defaultCallSettings = this._defaults['listTopicSubscriptions'];
Expand Down Expand Up @@ -1390,7 +1403,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
this.initialize();
Expand Down Expand Up @@ -1431,7 +1444,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
const defaultCallSettings = this._defaults['listTopicSnapshots'];
Expand Down Expand Up @@ -1479,7 +1492,7 @@ export class PublisherClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
topic: request.topic || '',
});
const defaultCallSettings = this._defaults['listTopicSnapshots'];
Expand Down
45 changes: 29 additions & 16 deletions src/v1/schema_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 {
IamClient,
IamProtos,
} 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 './schema_service_client_config.json';

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

/**
Expand Down Expand Up @@ -99,8 +97,18 @@ export class SchemaServiceClient {
* 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 SchemaServiceClient({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 SchemaServiceClient;
const servicePath =
Expand All @@ -120,8 +128,13 @@ export class SchemaServiceClient {
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 All @@ -142,7 +155,7 @@ export class SchemaServiceClient {
if (servicePath === staticMembers.servicePath) {
this.auth.defaultScopes = staticMembers.scopes;
}
this.iamClient = new IamClient(this._gaxGrpc, opts);
this.iamClient = new this._gaxModule.IamClient(this._gaxGrpc, opts);

// Determine the client header string.
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
Expand Down Expand Up @@ -208,7 +221,7 @@ export class SchemaServiceClient {
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 @@ -424,7 +437,7 @@ export class SchemaServiceClient {
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 @@ -510,7 +523,7 @@ export class SchemaServiceClient {
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 @@ -592,7 +605,7 @@ export class SchemaServiceClient {
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 @@ -676,7 +689,7 @@ export class SchemaServiceClient {
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 @@ -768,7 +781,7 @@ export class SchemaServiceClient {
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 @@ -866,7 +879,7 @@ export class SchemaServiceClient {
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 @@ -911,7 +924,7 @@ export class SchemaServiceClient {
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['listSchemas'];
Expand Down Expand Up @@ -963,7 +976,7 @@ export class SchemaServiceClient {
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['listSchemas'];
Expand Down
Loading

0 comments on commit 103c7db

Please sign in to comment.