Skip to content

Commit 59e0c8e

Browse files
fix: allow passing gax instance to client constructor (#1042)
- [ ] 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
1 parent 3228332 commit 59e0c8e

8 files changed

+304
-179
lines changed

packages/google-cloud-vision/src/v1/image_annotator_client.ts

+34-18
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
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,
@@ -35,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3535
* This file defines retry strategy and timeouts for all API methods in this library.
3636
*/
3737
import * as gapicConfig from './image_annotator_client_config.json';
38-
import {operationsProtos} from 'google-gax';
3938
const version = require('../../../package.json').version;
4039

4140
/**
@@ -98,8 +97,18 @@ export class ImageAnnotatorClient {
9897
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9998
* For more information, please check the
10099
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
100+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
101+
* need to avoid loading the default gRPC version and want to use the fallback
102+
* HTTP implementation. Load only fallback version and pass it to the constructor:
103+
* ```
104+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
105+
* const client = new ImageAnnotatorClient({fallback: 'rest'}, gax);
106+
* ```
101107
*/
102-
constructor(opts?: ClientOptions) {
108+
constructor(
109+
opts?: ClientOptions,
110+
gaxInstance?: typeof gax | typeof gax.fallback
111+
) {
103112
// Ensure that options include all the required fields.
104113
const staticMembers = this.constructor as typeof ImageAnnotatorClient;
105114
const servicePath =
@@ -119,8 +128,13 @@ export class ImageAnnotatorClient {
119128
opts['scopes'] = staticMembers.scopes;
120129
}
121130

131+
// Load google-gax module synchronously if needed
132+
if (!gaxInstance) {
133+
gaxInstance = require('google-gax') as typeof gax;
134+
}
135+
122136
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
123-
this._gaxModule = opts.fallback ? gax.fallback : gax;
137+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
124138

125139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
126140
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -248,7 +262,7 @@ export class ImageAnnotatorClient {
248262
this.innerApiCalls = {};
249263

250264
// Add a warn function to the client constructor so it can be easily tested.
251-
this.warn = gax.warn;
265+
this.warn = this._gaxModule.warn;
252266
}
253267

254268
/**
@@ -472,7 +486,7 @@ export class ImageAnnotatorClient {
472486
options.otherArgs = options.otherArgs || {};
473487
options.otherArgs.headers = options.otherArgs.headers || {};
474488
options.otherArgs.headers['x-goog-request-params'] =
475-
gax.routingHeader.fromParams({
489+
this._gaxModule.routingHeader.fromParams({
476490
parent: request.parent || '',
477491
});
478492
this.initialize();
@@ -583,7 +597,7 @@ export class ImageAnnotatorClient {
583597
options.otherArgs = options.otherArgs || {};
584598
options.otherArgs.headers = options.otherArgs.headers || {};
585599
options.otherArgs.headers['x-goog-request-params'] =
586-
gax.routingHeader.fromParams({
600+
this._gaxModule.routingHeader.fromParams({
587601
parent: request.parent || '',
588602
});
589603
this.initialize();
@@ -710,7 +724,7 @@ export class ImageAnnotatorClient {
710724
options.otherArgs = options.otherArgs || {};
711725
options.otherArgs.headers = options.otherArgs.headers || {};
712726
options.otherArgs.headers['x-goog-request-params'] =
713-
gax.routingHeader.fromParams({
727+
this._gaxModule.routingHeader.fromParams({
714728
parent: request.parent || '',
715729
});
716730
this.initialize();
@@ -740,11 +754,12 @@ export class ImageAnnotatorClient {
740754
protos.google.cloud.vision.v1.OperationMetadata
741755
>
742756
> {
743-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
744-
{name}
745-
);
757+
const request =
758+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
759+
{name}
760+
);
746761
const [operation] = await this.operationsClient.getOperation(request);
747-
const decodeOperation = new gax.Operation(
762+
const decodeOperation = new this._gaxModule.Operation(
748763
operation,
749764
this.descriptors.longrunning.asyncBatchAnnotateImages,
750765
this._gaxModule.createDefaultBackoffSettings()
@@ -869,7 +884,7 @@ export class ImageAnnotatorClient {
869884
options.otherArgs = options.otherArgs || {};
870885
options.otherArgs.headers = options.otherArgs.headers || {};
871886
options.otherArgs.headers['x-goog-request-params'] =
872-
gax.routingHeader.fromParams({
887+
this._gaxModule.routingHeader.fromParams({
873888
parent: request.parent || '',
874889
});
875890
this.initialize();
@@ -899,11 +914,12 @@ export class ImageAnnotatorClient {
899914
protos.google.cloud.vision.v1.OperationMetadata
900915
>
901916
> {
902-
const request = new operationsProtos.google.longrunning.GetOperationRequest(
903-
{name}
904-
);
917+
const request =
918+
new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest(
919+
{name}
920+
);
905921
const [operation] = await this.operationsClient.getOperation(request);
906-
const decodeOperation = new gax.Operation(
922+
const decodeOperation = new this._gaxModule.Operation(
907923
operation,
908924
this.descriptors.longrunning.asyncBatchAnnotateFiles,
909925
this._gaxModule.createDefaultBackoffSettings()

0 commit comments

Comments
 (0)