Skip to content

Commit

Permalink
feat: accept google-gax instance as a parameter (#342)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 474338479

Source-Link: googleapis/googleapis@d5d35e0

Source-Link: googleapis/googleapis-gen@efcd3f9
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9

fix: allow passing gax instance to client constructor
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 20, 2022
1 parent fdf1db9 commit a130929
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 170 deletions.
35 changes: 27 additions & 8 deletions packages/google-cloud-webrisk/src/v1/web_risk_service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
// ** All changes to this file may be overwritten. **

/* global window */
import * as gax from 'google-gax';
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import type * as gax from 'google-gax';
import type {
Callback,
CallOptions,
Descriptors,
ClientOptions,
} from 'google-gax';

import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');
Expand All @@ -28,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 './web_risk_service_client_config.json';

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

/**
Expand Down Expand Up @@ -89,8 +93,18 @@ export class WebRiskServiceClient {
* 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 WebRiskServiceClient({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 WebRiskServiceClient;
const servicePath =
Expand All @@ -110,8 +124,13 @@ export class WebRiskServiceClient {
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 @@ -174,7 +193,7 @@ export class WebRiskServiceClient {
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 @@ -663,8 +682,8 @@ export class WebRiskServiceClient {
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
gax.routingHeader.fromParams({
parent: request.parent || '',
this._gaxModule.routingHeader.fromParams({
parent: request.parent ?? '',
});
this.initialize();
return this.innerApiCalls.createSubmission(request, options, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
// ** All changes to this file may be overwritten. **

/* global window */
import * as gax from 'google-gax';
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
import type * as gax from 'google-gax';
import type {
Callback,
CallOptions,
Descriptors,
ClientOptions,
} from 'google-gax';

import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');
Expand All @@ -28,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 './web_risk_service_v1_beta1_client_config.json';

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

/**
Expand Down Expand Up @@ -88,8 +92,18 @@ export class WebRiskServiceV1Beta1Client {
* 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 WebRiskServiceV1Beta1Client({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 WebRiskServiceV1Beta1Client;
Expand All @@ -110,8 +124,13 @@ export class WebRiskServiceV1Beta1Client {
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 @@ -165,7 +184,7 @@ export class WebRiskServiceV1Beta1Client {
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
9 changes: 4 additions & 5 deletions packages/google-cloud-webrisk/system-test/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
import * as protoTypes from '../protos/protos';
import {assert} from 'chai';
import {describe, it} from 'mocha';
import {WebRiskServiceClient} from '../src/index';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const http2spy = require('http2spy');
const {WebRiskServiceV1Beta1Client} = http2spy.require(
require.resolve('../src/v1beta1')
);
const gax = http2spy.require('google-gax');
describe('header', () => {
it('populates x-goog-api-client header', async () => {
const client = new WebRiskServiceV1Beta1Client();
const client = new WebRiskServiceClient({}, gax);
const request = {
uri: 'http://testsafebrowsing.appspot.com/s/malware.html',
threatTypes: ['MALWARE'],
};
await client.searchUris(
request as unknown as protoTypes.google.cloud.webrisk.v1beta1.SearchUrisRequest
request as unknown as protoTypes.google.cloud.webrisk.v1.SearchUrisRequest
);
assert.ok(
/^gax\/[\w.-]+ gapic\/[\w.-]+ gl-node\/[0-9]+\.[\w.-]+ grpc\/[\w.-]+$/.test(
Expand Down
Loading

0 comments on commit a130929

Please sign in to comment.