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

Commit 51e82fd

Browse files
feat: accept google-gax instance as a parameter (#433)
* 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 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: use _gaxModule when accessing gax for bundling PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: https://github.com/googleapis/googleapis-gen/commit/f16a1d224f00a630ea43d6a9a1a31f566f45cdea Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZjE2YTFkMjI0ZjAwYTYzMGVhNDNkNmE5YTFhMzFmNTY2ZjQ1Y2RlYSJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: use gapic-generator-typescript v2.17.0 PiperOrigin-RevId: 474338479 Source-Link: googleapis/googleapis@d5d35e0 Source-Link: https://github.com/googleapis/googleapis-gen/commit/efcd3f93962a103f68f003e2a1eecde6fa216a27 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: use fully qualified request type name in tests PiperOrigin-RevId: 475685359 Source-Link: googleapis/googleapis@7a12973 Source-Link: https://github.com/googleapis/googleapis-gen/commit/370c729e2ba062a167449c27882ba5f379c5c34d Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMzcwYzcyOWUyYmEwNjJhMTY3NDQ5YzI3ODgyYmE1ZjM3OWM1YzM0ZCJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: owlbot post-processing for grafeas * fix: add Grafeas import back Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Alexander Fenster <[email protected]>
1 parent 411bae6 commit 51e82fd

7 files changed

+1525
-1267
lines changed

owlbot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def patch(library: pathlib.Path):
3030

3131
# perform surgery inserting the Grafeas client.
3232
s.replace(library / "src/v1/container_analysis_client.ts",
33-
"""import \* as gax from \'google-gax\';""",
34-
"""import * as gax from 'google-gax';
33+
"""import type \* as gax from \'google-gax\';""",
34+
"""import type * as gax from 'google-gax';
3535
import {GrafeasClient} from '@google-cloud/grafeas';""")
3636
s.replace(library / "src/v1/container_analysis_client.ts", "^}",
3737
r"""

src/v1/container_analysis_client.ts

+33-14
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
20+
import type * as gax from 'google-gax';
2121
import {GrafeasClient} from '@google-cloud/grafeas';
22-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
22+
import type {
23+
Callback,
24+
CallOptions,
25+
Descriptors,
26+
ClientOptions,
27+
} from 'google-gax';
2328

2429
import * as protos from '../../protos/protos';
2530
import jsonProtos = require('../../protos/protos.json');
@@ -29,7 +34,6 @@ import jsonProtos = require('../../protos/protos.json');
2934
* This file defines retry strategy and timeouts for all API methods in this library.
3035
*/
3136
import * as gapicConfig from './container_analysis_client_config.json';
32-
3337
const version = require('../../../package.json').version;
3438

3539
/**
@@ -101,8 +105,18 @@ export class ContainerAnalysisClient {
101105
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
102106
* For more information, please check the
103107
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
108+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
109+
* need to avoid loading the default gRPC version and want to use the fallback
110+
* HTTP implementation. Load only fallback version and pass it to the constructor:
111+
* ```
112+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
113+
* const client = new ContainerAnalysisClient({fallback: 'rest'}, gax);
114+
* ```
104115
*/
105-
constructor(opts?: ClientOptions) {
116+
constructor(
117+
opts?: ClientOptions,
118+
gaxInstance?: typeof gax | typeof gax.fallback
119+
) {
106120
// Ensure that options include all the required fields.
107121
const staticMembers = this.constructor as typeof ContainerAnalysisClient;
108122
const servicePath =
@@ -122,8 +136,13 @@ export class ContainerAnalysisClient {
122136
opts['scopes'] = staticMembers.scopes;
123137
}
124138

139+
// Load google-gax module synchronously if needed
140+
if (!gaxInstance) {
141+
gaxInstance = require('google-gax') as typeof gax;
142+
}
143+
125144
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
126-
this._gaxModule = opts.fallback ? gax.fallback : gax;
145+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
127146

128147
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
129148
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -192,7 +211,7 @@ export class ContainerAnalysisClient {
192211
this.innerApiCalls = {};
193212

194213
// Add a warn function to the client constructor so it can be easily tested.
195-
this.warn = gax.warn;
214+
this.warn = this._gaxModule.warn;
196215
}
197216

198217
/**
@@ -412,8 +431,8 @@ export class ContainerAnalysisClient {
412431
options.otherArgs = options.otherArgs || {};
413432
options.otherArgs.headers = options.otherArgs.headers || {};
414433
options.otherArgs.headers['x-goog-request-params'] =
415-
gax.routingHeader.fromParams({
416-
resource: request.resource || '',
434+
this._gaxModule.routingHeader.fromParams({
435+
resource: request.resource ?? '',
417436
});
418437
this.initialize();
419438
return this.innerApiCalls.setIamPolicy(request, options, callback);
@@ -506,8 +525,8 @@ export class ContainerAnalysisClient {
506525
options.otherArgs = options.otherArgs || {};
507526
options.otherArgs.headers = options.otherArgs.headers || {};
508527
options.otherArgs.headers['x-goog-request-params'] =
509-
gax.routingHeader.fromParams({
510-
resource: request.resource || '',
528+
this._gaxModule.routingHeader.fromParams({
529+
resource: request.resource ?? '',
511530
});
512531
this.initialize();
513532
return this.innerApiCalls.getIamPolicy(request, options, callback);
@@ -601,8 +620,8 @@ export class ContainerAnalysisClient {
601620
options.otherArgs = options.otherArgs || {};
602621
options.otherArgs.headers = options.otherArgs.headers || {};
603622
options.otherArgs.headers['x-goog-request-params'] =
604-
gax.routingHeader.fromParams({
605-
resource: request.resource || '',
623+
this._gaxModule.routingHeader.fromParams({
624+
resource: request.resource ?? '',
606625
});
607626
this.initialize();
608627
return this.innerApiCalls.testIamPermissions(request, options, callback);
@@ -701,8 +720,8 @@ export class ContainerAnalysisClient {
701720
options.otherArgs = options.otherArgs || {};
702721
options.otherArgs.headers = options.otherArgs.headers || {};
703722
options.otherArgs.headers['x-goog-request-params'] =
704-
gax.routingHeader.fromParams({
705-
parent: request.parent || '',
723+
this._gaxModule.routingHeader.fromParams({
724+
parent: request.parent ?? '',
706725
});
707726
this.initialize();
708727
return this.innerApiCalls.getVulnerabilityOccurrencesSummary(

src/v1beta1/container_analysis_v1_beta1_client.ts

+36-23
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {
20+
import type * as gax from 'google-gax';
21+
import type {
2222
Callback,
2323
CallOptions,
2424
Descriptors,
2525
ClientOptions,
2626
PaginationCallback,
2727
GaxCall,
2828
} from 'google-gax';
29-
3029
import {Transform} from 'stream';
3130
import * as protos from '../../protos/protos';
3231
import jsonProtos = require('../../protos/protos.json');
@@ -36,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3635
* This file defines retry strategy and timeouts for all API methods in this library.
3736
*/
3837
import * as gapicConfig from './container_analysis_v1_beta1_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

4240
/**
@@ -108,8 +106,18 @@ export class ContainerAnalysisV1Beta1Client {
108106
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
109107
* For more information, please check the
110108
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
109+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
110+
* need to avoid loading the default gRPC version and want to use the fallback
111+
* HTTP implementation. Load only fallback version and pass it to the constructor:
112+
* ```
113+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
114+
* const client = new ContainerAnalysisV1Beta1Client({fallback: 'rest'}, gax);
115+
* ```
111116
*/
112-
constructor(opts?: ClientOptions) {
117+
constructor(
118+
opts?: ClientOptions,
119+
gaxInstance?: typeof gax | typeof gax.fallback
120+
) {
113121
// Ensure that options include all the required fields.
114122
const staticMembers = this
115123
.constructor as typeof ContainerAnalysisV1Beta1Client;
@@ -130,8 +138,13 @@ export class ContainerAnalysisV1Beta1Client {
130138
opts['scopes'] = staticMembers.scopes;
131139
}
132140

141+
// Load google-gax module synchronously if needed
142+
if (!gaxInstance) {
143+
gaxInstance = require('google-gax') as typeof gax;
144+
}
145+
133146
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
134-
this._gaxModule = opts.fallback ? gax.fallback : gax;
147+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
135148

136149
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
137150
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -208,7 +221,7 @@ export class ContainerAnalysisV1Beta1Client {
208221
this.innerApiCalls = {};
209222

210223
// Add a warn function to the client constructor so it can be easily tested.
211-
this.warn = gax.warn;
224+
this.warn = this._gaxModule.warn;
212225
}
213226

214227
/**
@@ -430,8 +443,8 @@ export class ContainerAnalysisV1Beta1Client {
430443
options.otherArgs = options.otherArgs || {};
431444
options.otherArgs.headers = options.otherArgs.headers || {};
432445
options.otherArgs.headers['x-goog-request-params'] =
433-
gax.routingHeader.fromParams({
434-
resource: request.resource || '',
446+
this._gaxModule.routingHeader.fromParams({
447+
resource: request.resource ?? '',
435448
});
436449
this.initialize();
437450
return this.innerApiCalls.setIamPolicy(request, options, callback);
@@ -524,8 +537,8 @@ export class ContainerAnalysisV1Beta1Client {
524537
options.otherArgs = options.otherArgs || {};
525538
options.otherArgs.headers = options.otherArgs.headers || {};
526539
options.otherArgs.headers['x-goog-request-params'] =
527-
gax.routingHeader.fromParams({
528-
resource: request.resource || '',
540+
this._gaxModule.routingHeader.fromParams({
541+
resource: request.resource ?? '',
529542
});
530543
this.initialize();
531544
return this.innerApiCalls.getIamPolicy(request, options, callback);
@@ -619,8 +632,8 @@ export class ContainerAnalysisV1Beta1Client {
619632
options.otherArgs = options.otherArgs || {};
620633
options.otherArgs.headers = options.otherArgs.headers || {};
621634
options.otherArgs.headers['x-goog-request-params'] =
622-
gax.routingHeader.fromParams({
623-
resource: request.resource || '',
635+
this._gaxModule.routingHeader.fromParams({
636+
resource: request.resource ?? '',
624637
});
625638
this.initialize();
626639
return this.innerApiCalls.testIamPermissions(request, options, callback);
@@ -717,8 +730,8 @@ export class ContainerAnalysisV1Beta1Client {
717730
options.otherArgs = options.otherArgs || {};
718731
options.otherArgs.headers = options.otherArgs.headers || {};
719732
options.otherArgs.headers['x-goog-request-params'] =
720-
gax.routingHeader.fromParams({
721-
name: request.name || '',
733+
this._gaxModule.routingHeader.fromParams({
734+
name: request.name ?? '',
722735
});
723736
this.initialize();
724737
return this.innerApiCalls.getScanConfig(request, options, callback);
@@ -817,8 +830,8 @@ export class ContainerAnalysisV1Beta1Client {
817830
options.otherArgs = options.otherArgs || {};
818831
options.otherArgs.headers = options.otherArgs.headers || {};
819832
options.otherArgs.headers['x-goog-request-params'] =
820-
gax.routingHeader.fromParams({
821-
name: request.name || '',
833+
this._gaxModule.routingHeader.fromParams({
834+
name: request.name ?? '',
822835
});
823836
this.initialize();
824837
return this.innerApiCalls.updateScanConfig(request, options, callback);
@@ -919,8 +932,8 @@ export class ContainerAnalysisV1Beta1Client {
919932
options.otherArgs = options.otherArgs || {};
920933
options.otherArgs.headers = options.otherArgs.headers || {};
921934
options.otherArgs.headers['x-goog-request-params'] =
922-
gax.routingHeader.fromParams({
923-
parent: request.parent || '',
935+
this._gaxModule.routingHeader.fromParams({
936+
parent: request.parent ?? '',
924937
});
925938
this.initialize();
926939
return this.innerApiCalls.listScanConfigs(request, options, callback);
@@ -960,8 +973,8 @@ export class ContainerAnalysisV1Beta1Client {
960973
options.otherArgs = options.otherArgs || {};
961974
options.otherArgs.headers = options.otherArgs.headers || {};
962975
options.otherArgs.headers['x-goog-request-params'] =
963-
gax.routingHeader.fromParams({
964-
parent: request.parent || '',
976+
this._gaxModule.routingHeader.fromParams({
977+
parent: request.parent ?? '',
965978
});
966979
const defaultCallSettings = this._defaults['listScanConfigs'];
967980
const callSettings = defaultCallSettings.merge(options);
@@ -1010,8 +1023,8 @@ export class ContainerAnalysisV1Beta1Client {
10101023
options.otherArgs = options.otherArgs || {};
10111024
options.otherArgs.headers = options.otherArgs.headers || {};
10121025
options.otherArgs.headers['x-goog-request-params'] =
1013-
gax.routingHeader.fromParams({
1014-
parent: request.parent || '',
1026+
this._gaxModule.routingHeader.fromParams({
1027+
parent: request.parent ?? '',
10151028
});
10161029
const defaultCallSettings = this._defaults['listScanConfigs'];
10171030
const callSettings = defaultCallSettings.merge(options);

0 commit comments

Comments
 (0)