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

Commit

Permalink
fix: allow passing gax instance to client constructor (#24)
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: https://github.com/googleapis/googleapis-gen/commit/f16a1d224f00a630ea43d6a9a1a31f566f45cdea
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: https://github.com/googleapis/googleapis-gen/commit/e97a1ac204ead4fe7341f91e72db7c6ac6016341
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
  • Loading branch information
gcf-owl-bot[bot] authored Sep 1, 2022
1 parent acda49f commit 91e2ac5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 45 deletions.
46 changes: 30 additions & 16 deletions src/v2/revisions_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 './revisions_client_config.json';
import {operationsProtos} from 'google-gax';
const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -99,8 +97,18 @@ export class RevisionsClient {
* 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 RevisionsClient({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 RevisionsClient;
const servicePath =
Expand All @@ -120,8 +128,13 @@ export class RevisionsClient {
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 @@ -250,7 +263,7 @@ export class RevisionsClient {
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 @@ -467,7 +480,7 @@ export class RevisionsClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.getRevision(request, options, callback);
}
Expand Down Expand Up @@ -590,7 +603,7 @@ export class RevisionsClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.deleteRevision(request, options, callback);
}
Expand All @@ -614,11 +627,12 @@ export class RevisionsClient {
protos.google.cloud.run.v2.Revision
>
> {
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.deleteRevision,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -731,7 +745,7 @@ export class RevisionsClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.listRevisions(request, options, callback);
}
Expand Down Expand Up @@ -786,7 +800,7 @@ export class RevisionsClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
const defaultCallSettings = this._defaults['listRevisions'];
const callSettings = defaultCallSettings.merge(options);
this.initialize();
Expand Down Expand Up @@ -850,7 +864,7 @@ export class RevisionsClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
const defaultCallSettings = this._defaults['listRevisions'];
const callSettings = defaultCallSettings.merge(options);
this.initialize();
Expand Down
74 changes: 45 additions & 29 deletions src/v2/services_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 './services_client_config.json';
import {operationsProtos} from 'google-gax';
const version = require('../../../package.json').version;

/**
Expand Down Expand Up @@ -99,8 +97,18 @@ export class ServicesClient {
* 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 ServicesClient({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 ServicesClient;
const servicePath =
Expand All @@ -120,8 +128,13 @@ export class ServicesClient {
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 @@ -269,7 +282,7 @@ export class ServicesClient {
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 @@ -490,7 +503,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.getService(request, options, callback);
}
Expand Down Expand Up @@ -576,7 +589,7 @@ export class ServicesClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
resource: request.resource || '',
});
this.initialize();
Expand Down Expand Up @@ -672,7 +685,7 @@ export class ServicesClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
resource: request.resource || '',
});
this.initialize();
Expand Down Expand Up @@ -763,7 +776,7 @@ export class ServicesClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
this._gaxModule.routingHeader.fromParams({
resource: request.resource || '',
});
this.initialize();
Expand Down Expand Up @@ -889,7 +902,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.createService(request, options, callback);
}
Expand All @@ -913,11 +926,12 @@ export class ServicesClient {
protos.google.cloud.run.v2.Service
>
> {
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.createService,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1044,7 +1058,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.updateService(request, options, callback);
}
Expand All @@ -1068,11 +1082,12 @@ export class ServicesClient {
protos.google.cloud.run.v2.Service
>
> {
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.updateService,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1201,7 +1216,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.deleteService(request, options, callback);
}
Expand All @@ -1225,11 +1240,12 @@ export class ServicesClient {
protos.google.cloud.run.v2.Service
>
> {
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.deleteService,
this._gaxModule.createDefaultBackoffSettings()
Expand Down Expand Up @@ -1341,7 +1357,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
this.initialize();
return this.innerApiCalls.listServices(request, options, callback);
}
Expand Down Expand Up @@ -1395,7 +1411,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
const defaultCallSettings = this._defaults['listServices'];
const callSettings = defaultCallSettings.merge(options);
this.initialize();
Expand Down Expand Up @@ -1458,7 +1474,7 @@ export class ServicesClient {
}
}
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams(routingParameter);
this._gaxModule.routingHeader.fromParams(routingParameter);
const defaultCallSettings = this._defaults['listServices'];
const callSettings = defaultCallSettings.merge(options);
this.initialize();
Expand Down

0 comments on commit 91e2ac5

Please sign in to comment.