Skip to content

Commit a130929

Browse files
feat: accept google-gax instance as a parameter (#342)
- [ ] 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
1 parent fdf1db9 commit a130929

File tree

5 files changed

+129
-170
lines changed

5 files changed

+129
-170
lines changed

packages/google-cloud-webrisk/src/v1/web_risk_service_client.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './web_risk_service_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -89,8 +93,18 @@ export class WebRiskServiceClient {
8993
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9094
* For more information, please check the
9195
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
96+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
97+
* need to avoid loading the default gRPC version and want to use the fallback
98+
* HTTP implementation. Load only fallback version and pass it to the constructor:
99+
* ```
100+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
101+
* const client = new WebRiskServiceClient({fallback: 'rest'}, gax);
102+
* ```
92103
*/
93-
constructor(opts?: ClientOptions) {
104+
constructor(
105+
opts?: ClientOptions,
106+
gaxInstance?: typeof gax | typeof gax.fallback
107+
) {
94108
// Ensure that options include all the required fields.
95109
const staticMembers = this.constructor as typeof WebRiskServiceClient;
96110
const servicePath =
@@ -110,8 +124,13 @@ export class WebRiskServiceClient {
110124
opts['scopes'] = staticMembers.scopes;
111125
}
112126

127+
// Load google-gax module synchronously if needed
128+
if (!gaxInstance) {
129+
gaxInstance = require('google-gax') as typeof gax;
130+
}
131+
113132
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
114-
this._gaxModule = opts.fallback ? gax.fallback : gax;
133+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
115134

116135
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
117136
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -174,7 +193,7 @@ export class WebRiskServiceClient {
174193
this.innerApiCalls = {};
175194

176195
// Add a warn function to the client constructor so it can be easily tested.
177-
this.warn = gax.warn;
196+
this.warn = this._gaxModule.warn;
178197
}
179198

180199
/**
@@ -663,8 +682,8 @@ export class WebRiskServiceClient {
663682
options.otherArgs = options.otherArgs || {};
664683
options.otherArgs.headers = options.otherArgs.headers || {};
665684
options.otherArgs.headers['x-goog-request-params'] =
666-
gax.routingHeader.fromParams({
667-
parent: request.parent || '',
685+
this._gaxModule.routingHeader.fromParams({
686+
parent: request.parent ?? '',
668687
});
669688
this.initialize();
670689
return this.innerApiCalls.createSubmission(request, options, callback);

packages/google-cloud-webrisk/src/v1beta1/web_risk_service_v1_beta1_client.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './web_risk_service_v1_beta1_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -88,8 +92,18 @@ export class WebRiskServiceV1Beta1Client {
8892
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
8993
* For more information, please check the
9094
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
95+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
96+
* need to avoid loading the default gRPC version and want to use the fallback
97+
* HTTP implementation. Load only fallback version and pass it to the constructor:
98+
* ```
99+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
100+
* const client = new WebRiskServiceV1Beta1Client({fallback: 'rest'}, gax);
101+
* ```
91102
*/
92-
constructor(opts?: ClientOptions) {
103+
constructor(
104+
opts?: ClientOptions,
105+
gaxInstance?: typeof gax | typeof gax.fallback
106+
) {
93107
// Ensure that options include all the required fields.
94108
const staticMembers = this
95109
.constructor as typeof WebRiskServiceV1Beta1Client;
@@ -110,8 +124,13 @@ export class WebRiskServiceV1Beta1Client {
110124
opts['scopes'] = staticMembers.scopes;
111125
}
112126

127+
// Load google-gax module synchronously if needed
128+
if (!gaxInstance) {
129+
gaxInstance = require('google-gax') as typeof gax;
130+
}
131+
113132
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
114-
this._gaxModule = opts.fallback ? gax.fallback : gax;
133+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
115134

116135
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
117136
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -165,7 +184,7 @@ export class WebRiskServiceV1Beta1Client {
165184
this.innerApiCalls = {};
166185

167186
// Add a warn function to the client constructor so it can be easily tested.
168-
this.warn = gax.warn;
187+
this.warn = this._gaxModule.warn;
169188
}
170189

171190
/**

packages/google-cloud-webrisk/system-test/header.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
import * as protoTypes from '../protos/protos';
1616
import {assert} from 'chai';
1717
import {describe, it} from 'mocha';
18+
import {WebRiskServiceClient} from '../src/index';
1819
// eslint-disable-next-line @typescript-eslint/no-var-requires
1920
const http2spy = require('http2spy');
20-
const {WebRiskServiceV1Beta1Client} = http2spy.require(
21-
require.resolve('../src/v1beta1')
22-
);
21+
const gax = http2spy.require('google-gax');
2322
describe('header', () => {
2423
it('populates x-goog-api-client header', async () => {
25-
const client = new WebRiskServiceV1Beta1Client();
24+
const client = new WebRiskServiceClient({}, gax);
2625
const request = {
2726
uri: 'http://testsafebrowsing.appspot.com/s/malware.html',
2827
threatTypes: ['MALWARE'],
2928
};
3029
await client.searchUris(
31-
request as unknown as protoTypes.google.cloud.webrisk.v1beta1.SearchUrisRequest
30+
request as unknown as protoTypes.google.cloud.webrisk.v1.SearchUrisRequest
3231
);
3332
assert.ok(
3433
/^gax\/[\w.-]+ gapic\/[\w.-]+ gl-node\/[0-9]+\.[\w.-]+ grpc\/[\w.-]+$/.test(

0 commit comments

Comments
 (0)