Skip to content

Commit

Permalink
feat: deferred client initialization (#551)
Browse files Browse the repository at this point in the history
This PR includes changes from googleapis/gapic-generator-typescript#317
that will move the asynchronous initialization and authentication from the client constructor
to an `initialize()` method. This method will be automatically called when the first RPC call
is performed.

The client library usage has not changed, there is no need to update any code.

If you want to make sure the client is authenticated _before_ the first RPC call, you can do
```js
await client.initialize();
```
manually before calling any client method.
  • Loading branch information
gcf-merge-on-green[bot] committed Mar 6, 2020
1 parent 233c854 commit d36320e
Show file tree
Hide file tree
Showing 17 changed files with 967 additions and 251 deletions.
89 changes: 61 additions & 28 deletions packages/google-cloud-vision/src/v1/image_annotator_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ export class ImageAnnotatorClient {
private _innerApiCalls: {[name: string]: Function};
private _pathTemplates: {[name: string]: gax.PathTemplate};
private _terminated = false;
private _opts: ClientOptions;
private _gaxModule: typeof gax | typeof gax.fallback;
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
auth: gax.GoogleAuth;
operationsClient: gax.OperationsClient;
imageAnnotatorStub: Promise<{[name: string]: Function}>;
imageAnnotatorStub?: Promise<{[name: string]: Function}>;

/**
* Construct an instance of ImageAnnotatorClient.
Expand All @@ -70,8 +75,6 @@ export class ImageAnnotatorClient {
* app is running in an environment which supports
* {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
* your project ID will be detected automatically.
* @param {function} [options.promise] - Custom promise module to use instead
* of native Promises.
* @param {string} [options.apiEndpoint] - The domain name of the
* API remote host.
*/
Expand Down Expand Up @@ -101,25 +104,28 @@ export class ImageAnnotatorClient {
// If we are in browser, we are already using fallback because of the
// "browser" field in package.json.
// But if we were explicitly requested to use fallback, let's do it now.
const gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;
this._gaxModule = !isBrowser && opts.fallback ? gax.fallback : gax;

// Create a `gaxGrpc` object, with any grpc-specific options
// sent to the client.
opts.scopes = (this.constructor as typeof ImageAnnotatorClient).scopes;
const gaxGrpc = new gaxModule.GrpcClient(opts);
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);

// Save options to use in initialize() method.
this._opts = opts;

// Save the auth object to the client, for use by other methods.
this.auth = gaxGrpc.auth as gax.GoogleAuth;
this.auth = this._gaxGrpc.auth as gax.GoogleAuth;

// Determine the client header string.
const clientHeader = [`gax/${gaxModule.version}`, `gapic/${version}`];
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
if (typeof process !== 'undefined' && 'versions' in process) {
clientHeader.push(`gl-node/${process.versions.node}`);
} else {
clientHeader.push(`gl-web/${gaxModule.version}`);
clientHeader.push(`gl-web/${this._gaxModule.version}`);
}
if (!opts.fallback) {
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
Expand All @@ -135,21 +141,21 @@ export class ImageAnnotatorClient {
'protos',
'protos.json'
);
const protos = gaxGrpc.loadProto(
this._protos = this._gaxGrpc.loadProto(
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
);

// This API contains "path templates"; forward-slash-separated
// identifiers to uniquely identify resources within the API.
// Create useful helper objects for these.
this._pathTemplates = {
productPathTemplate: new gaxModule.PathTemplate(
productPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/locations/{location}/products/{product}'
),
productSetPathTemplate: new gaxModule.PathTemplate(
productSetPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/locations/{location}/productSets/{product_set}'
),
referenceImagePathTemplate: new gaxModule.PathTemplate(
referenceImagePathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/locations/{location}/products/{product}/referenceImages/{reference_image}'
),
};
Expand All @@ -158,13 +164,15 @@ export class ImageAnnotatorClient {
// an Operation object that allows for tracking of the operation,
// rather than holding a request open.
const protoFilesRoot = opts.fallback
? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'))
: gaxModule.protobuf.loadSync(nodejsProtoPath);
? this._gaxModule.protobuf.Root.fromJSON(
require('../../protos/protos.json')
)
: this._gaxModule.protobuf.loadSync(nodejsProtoPath);

this.operationsClient = gaxModule
this.operationsClient = this._gaxModule
.lro({
auth: this.auth,
grpc: 'grpc' in gaxGrpc ? gaxGrpc.grpc : undefined,
grpc: 'grpc' in this._gaxGrpc ? this._gaxGrpc.grpc : undefined,
})
.operationsClient(opts);
const asyncBatchAnnotateImagesResponse = protoFilesRoot.lookup(
Expand All @@ -181,7 +189,7 @@ export class ImageAnnotatorClient {
) as gax.protobuf.Type;

this._descriptors.longrunning = {
asyncBatchAnnotateImages: new gaxModule.LongrunningDescriptor(
asyncBatchAnnotateImages: new this._gaxModule.LongrunningDescriptor(
this.operationsClient,
asyncBatchAnnotateImagesResponse.decode.bind(
asyncBatchAnnotateImagesResponse
Expand All @@ -190,7 +198,7 @@ export class ImageAnnotatorClient {
asyncBatchAnnotateImagesMetadata
)
),
asyncBatchAnnotateFiles: new gaxModule.LongrunningDescriptor(
asyncBatchAnnotateFiles: new this._gaxModule.LongrunningDescriptor(
this.operationsClient,
asyncBatchAnnotateFilesResponse.decode.bind(
asyncBatchAnnotateFilesResponse
Expand All @@ -202,7 +210,7 @@ export class ImageAnnotatorClient {
};

// Put together the default options sent with requests.
const defaults = gaxGrpc.constructSettings(
this._defaults = this._gaxGrpc.constructSettings(
'google.cloud.vision.v1.ImageAnnotator',
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
Expand All @@ -213,17 +221,35 @@ export class ImageAnnotatorClient {
// of calling the API is handled in `google-gax`, with this code
// merely providing the destination and request information.
this._innerApiCalls = {};
}

/**
* Initialize the client.
* Performs asynchronous operations (such as authentication) and prepares the client.
* This function will be called automatically when any class method is called for the
* first time, but if you need to initialize it before calling an actual method,
* feel free to call initialize() directly.
*
* You can await on this method if you want to make sure the client is initialized.
*
* @returns {Promise} A promise that resolves to an authenticated service stub.
*/
initialize() {
// If the client stub promise is already initialized, return immediately.
if (this.imageAnnotatorStub) {
return this.imageAnnotatorStub;
}

// Put together the "service stub" for
// google.cloud.vision.v1.ImageAnnotator.
this.imageAnnotatorStub = gaxGrpc.createStub(
opts.fallback
? (protos as protobuf.Root).lookupService(
this.imageAnnotatorStub = this._gaxGrpc.createStub(
this._opts.fallback
? (this._protos as protobuf.Root).lookupService(
'google.cloud.vision.v1.ImageAnnotator'
)
: // tslint:disable-next-line no-any
(protos as any).google.cloud.vision.v1.ImageAnnotator,
opts
(this._protos as any).google.cloud.vision.v1.ImageAnnotator,
this._opts
) as Promise<{[method: string]: Function}>;

// Iterate over each of the methods that the service provides
Expand All @@ -248,9 +274,9 @@ export class ImageAnnotatorClient {
}
);

const apiCall = gaxModule.createApiCall(
const apiCall = this._gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName] ||
this._descriptors.longrunning[methodName]
Expand All @@ -264,6 +290,8 @@ export class ImageAnnotatorClient {
return apiCall(argument, callOptions, callback);
};
}

return this.imageAnnotatorStub;
}

/**
Expand Down Expand Up @@ -410,6 +438,7 @@ export class ImageAnnotatorClient {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
return this._innerApiCalls.batchAnnotateImages(request, options, callback);
}
batchAnnotateFiles(
Expand Down Expand Up @@ -502,6 +531,7 @@ export class ImageAnnotatorClient {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
return this._innerApiCalls.batchAnnotateFiles(request, options, callback);
}

Expand Down Expand Up @@ -612,6 +642,7 @@ export class ImageAnnotatorClient {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
return this._innerApiCalls.asyncBatchAnnotateImages(
request,
options,
Expand Down Expand Up @@ -720,6 +751,7 @@ export class ImageAnnotatorClient {
] = gax.routingHeader.fromParams({
parent: request.parent || '',
});
this.initialize();
return this._innerApiCalls.asyncBatchAnnotateFiles(
request,
options,
Expand Down Expand Up @@ -912,8 +944,9 @@ export class ImageAnnotatorClient {
* The client will no longer be usable and all future behavior is undefined.
*/
close(): Promise<void> {
this.initialize();
if (!this._terminated) {
return this.imageAnnotatorStub.then(stub => {
return this.imageAnnotatorStub!.then(stub => {
this._terminated = true;
stub.close();
});
Expand Down
Loading

0 comments on commit d36320e

Please sign in to comment.