diff --git a/packages/google-cloud-automl/linkinator.config.json b/packages/google-cloud-automl/linkinator.config.json index af4fa72352d..471cb1eadfd 100644 --- a/packages/google-cloud-automl/linkinator.config.json +++ b/packages/google-cloud-automl/linkinator.config.json @@ -5,5 +5,7 @@ "https://codecov.io/gh/googleapis/", "www.googleapis.com", "img.shields.io" - ] + ], + "silent": true, + "concurrency": 10 } diff --git a/packages/google-cloud-automl/src/v1/auto_ml_client.ts b/packages/google-cloud-automl/src/v1/auto_ml_client.ts index 5b12a04e732..c1c801af809 100644 --- a/packages/google-cloud-automl/src/v1/auto_ml_client.ts +++ b/packages/google-cloud-automl/src/v1/auto_ml_client.ts @@ -58,9 +58,14 @@ export class AutoMlClient { 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; - autoMlStub: Promise<{[name: string]: Function}>; + autoMlStub?: Promise<{[name: string]: Function}>; /** * Construct an instance of AutoMlClient. @@ -84,8 +89,6 @@ export class AutoMlClient { * 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. */ @@ -115,25 +118,28 @@ export class AutoMlClient { // 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 AutoMlClient).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}`); @@ -149,7 +155,7 @@ export class AutoMlClient { 'protos', 'protos.json' ); - const protos = gaxGrpc.loadProto( + this._protos = this._gaxGrpc.loadProto( opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); @@ -157,19 +163,19 @@ export class AutoMlClient { // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - annotationSpecPathTemplate: new gaxModule.PathTemplate( + annotationSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/annotationSpecs/{annotation_spec}' ), - datasetPathTemplate: new gaxModule.PathTemplate( + datasetPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}' ), - locationPathTemplate: new gaxModule.PathTemplate( + locationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}' ), - modelPathTemplate: new gaxModule.PathTemplate( + modelPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}' ), - modelEvaluationPathTemplate: new gaxModule.PathTemplate( + modelEvaluationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}/modelEvaluations/{model_evaluation}' ), }; @@ -178,17 +184,17 @@ export class AutoMlClient { // (e.g. 50 results at a time, with tokens to get subsequent // pages). Denote the keys used for pagination and results. this._descriptors.page = { - listDatasets: new gaxModule.PageDescriptor( + listDatasets: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'datasets' ), - listModels: new gaxModule.PageDescriptor( + listModels: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'model' ), - listModelEvaluations: new gaxModule.PageDescriptor( + listModelEvaluations: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'modelEvaluation' @@ -199,13 +205,15 @@ export class AutoMlClient { // 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 createDatasetResponse = protoFilesRoot.lookup( @@ -264,47 +272,47 @@ export class AutoMlClient { ) as gax.protobuf.Type; this._descriptors.longrunning = { - createDataset: new gaxModule.LongrunningDescriptor( + createDataset: new this._gaxModule.LongrunningDescriptor( this.operationsClient, createDatasetResponse.decode.bind(createDatasetResponse), createDatasetMetadata.decode.bind(createDatasetMetadata) ), - deleteDataset: new gaxModule.LongrunningDescriptor( + deleteDataset: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deleteDatasetResponse.decode.bind(deleteDatasetResponse), deleteDatasetMetadata.decode.bind(deleteDatasetMetadata) ), - importData: new gaxModule.LongrunningDescriptor( + importData: new this._gaxModule.LongrunningDescriptor( this.operationsClient, importDataResponse.decode.bind(importDataResponse), importDataMetadata.decode.bind(importDataMetadata) ), - exportData: new gaxModule.LongrunningDescriptor( + exportData: new this._gaxModule.LongrunningDescriptor( this.operationsClient, exportDataResponse.decode.bind(exportDataResponse), exportDataMetadata.decode.bind(exportDataMetadata) ), - createModel: new gaxModule.LongrunningDescriptor( + createModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, createModelResponse.decode.bind(createModelResponse), createModelMetadata.decode.bind(createModelMetadata) ), - deleteModel: new gaxModule.LongrunningDescriptor( + deleteModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deleteModelResponse.decode.bind(deleteModelResponse), deleteModelMetadata.decode.bind(deleteModelMetadata) ), - deployModel: new gaxModule.LongrunningDescriptor( + deployModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deployModelResponse.decode.bind(deployModelResponse), deployModelMetadata.decode.bind(deployModelMetadata) ), - undeployModel: new gaxModule.LongrunningDescriptor( + undeployModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, undeployModelResponse.decode.bind(undeployModelResponse), undeployModelMetadata.decode.bind(undeployModelMetadata) ), - exportModel: new gaxModule.LongrunningDescriptor( + exportModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, exportModelResponse.decode.bind(exportModelResponse), exportModelMetadata.decode.bind(exportModelMetadata) @@ -312,7 +320,7 @@ export class AutoMlClient { }; // Put together the default options sent with requests. - const defaults = gaxGrpc.constructSettings( + this._defaults = this._gaxGrpc.constructSettings( 'google.cloud.automl.v1.AutoMl', gapicConfig as gax.ClientConfig, opts.clientConfig || {}, @@ -323,17 +331,35 @@ export class AutoMlClient { // 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.autoMlStub) { + return this.autoMlStub; + } // Put together the "service stub" for // google.cloud.automl.v1.AutoMl. - this.autoMlStub = gaxGrpc.createStub( - opts.fallback - ? (protos as protobuf.Root).lookupService( + this.autoMlStub = this._gaxGrpc.createStub( + this._opts.fallback + ? (this._protos as protobuf.Root).lookupService( 'google.cloud.automl.v1.AutoMl' ) : // tslint:disable-next-line no-any - (protos as any).google.cloud.automl.v1.AutoMl, - opts + (this._protos as any).google.cloud.automl.v1.AutoMl, + this._opts ) as Promise<{[method: string]: Function}>; // Iterate over each of the methods that the service provides @@ -372,9 +398,9 @@ export class AutoMlClient { } ); - 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] @@ -388,6 +414,8 @@ export class AutoMlClient { return apiCall(argument, callOptions, callback); }; } + + return this.autoMlStub; } /** @@ -509,6 +537,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getDataset(request, options, callback); } updateDataset( @@ -582,6 +611,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ 'dataset.name': request.dataset!.name || '', }); + this.initialize(); return this._innerApiCalls.updateDataset(request, options, callback); } getAnnotationSpec( @@ -654,6 +684,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getAnnotationSpec(request, options, callback); } getModel( @@ -725,6 +756,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getModel(request, options, callback); } updateModel( @@ -798,6 +830,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ 'model.name': request.model!.name || '', }); + this.initialize(); return this._innerApiCalls.updateModel(request, options, callback); } getModelEvaluation( @@ -870,6 +903,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getModelEvaluation(request, options, callback); } @@ -959,6 +993,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.createDataset(request, options, callback); } deleteDataset( @@ -989,9 +1024,9 @@ export class AutoMlClient { /** * Deletes a dataset and all of its contents. * Returns empty response in the - * [response][google.longrunning.Operation.response] field when it completes, + * {@link google.longrunning.Operation.response|response} field when it completes, * and `delete_details` in the - * [metadata][google.longrunning.Operation.metadata] field. + * {@link google.longrunning.Operation.metadata|metadata} field. * * @param {Object} request * The request object that will be sent. @@ -1049,6 +1084,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deleteDataset(request, options, callback); } importData( @@ -1082,10 +1118,10 @@ export class AutoMlClient { * * For Tables: * * A - * [schema_inference_version][google.cloud.automl.v1.InputConfig.params] + * {@link google.cloud.automl.v1.InputConfig.params|schema_inference_version} * parameter must be explicitly set. * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1147,6 +1183,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.importData(request, options, callback); } exportData( @@ -1177,7 +1214,7 @@ export class AutoMlClient { /** * Exports dataset's data to the provided output location. * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1237,6 +1274,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.exportData(request, options, callback); } createModel( @@ -1266,7 +1304,7 @@ export class AutoMlClient { ): void; /** * Creates a model. - * Returns a Model in the [response][google.longrunning.Operation.response] + * Returns a Model in the {@link google.longrunning.Operation.response|response} * field when it completes. * When you create a model, several model evaluations are created for it: * a global evaluation, and one evaluation for each annotation spec. @@ -1329,6 +1367,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.createModel(request, options, callback); } deleteModel( @@ -1359,9 +1398,9 @@ export class AutoMlClient { /** * Deletes a model. * Returns `google.protobuf.Empty` in the - * [response][google.longrunning.Operation.response] field when it completes, + * {@link google.longrunning.Operation.response|response} field when it completes, * and `delete_details` in the - * [metadata][google.longrunning.Operation.metadata] field. + * {@link google.longrunning.Operation.metadata|metadata} field. * * @param {Object} request * The request object that will be sent. @@ -1419,6 +1458,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deleteModel(request, options, callback); } deployModel( @@ -1451,14 +1491,14 @@ export class AutoMlClient { * same parameters has no effect. Deploying with different parametrs * (as e.g. changing * - * [node_number][google.cloud.automl.v1p1beta.ImageObjectDetectionModelDeploymentMetadata.node_number]) + * {@link google.cloud.automl.v1p1beta.ImageObjectDetectionModelDeploymentMetadata.node_number|node_number}) * will reset the deployment state without pausing the model's availability. * * Only applicable for Text Classification, Image Object Detection , Tables, and Image Segmentation; all other domains manage * deployment automatically. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1520,6 +1560,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deployModel(request, options, callback); } undeployModel( @@ -1554,7 +1595,7 @@ export class AutoMlClient { * all other domains manage deployment automatically. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1612,6 +1653,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.undeployModel(request, options, callback); } exportModel( @@ -1643,10 +1685,10 @@ export class AutoMlClient { * Exports a trained, "export-able", model to a user specified Google Cloud * Storage location. A model is considered export-able if and only if it has * an export format defined for it in - * [ModelExportOutputConfig][google.cloud.automl.v1.ModelExportOutputConfig]. + * {@link google.cloud.automl.v1.ModelExportOutputConfig|ModelExportOutputConfig}. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1706,6 +1748,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.exportModel(request, options, callback); } listDatasets( @@ -1748,8 +1791,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListDatasetsResponse.next_page_token][google.cloud.automl.v1.ListDatasetsResponse.next_page_token] of the previous - * [AutoMl.ListDatasets][google.cloud.automl.v1.AutoMl.ListDatasets] call. + * {@link google.cloud.automl.v1.ListDatasetsResponse.next_page_token|ListDatasetsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListDatasets|AutoMl.ListDatasets} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -1805,6 +1848,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listDatasets(request, options, callback); } @@ -1839,8 +1883,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListDatasetsResponse.next_page_token][google.cloud.automl.v1.ListDatasetsResponse.next_page_token] of the previous - * [AutoMl.ListDatasets][google.cloud.automl.v1.AutoMl.ListDatasets] call. + * {@link google.cloud.automl.v1.ListDatasetsResponse.next_page_token|ListDatasetsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListDatasets|AutoMl.ListDatasets} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -1860,6 +1904,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listDatasets.createStream( this._innerApiCalls.listDatasets as gax.GaxCall, request, @@ -1907,8 +1952,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListModelsResponse.next_page_token][google.cloud.automl.v1.ListModelsResponse.next_page_token] of the previous - * [AutoMl.ListModels][google.cloud.automl.v1.AutoMl.ListModels] call. + * {@link google.cloud.automl.v1.ListModelsResponse.next_page_token|ListModelsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListModels|AutoMl.ListModels} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -1964,6 +2009,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listModels(request, options, callback); } @@ -1999,8 +2045,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListModelsResponse.next_page_token][google.cloud.automl.v1.ListModelsResponse.next_page_token] of the previous - * [AutoMl.ListModels][google.cloud.automl.v1.AutoMl.ListModels] call. + * {@link google.cloud.automl.v1.ListModelsResponse.next_page_token|ListModelsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListModels|AutoMl.ListModels} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2020,6 +2066,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listModels.createStream( this._innerApiCalls.listModels as gax.GaxCall, request, @@ -2071,8 +2118,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained via - * [ListModelEvaluationsResponse.next_page_token][google.cloud.automl.v1.ListModelEvaluationsResponse.next_page_token] of the previous - * [AutoMl.ListModelEvaluations][google.cloud.automl.v1.AutoMl.ListModelEvaluations] call. + * {@link google.cloud.automl.v1.ListModelEvaluationsResponse.next_page_token|ListModelEvaluationsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListModelEvaluations|AutoMl.ListModelEvaluations} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2128,6 +2175,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listModelEvaluations(request, options, callback); } @@ -2167,8 +2215,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained via - * [ListModelEvaluationsResponse.next_page_token][google.cloud.automl.v1.ListModelEvaluationsResponse.next_page_token] of the previous - * [AutoMl.ListModelEvaluations][google.cloud.automl.v1.AutoMl.ListModelEvaluations] call. + * {@link google.cloud.automl.v1.ListModelEvaluationsResponse.next_page_token|ListModelEvaluationsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1.AutoMl.ListModelEvaluations|AutoMl.ListModelEvaluations} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2188,6 +2236,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listModelEvaluations.createStream( this._innerApiCalls.listModelEvaluations as gax.GaxCall, request, @@ -2489,8 +2538,9 @@ export class AutoMlClient { * The client will no longer be usable and all future behavior is undefined. */ close(): Promise { + this.initialize(); if (!this._terminated) { - return this.autoMlStub.then(stub => { + return this.autoMlStub!.then(stub => { this._terminated = true; stub.close(); }); diff --git a/packages/google-cloud-automl/src/v1/prediction_service_client.ts b/packages/google-cloud-automl/src/v1/prediction_service_client.ts index d3bcf5674dd..515ce3c2f1f 100644 --- a/packages/google-cloud-automl/src/v1/prediction_service_client.ts +++ b/packages/google-cloud-automl/src/v1/prediction_service_client.ts @@ -45,9 +45,14 @@ export class PredictionServiceClient { 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; - predictionServiceStub: Promise<{[name: string]: Function}>; + predictionServiceStub?: Promise<{[name: string]: Function}>; /** * Construct an instance of PredictionServiceClient. @@ -71,8 +76,6 @@ export class PredictionServiceClient { * 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. */ @@ -102,25 +105,28 @@ export class PredictionServiceClient { // 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 PredictionServiceClient).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}`); @@ -136,7 +142,7 @@ export class PredictionServiceClient { 'protos', 'protos.json' ); - const protos = gaxGrpc.loadProto( + this._protos = this._gaxGrpc.loadProto( opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); @@ -144,16 +150,16 @@ export class PredictionServiceClient { // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - annotationSpecPathTemplate: new gaxModule.PathTemplate( + annotationSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/annotationSpecs/{annotation_spec}' ), - datasetPathTemplate: new gaxModule.PathTemplate( + datasetPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}' ), - modelPathTemplate: new gaxModule.PathTemplate( + modelPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}' ), - modelEvaluationPathTemplate: new gaxModule.PathTemplate( + modelEvaluationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}/modelEvaluations/{model_evaluation}' ), }; @@ -162,13 +168,15 @@ export class PredictionServiceClient { // 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 batchPredictResponse = protoFilesRoot.lookup( @@ -179,7 +187,7 @@ export class PredictionServiceClient { ) as gax.protobuf.Type; this._descriptors.longrunning = { - batchPredict: new gaxModule.LongrunningDescriptor( + batchPredict: new this._gaxModule.LongrunningDescriptor( this.operationsClient, batchPredictResponse.decode.bind(batchPredictResponse), batchPredictMetadata.decode.bind(batchPredictMetadata) @@ -187,7 +195,7 @@ export class PredictionServiceClient { }; // Put together the default options sent with requests. - const defaults = gaxGrpc.constructSettings( + this._defaults = this._gaxGrpc.constructSettings( 'google.cloud.automl.v1.PredictionService', gapicConfig as gax.ClientConfig, opts.clientConfig || {}, @@ -198,17 +206,35 @@ export class PredictionServiceClient { // 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.predictionServiceStub) { + return this.predictionServiceStub; + } // Put together the "service stub" for // google.cloud.automl.v1.PredictionService. - this.predictionServiceStub = gaxGrpc.createStub( - opts.fallback - ? (protos as protobuf.Root).lookupService( + this.predictionServiceStub = this._gaxGrpc.createStub( + this._opts.fallback + ? (this._protos as protobuf.Root).lookupService( 'google.cloud.automl.v1.PredictionService' ) : // tslint:disable-next-line no-any - (protos as any).google.cloud.automl.v1.PredictionService, - opts + (this._protos as any).google.cloud.automl.v1.PredictionService, + this._opts ) as Promise<{[method: string]: Function}>; // Iterate over each of the methods that the service provides @@ -228,9 +254,9 @@ export class PredictionServiceClient { } ); - 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] @@ -244,6 +270,8 @@ export class PredictionServiceClient { return apiCall(argument, callOptions, callback); }; } + + return this.predictionServiceStub; } /** @@ -392,9 +420,9 @@ export class PredictionServiceClient { * `feature_importance` * : (boolean) Whether * - * [feature_importance][google.cloud.automl.v1.TablesModelColumnInfo.feature_importance] + * {@link google.cloud.automl.v1.TablesModelColumnInfo.feature_importance|feature_importance} * is populated in the returned list of - * [TablesAnnotation][google.cloud.automl.v1.TablesAnnotation] + * {@link google.cloud.automl.v1.TablesAnnotation|TablesAnnotation} * objects. The default is false. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. @@ -439,6 +467,7 @@ export class PredictionServiceClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.predict(request, options, callback); } @@ -468,12 +497,12 @@ export class PredictionServiceClient { > ): void; /** - * Perform a batch prediction. Unlike the online [Predict][google.cloud.automl.v1.PredictionService.Predict], batch + * Perform a batch prediction. Unlike the online {@link google.cloud.automl.v1.PredictionService.Predict|Predict}, batch * prediction result won't be immediately available in the response. Instead, * a long running operation object is returned. User can poll the operation - * result via [GetOperation][google.longrunning.Operations.GetOperation] - * method. Once the operation is done, [BatchPredictResult][google.cloud.automl.v1.BatchPredictResult] is returned in - * the [response][google.longrunning.Operation.response] field. + * result via {@link google.longrunning.Operations.GetOperation|GetOperation} + * method. Once the operation is done, {@link google.cloud.automl.v1.BatchPredictResult|BatchPredictResult} is returned in + * the {@link google.longrunning.Operation.response|response} field. * Available for following ML scenarios: * * * AutoML Vision Classification @@ -630,6 +659,7 @@ export class PredictionServiceClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.batchPredict(request, options, callback); } // -------------------- @@ -890,8 +920,9 @@ export class PredictionServiceClient { * The client will no longer be usable and all future behavior is undefined. */ close(): Promise { + this.initialize(); if (!this._terminated) { - return this.predictionServiceStub.then(stub => { + return this.predictionServiceStub!.then(stub => { this._terminated = true; stub.close(); }); diff --git a/packages/google-cloud-automl/src/v1beta1/auto_ml_client.ts b/packages/google-cloud-automl/src/v1beta1/auto_ml_client.ts index 0babf3e1909..13c6928e683 100644 --- a/packages/google-cloud-automl/src/v1beta1/auto_ml_client.ts +++ b/packages/google-cloud-automl/src/v1beta1/auto_ml_client.ts @@ -58,9 +58,14 @@ export class AutoMlClient { 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; - autoMlStub: Promise<{[name: string]: Function}>; + autoMlStub?: Promise<{[name: string]: Function}>; /** * Construct an instance of AutoMlClient. @@ -84,8 +89,6 @@ export class AutoMlClient { * 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. */ @@ -115,25 +118,28 @@ export class AutoMlClient { // 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 AutoMlClient).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}`); @@ -149,7 +155,7 @@ export class AutoMlClient { 'protos', 'protos.json' ); - const protos = gaxGrpc.loadProto( + this._protos = this._gaxGrpc.loadProto( opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); @@ -157,25 +163,25 @@ export class AutoMlClient { // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - annotationSpecPathTemplate: new gaxModule.PathTemplate( + annotationSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/annotationSpecs/{annotation_spec}' ), - columnSpecPathTemplate: new gaxModule.PathTemplate( + columnSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/tableSpecs/{table_spec}/columnSpecs/{column_spec}' ), - datasetPathTemplate: new gaxModule.PathTemplate( + datasetPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}' ), - locationPathTemplate: new gaxModule.PathTemplate( + locationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}' ), - modelPathTemplate: new gaxModule.PathTemplate( + modelPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}' ), - modelEvaluationPathTemplate: new gaxModule.PathTemplate( + modelEvaluationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}/modelEvaluations/{model_evaluation}' ), - tableSpecPathTemplate: new gaxModule.PathTemplate( + tableSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/tableSpecs/{table_spec}' ), }; @@ -184,27 +190,27 @@ export class AutoMlClient { // (e.g. 50 results at a time, with tokens to get subsequent // pages). Denote the keys used for pagination and results. this._descriptors.page = { - listDatasets: new gaxModule.PageDescriptor( + listDatasets: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'datasets' ), - listTableSpecs: new gaxModule.PageDescriptor( + listTableSpecs: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'tableSpecs' ), - listColumnSpecs: new gaxModule.PageDescriptor( + listColumnSpecs: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'columnSpecs' ), - listModels: new gaxModule.PageDescriptor( + listModels: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'model' ), - listModelEvaluations: new gaxModule.PageDescriptor( + listModelEvaluations: new this._gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'modelEvaluation' @@ -215,13 +221,15 @@ export class AutoMlClient { // 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 deleteDatasetResponse = protoFilesRoot.lookup( @@ -280,47 +288,47 @@ export class AutoMlClient { ) as gax.protobuf.Type; this._descriptors.longrunning = { - deleteDataset: new gaxModule.LongrunningDescriptor( + deleteDataset: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deleteDatasetResponse.decode.bind(deleteDatasetResponse), deleteDatasetMetadata.decode.bind(deleteDatasetMetadata) ), - importData: new gaxModule.LongrunningDescriptor( + importData: new this._gaxModule.LongrunningDescriptor( this.operationsClient, importDataResponse.decode.bind(importDataResponse), importDataMetadata.decode.bind(importDataMetadata) ), - exportData: new gaxModule.LongrunningDescriptor( + exportData: new this._gaxModule.LongrunningDescriptor( this.operationsClient, exportDataResponse.decode.bind(exportDataResponse), exportDataMetadata.decode.bind(exportDataMetadata) ), - createModel: new gaxModule.LongrunningDescriptor( + createModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, createModelResponse.decode.bind(createModelResponse), createModelMetadata.decode.bind(createModelMetadata) ), - deleteModel: new gaxModule.LongrunningDescriptor( + deleteModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deleteModelResponse.decode.bind(deleteModelResponse), deleteModelMetadata.decode.bind(deleteModelMetadata) ), - deployModel: new gaxModule.LongrunningDescriptor( + deployModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, deployModelResponse.decode.bind(deployModelResponse), deployModelMetadata.decode.bind(deployModelMetadata) ), - undeployModel: new gaxModule.LongrunningDescriptor( + undeployModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, undeployModelResponse.decode.bind(undeployModelResponse), undeployModelMetadata.decode.bind(undeployModelMetadata) ), - exportModel: new gaxModule.LongrunningDescriptor( + exportModel: new this._gaxModule.LongrunningDescriptor( this.operationsClient, exportModelResponse.decode.bind(exportModelResponse), exportModelMetadata.decode.bind(exportModelMetadata) ), - exportEvaluatedExamples: new gaxModule.LongrunningDescriptor( + exportEvaluatedExamples: new this._gaxModule.LongrunningDescriptor( this.operationsClient, exportEvaluatedExamplesResponse.decode.bind( exportEvaluatedExamplesResponse @@ -332,7 +340,7 @@ export class AutoMlClient { }; // Put together the default options sent with requests. - const defaults = gaxGrpc.constructSettings( + this._defaults = this._gaxGrpc.constructSettings( 'google.cloud.automl.v1beta1.AutoMl', gapicConfig as gax.ClientConfig, opts.clientConfig || {}, @@ -343,17 +351,35 @@ export class AutoMlClient { // 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.autoMlStub) { + return this.autoMlStub; + } // Put together the "service stub" for // google.cloud.automl.v1beta1.AutoMl. - this.autoMlStub = gaxGrpc.createStub( - opts.fallback - ? (protos as protobuf.Root).lookupService( + this.autoMlStub = this._gaxGrpc.createStub( + this._opts.fallback + ? (this._protos as protobuf.Root).lookupService( 'google.cloud.automl.v1beta1.AutoMl' ) : // tslint:disable-next-line no-any - (protos as any).google.cloud.automl.v1beta1.AutoMl, - opts + (this._protos as any).google.cloud.automl.v1beta1.AutoMl, + this._opts ) as Promise<{[method: string]: Function}>; // Iterate over each of the methods that the service provides @@ -398,9 +424,9 @@ export class AutoMlClient { } ); - 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] @@ -414,6 +440,8 @@ export class AutoMlClient { return apiCall(argument, callOptions, callback); }; } + + return this.autoMlStub; } /** @@ -538,6 +566,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.createDataset(request, options, callback); } getDataset( @@ -610,6 +639,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getDataset(request, options, callback); } updateDataset( @@ -684,6 +714,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ 'dataset.name': request.dataset!.name || '', }); + this.initialize(); return this._innerApiCalls.updateDataset(request, options, callback); } getAnnotationSpec( @@ -764,6 +795,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getAnnotationSpec(request, options, callback); } getTableSpec( @@ -838,6 +870,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getTableSpec(request, options, callback); } updateTableSpec( @@ -920,6 +953,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ 'table_spec.name': request.tableSpec!.name || '', }); + this.initialize(); return this._innerApiCalls.updateTableSpec(request, options, callback); } getColumnSpec( @@ -994,6 +1028,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getColumnSpec(request, options, callback); } updateColumnSpec( @@ -1076,6 +1111,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ 'column_spec.name': request.columnSpec!.name || '', }); + this.initialize(); return this._innerApiCalls.updateColumnSpec(request, options, callback); } getModel( @@ -1147,6 +1183,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getModel(request, options, callback); } getModelEvaluation( @@ -1227,6 +1264,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.getModelEvaluation(request, options, callback); } @@ -1258,9 +1296,9 @@ export class AutoMlClient { /** * Deletes a dataset and all of its contents. * Returns empty response in the - * [response][google.longrunning.Operation.response] field when it completes, + * {@link google.longrunning.Operation.response|response} field when it completes, * and `delete_details` in the - * [metadata][google.longrunning.Operation.metadata] field. + * {@link google.longrunning.Operation.metadata|metadata} field. * * @param {Object} request * The request object that will be sent. @@ -1318,6 +1356,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deleteDataset(request, options, callback); } importData( @@ -1351,10 +1390,10 @@ export class AutoMlClient { * * For Tables: * * A - * [schema_inference_version][google.cloud.automl.v1beta1.InputConfig.params] + * {@link google.cloud.automl.v1beta1.InputConfig.params|schema_inference_version} * parameter must be explicitly set. * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1416,6 +1455,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.importData(request, options, callback); } exportData( @@ -1446,7 +1486,7 @@ export class AutoMlClient { /** * Exports dataset's data to the provided output location. * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1506,6 +1546,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.exportData(request, options, callback); } createModel( @@ -1535,7 +1576,7 @@ export class AutoMlClient { ): void; /** * Creates a model. - * Returns a Model in the [response][google.longrunning.Operation.response] + * Returns a Model in the {@link google.longrunning.Operation.response|response} * field when it completes. * When you create a model, several model evaluations are created for it: * a global evaluation, and one evaluation for each annotation spec. @@ -1598,6 +1639,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.createModel(request, options, callback); } deleteModel( @@ -1628,9 +1670,9 @@ export class AutoMlClient { /** * Deletes a model. * Returns `google.protobuf.Empty` in the - * [response][google.longrunning.Operation.response] field when it completes, + * {@link google.longrunning.Operation.response|response} field when it completes, * and `delete_details` in the - * [metadata][google.longrunning.Operation.metadata] field. + * {@link google.longrunning.Operation.metadata|metadata} field. * * @param {Object} request * The request object that will be sent. @@ -1688,6 +1730,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deleteModel(request, options, callback); } deployModel( @@ -1720,14 +1763,14 @@ export class AutoMlClient { * same parameters has no effect. Deploying with different parametrs * (as e.g. changing * - * [node_number][google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata.node_number]) + * {@link google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata.node_number|node_number}) * will reset the deployment state without pausing the model's availability. * * Only applicable for Text Classification, Image Object Detection , Tables, and Image Segmentation; all other domains manage * deployment automatically. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1789,6 +1832,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.deployModel(request, options, callback); } undeployModel( @@ -1823,7 +1867,7 @@ export class AutoMlClient { * all other domains manage deployment automatically. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1881,6 +1925,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.undeployModel(request, options, callback); } exportModel( @@ -1913,10 +1958,10 @@ export class AutoMlClient { * Storage location. A model is considered export-able if and only if it has * an export format defined for it in * - * [ModelExportOutputConfig][google.cloud.automl.v1beta1.ModelExportOutputConfig]. + * {@link google.cloud.automl.v1beta1.ModelExportOutputConfig|ModelExportOutputConfig}. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -1976,6 +2021,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.exportModel(request, options, callback); } exportEvaluatedExamples( @@ -2017,7 +2063,7 @@ export class AutoMlClient { * Currently only available for Tables. * * Returns an empty response in the - * [response][google.longrunning.Operation.response] field when it completes. + * {@link google.longrunning.Operation.response|response} field when it completes. * * @param {Object} request * The request object that will be sent. @@ -2078,6 +2124,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.exportEvaluatedExamples( request, options, @@ -2124,8 +2171,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListDatasetsResponse.next_page_token][google.cloud.automl.v1beta1.ListDatasetsResponse.next_page_token] of the previous - * [AutoMl.ListDatasets][google.cloud.automl.v1beta1.AutoMl.ListDatasets] call. + * {@link google.cloud.automl.v1beta1.ListDatasetsResponse.next_page_token|ListDatasetsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListDatasets|AutoMl.ListDatasets} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2181,6 +2228,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listDatasets(request, options, callback); } @@ -2215,8 +2263,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListDatasetsResponse.next_page_token][google.cloud.automl.v1beta1.ListDatasetsResponse.next_page_token] of the previous - * [AutoMl.ListDatasets][google.cloud.automl.v1beta1.AutoMl.ListDatasets] call. + * {@link google.cloud.automl.v1beta1.ListDatasetsResponse.next_page_token|ListDatasetsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListDatasets|AutoMl.ListDatasets} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2236,6 +2284,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listDatasets.createStream( this._innerApiCalls.listDatasets as gax.GaxCall, request, @@ -2278,8 +2327,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained from the - * [ListTableSpecsResponse.next_page_token][google.cloud.automl.v1beta1.ListTableSpecsResponse.next_page_token] field of the previous - * [AutoMl.ListTableSpecs][google.cloud.automl.v1beta1.AutoMl.ListTableSpecs] call. + * {@link google.cloud.automl.v1beta1.ListTableSpecsResponse.next_page_token|ListTableSpecsResponse.next_page_token} field of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListTableSpecs|AutoMl.ListTableSpecs} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2335,6 +2384,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listTableSpecs(request, options, callback); } @@ -2365,8 +2415,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained from the - * [ListTableSpecsResponse.next_page_token][google.cloud.automl.v1beta1.ListTableSpecsResponse.next_page_token] field of the previous - * [AutoMl.ListTableSpecs][google.cloud.automl.v1beta1.AutoMl.ListTableSpecs] call. + * {@link google.cloud.automl.v1beta1.ListTableSpecsResponse.next_page_token|ListTableSpecsResponse.next_page_token} field of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListTableSpecs|AutoMl.ListTableSpecs} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2386,6 +2436,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listTableSpecs.createStream( this._innerApiCalls.listTableSpecs as gax.GaxCall, request, @@ -2428,8 +2479,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained from the - * [ListColumnSpecsResponse.next_page_token][google.cloud.automl.v1beta1.ListColumnSpecsResponse.next_page_token] field of the previous - * [AutoMl.ListColumnSpecs][google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs] call. + * {@link google.cloud.automl.v1beta1.ListColumnSpecsResponse.next_page_token|ListColumnSpecsResponse.next_page_token} field of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs|AutoMl.ListColumnSpecs} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2485,6 +2536,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listColumnSpecs(request, options, callback); } @@ -2515,8 +2567,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained from the - * [ListColumnSpecsResponse.next_page_token][google.cloud.automl.v1beta1.ListColumnSpecsResponse.next_page_token] field of the previous - * [AutoMl.ListColumnSpecs][google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs] call. + * {@link google.cloud.automl.v1beta1.ListColumnSpecsResponse.next_page_token|ListColumnSpecsResponse.next_page_token} field of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListColumnSpecs|AutoMl.ListColumnSpecs} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2536,6 +2588,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listColumnSpecs.createStream( this._innerApiCalls.listColumnSpecs as gax.GaxCall, request, @@ -2583,8 +2636,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListModelsResponse.next_page_token][google.cloud.automl.v1beta1.ListModelsResponse.next_page_token] of the previous - * [AutoMl.ListModels][google.cloud.automl.v1beta1.AutoMl.ListModels] call. + * {@link google.cloud.automl.v1beta1.ListModelsResponse.next_page_token|ListModelsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListModels|AutoMl.ListModels} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2640,6 +2693,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listModels(request, options, callback); } @@ -2675,8 +2729,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return * Typically obtained via - * [ListModelsResponse.next_page_token][google.cloud.automl.v1beta1.ListModelsResponse.next_page_token] of the previous - * [AutoMl.ListModels][google.cloud.automl.v1beta1.AutoMl.ListModels] call. + * {@link google.cloud.automl.v1beta1.ListModelsResponse.next_page_token|ListModelsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListModels|AutoMl.ListModels} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2696,6 +2750,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listModels.createStream( this._innerApiCalls.listModels as gax.GaxCall, request, @@ -2747,8 +2802,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained via - * [ListModelEvaluationsResponse.next_page_token][google.cloud.automl.v1beta1.ListModelEvaluationsResponse.next_page_token] of the previous - * [AutoMl.ListModelEvaluations][google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations] call. + * {@link google.cloud.automl.v1beta1.ListModelEvaluationsResponse.next_page_token|ListModelEvaluationsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations|AutoMl.ListModelEvaluations} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Promise} - The promise which resolves to an array. @@ -2804,6 +2859,7 @@ export class AutoMlClient { ] = gax.routingHeader.fromParams({ parent: request.parent || '', }); + this.initialize(); return this._innerApiCalls.listModelEvaluations(request, options, callback); } @@ -2843,8 +2899,8 @@ export class AutoMlClient { * @param {string} request.pageToken * A token identifying a page of results for the server to return. * Typically obtained via - * [ListModelEvaluationsResponse.next_page_token][google.cloud.automl.v1beta1.ListModelEvaluationsResponse.next_page_token] of the previous - * [AutoMl.ListModelEvaluations][google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations] call. + * {@link google.cloud.automl.v1beta1.ListModelEvaluationsResponse.next_page_token|ListModelEvaluationsResponse.next_page_token} of the previous + * {@link google.cloud.automl.v1beta1.AutoMl.ListModelEvaluations|AutoMl.ListModelEvaluations} call. * @param {object} [options] * Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details. * @returns {Stream} @@ -2864,6 +2920,7 @@ export class AutoMlClient { parent: request.parent || '', }); const callSettings = new gax.CallSettings(options); + this.initialize(); return this._descriptors.page.listModelEvaluations.createStream( this._innerApiCalls.listModelEvaluations as gax.GaxCall, request, @@ -3322,8 +3379,9 @@ export class AutoMlClient { * The client will no longer be usable and all future behavior is undefined. */ close(): Promise { + this.initialize(); if (!this._terminated) { - return this.autoMlStub.then(stub => { + return this.autoMlStub!.then(stub => { this._terminated = true; stub.close(); }); diff --git a/packages/google-cloud-automl/src/v1beta1/prediction_service_client.ts b/packages/google-cloud-automl/src/v1beta1/prediction_service_client.ts index ef590a33afe..500f2075513 100644 --- a/packages/google-cloud-automl/src/v1beta1/prediction_service_client.ts +++ b/packages/google-cloud-automl/src/v1beta1/prediction_service_client.ts @@ -45,9 +45,14 @@ export class PredictionServiceClient { 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; - predictionServiceStub: Promise<{[name: string]: Function}>; + predictionServiceStub?: Promise<{[name: string]: Function}>; /** * Construct an instance of PredictionServiceClient. @@ -71,8 +76,6 @@ export class PredictionServiceClient { * 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. */ @@ -102,25 +105,28 @@ export class PredictionServiceClient { // 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 PredictionServiceClient).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}`); @@ -136,7 +142,7 @@ export class PredictionServiceClient { 'protos', 'protos.json' ); - const protos = gaxGrpc.loadProto( + this._protos = this._gaxGrpc.loadProto( opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); @@ -144,22 +150,22 @@ export class PredictionServiceClient { // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - annotationSpecPathTemplate: new gaxModule.PathTemplate( + annotationSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/annotationSpecs/{annotation_spec}' ), - columnSpecPathTemplate: new gaxModule.PathTemplate( + columnSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/tableSpecs/{table_spec}/columnSpecs/{column_spec}' ), - datasetPathTemplate: new gaxModule.PathTemplate( + datasetPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}' ), - modelPathTemplate: new gaxModule.PathTemplate( + modelPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}' ), - modelEvaluationPathTemplate: new gaxModule.PathTemplate( + modelEvaluationPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/models/{model}/modelEvaluations/{model_evaluation}' ), - tableSpecPathTemplate: new gaxModule.PathTemplate( + tableSpecPathTemplate: new this._gaxModule.PathTemplate( 'projects/{project}/locations/{location}/datasets/{dataset}/tableSpecs/{table_spec}' ), }; @@ -168,13 +174,15 @@ export class PredictionServiceClient { // 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 batchPredictResponse = protoFilesRoot.lookup( @@ -185,7 +193,7 @@ export class PredictionServiceClient { ) as gax.protobuf.Type; this._descriptors.longrunning = { - batchPredict: new gaxModule.LongrunningDescriptor( + batchPredict: new this._gaxModule.LongrunningDescriptor( this.operationsClient, batchPredictResponse.decode.bind(batchPredictResponse), batchPredictMetadata.decode.bind(batchPredictMetadata) @@ -193,7 +201,7 @@ export class PredictionServiceClient { }; // Put together the default options sent with requests. - const defaults = gaxGrpc.constructSettings( + this._defaults = this._gaxGrpc.constructSettings( 'google.cloud.automl.v1beta1.PredictionService', gapicConfig as gax.ClientConfig, opts.clientConfig || {}, @@ -204,17 +212,35 @@ export class PredictionServiceClient { // 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.predictionServiceStub) { + return this.predictionServiceStub; + } // Put together the "service stub" for // google.cloud.automl.v1beta1.PredictionService. - this.predictionServiceStub = gaxGrpc.createStub( - opts.fallback - ? (protos as protobuf.Root).lookupService( + this.predictionServiceStub = this._gaxGrpc.createStub( + this._opts.fallback + ? (this._protos as protobuf.Root).lookupService( 'google.cloud.automl.v1beta1.PredictionService' ) : // tslint:disable-next-line no-any - (protos as any).google.cloud.automl.v1beta1.PredictionService, - opts + (this._protos as any).google.cloud.automl.v1beta1.PredictionService, + this._opts ) as Promise<{[method: string]: Function}>; // Iterate over each of the methods that the service provides @@ -234,9 +260,9 @@ export class PredictionServiceClient { } ); - 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] @@ -250,6 +276,8 @@ export class PredictionServiceClient { return apiCall(argument, callOptions, callback); }; } + + return this.predictionServiceStub; } /** @@ -338,7 +366,7 @@ export class PredictionServiceClient { * * Tables - Row, with column values matching the columns of the model, * up to 5MB. Not available for FORECASTING * - * [prediction_type][google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type]. + * {@link google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type|prediction_type}. * * Text Sentiment - TextSnippet, content up 500 characters, UTF-8 * encoded. * @@ -413,6 +441,7 @@ export class PredictionServiceClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.predict(request, options, callback); } @@ -442,12 +471,12 @@ export class PredictionServiceClient { > ): void; /** - * Perform a batch prediction. Unlike the online [Predict][google.cloud.automl.v1beta1.PredictionService.Predict], batch + * Perform a batch prediction. Unlike the online {@link google.cloud.automl.v1beta1.PredictionService.Predict|Predict}, batch * prediction result won't be immediately available in the response. Instead, * a long running operation object is returned. User can poll the operation - * result via [GetOperation][google.longrunning.Operations.GetOperation] - * method. Once the operation is done, [BatchPredictResult][google.cloud.automl.v1beta1.BatchPredictResult] is returned in - * the [response][google.longrunning.Operation.response] field. + * result via {@link google.longrunning.Operations.GetOperation|GetOperation} + * method. Once the operation is done, {@link google.cloud.automl.v1beta1.BatchPredictResult|BatchPredictResult} is returned in + * the {@link google.longrunning.Operation.response|response} field. * Available for following ML problems: * * Image Classification * * Image Object Detection @@ -587,6 +616,7 @@ export class PredictionServiceClient { ] = gax.routingHeader.fromParams({ name: request.name || '', }); + this.initialize(); return this._innerApiCalls.batchPredict(request, options, callback); } // -------------------- @@ -1004,8 +1034,9 @@ export class PredictionServiceClient { * The client will no longer be usable and all future behavior is undefined. */ close(): Promise { + this.initialize(); if (!this._terminated) { - return this.predictionServiceStub.then(stub => { + return this.predictionServiceStub!.then(stub => { this._terminated = true; stub.close(); }); diff --git a/packages/google-cloud-automl/synth.metadata b/packages/google-cloud-automl/synth.metadata index 05e0d034c5a..8262e149b52 100644 --- a/packages/google-cloud-automl/synth.metadata +++ b/packages/google-cloud-automl/synth.metadata @@ -1,5 +1,5 @@ { - "updateTime": "2020-03-05T22:40:22.969554Z", + "updateTime": "2020-03-06T12:13:29.324535Z", "sources": [ { "git": { diff --git a/packages/google-cloud-automl/test/gapic-auto_ml-v1.ts b/packages/google-cloud-automl/test/gapic-auto_ml-v1.ts index d4cdcb59105..a583b0bc13f 100644 --- a/packages/google-cloud-automl/test/gapic-auto_ml-v1.ts +++ b/packages/google-cloud-automl/test/gapic-auto_ml-v1.ts @@ -102,12 +102,30 @@ describe('v1.AutoMlClient', () => { }); assert(client); }); + it('has initialize method and supports deferred initialization', async () => { + const client = new automlModule.v1.AutoMlClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.autoMlStub, undefined); + await client.initialize(); + assert(client.autoMlStub); + }); + it('has close method', () => { + const client = new automlModule.v1.AutoMlClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); describe('getDataset', () => { it('invokes getDataset without error', done => { const client = new automlModule.v1.AutoMlClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetDatasetRequest = {}; request.name = ''; @@ -131,6 +149,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetDatasetRequest = {}; request.name = ''; @@ -156,6 +176,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUpdateDatasetRequest = {}; request.dataset = {}; @@ -180,6 +202,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUpdateDatasetRequest = {}; request.dataset = {}; @@ -206,6 +230,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetAnnotationSpecRequest = {}; request.name = ''; @@ -229,6 +255,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetAnnotationSpecRequest = {}; request.name = ''; @@ -254,6 +282,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetModelRequest = {}; request.name = ''; @@ -277,6 +307,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetModelRequest = {}; request.name = ''; @@ -302,6 +334,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUpdateModelRequest = {}; request.model = {}; @@ -326,6 +360,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUpdateModelRequest = {}; request.model = {}; @@ -352,6 +388,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetModelEvaluationRequest = {}; request.name = ''; @@ -375,6 +413,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IGetModelEvaluationRequest = {}; request.name = ''; @@ -400,6 +440,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.ICreateDatasetRequest = {}; request.parent = ''; @@ -430,6 +472,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.ICreateDatasetRequest = {}; request.parent = ''; @@ -463,6 +507,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeleteDatasetRequest = {}; request.name = ''; @@ -493,6 +539,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeleteDatasetRequest = {}; request.name = ''; @@ -526,6 +574,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IImportDataRequest = {}; request.name = ''; @@ -556,6 +606,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IImportDataRequest = {}; request.name = ''; @@ -589,6 +641,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IExportDataRequest = {}; request.name = ''; @@ -619,6 +673,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IExportDataRequest = {}; request.name = ''; @@ -652,6 +708,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.ICreateModelRequest = {}; request.parent = ''; @@ -682,6 +740,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.ICreateModelRequest = {}; request.parent = ''; @@ -715,6 +775,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeleteModelRequest = {}; request.name = ''; @@ -745,6 +807,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeleteModelRequest = {}; request.name = ''; @@ -778,6 +842,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeployModelRequest = {}; request.name = ''; @@ -808,6 +874,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IDeployModelRequest = {}; request.name = ''; @@ -841,6 +909,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUndeployModelRequest = {}; request.name = ''; @@ -871,6 +941,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IUndeployModelRequest = {}; request.name = ''; @@ -904,6 +976,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IExportModelRequest = {}; request.name = ''; @@ -934,6 +1008,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IExportModelRequest = {}; request.name = ''; @@ -967,6 +1043,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListDatasetsRequest = {}; request.parent = ''; @@ -994,6 +1072,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListDatasetsRequest = {}; request.parent = ''; @@ -1026,6 +1106,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListModelsRequest = {}; request.parent = ''; @@ -1053,6 +1135,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListModelsRequest = {}; request.parent = ''; @@ -1085,6 +1169,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListModelEvaluationsRequest = {}; request.parent = ''; @@ -1112,6 +1198,8 @@ describe('v1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IListModelEvaluationsRequest = {}; request.parent = ''; diff --git a/packages/google-cloud-automl/test/gapic-auto_ml-v1beta1.ts b/packages/google-cloud-automl/test/gapic-auto_ml-v1beta1.ts index cd3bc2f0c64..688706cf54e 100644 --- a/packages/google-cloud-automl/test/gapic-auto_ml-v1beta1.ts +++ b/packages/google-cloud-automl/test/gapic-auto_ml-v1beta1.ts @@ -102,12 +102,30 @@ describe('v1beta1.AutoMlClient', () => { }); assert(client); }); + it('has initialize method and supports deferred initialization', async () => { + const client = new automlModule.v1beta1.AutoMlClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.autoMlStub, undefined); + await client.initialize(); + assert(client.autoMlStub); + }); + it('has close method', () => { + const client = new automlModule.v1beta1.AutoMlClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); describe('createDataset', () => { it('invokes createDataset without error', done => { const client = new automlModule.v1beta1.AutoMlClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.ICreateDatasetRequest = {}; request.parent = ''; @@ -131,6 +149,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.ICreateDatasetRequest = {}; request.parent = ''; @@ -156,6 +176,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetDatasetRequest = {}; request.name = ''; @@ -179,6 +201,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetDatasetRequest = {}; request.name = ''; @@ -204,6 +228,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateDatasetRequest = {}; request.dataset = {}; @@ -228,6 +254,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateDatasetRequest = {}; request.dataset = {}; @@ -254,6 +282,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetAnnotationSpecRequest = {}; request.name = ''; @@ -277,6 +307,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetAnnotationSpecRequest = {}; request.name = ''; @@ -302,6 +334,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetTableSpecRequest = {}; request.name = ''; @@ -325,6 +359,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetTableSpecRequest = {}; request.name = ''; @@ -350,6 +386,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateTableSpecRequest = {}; request.tableSpec = {}; @@ -374,6 +412,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateTableSpecRequest = {}; request.tableSpec = {}; @@ -400,6 +440,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetColumnSpecRequest = {}; request.name = ''; @@ -423,6 +465,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetColumnSpecRequest = {}; request.name = ''; @@ -448,6 +492,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateColumnSpecRequest = {}; request.columnSpec = {}; @@ -472,6 +518,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUpdateColumnSpecRequest = {}; request.columnSpec = {}; @@ -498,6 +546,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetModelRequest = {}; request.name = ''; @@ -521,6 +571,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetModelRequest = {}; request.name = ''; @@ -546,6 +598,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetModelEvaluationRequest = {}; request.name = ''; @@ -569,6 +623,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IGetModelEvaluationRequest = {}; request.name = ''; @@ -594,6 +650,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeleteDatasetRequest = {}; request.name = ''; @@ -624,6 +682,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeleteDatasetRequest = {}; request.name = ''; @@ -657,6 +717,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IImportDataRequest = {}; request.name = ''; @@ -687,6 +749,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IImportDataRequest = {}; request.name = ''; @@ -720,6 +784,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportDataRequest = {}; request.name = ''; @@ -750,6 +816,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportDataRequest = {}; request.name = ''; @@ -783,6 +851,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.ICreateModelRequest = {}; request.parent = ''; @@ -813,6 +883,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.ICreateModelRequest = {}; request.parent = ''; @@ -846,6 +918,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeleteModelRequest = {}; request.name = ''; @@ -876,6 +950,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeleteModelRequest = {}; request.name = ''; @@ -909,6 +985,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeployModelRequest = {}; request.name = ''; @@ -939,6 +1017,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IDeployModelRequest = {}; request.name = ''; @@ -972,6 +1052,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUndeployModelRequest = {}; request.name = ''; @@ -1002,6 +1084,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IUndeployModelRequest = {}; request.name = ''; @@ -1035,6 +1119,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportModelRequest = {}; request.name = ''; @@ -1065,6 +1151,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportModelRequest = {}; request.name = ''; @@ -1098,6 +1186,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportEvaluatedExamplesRequest = {}; request.name = ''; @@ -1128,6 +1218,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IExportEvaluatedExamplesRequest = {}; request.name = ''; @@ -1161,6 +1253,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListDatasetsRequest = {}; request.parent = ''; @@ -1188,6 +1282,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListDatasetsRequest = {}; request.parent = ''; @@ -1220,6 +1316,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListTableSpecsRequest = {}; request.parent = ''; @@ -1247,6 +1345,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListTableSpecsRequest = {}; request.parent = ''; @@ -1279,6 +1379,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListColumnSpecsRequest = {}; request.parent = ''; @@ -1306,6 +1408,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListColumnSpecsRequest = {}; request.parent = ''; @@ -1338,6 +1442,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListModelsRequest = {}; request.parent = ''; @@ -1365,6 +1471,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListModelsRequest = {}; request.parent = ''; @@ -1397,6 +1505,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListModelEvaluationsRequest = {}; request.parent = ''; @@ -1424,6 +1534,8 @@ describe('v1beta1.AutoMlClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IListModelEvaluationsRequest = {}; request.parent = ''; diff --git a/packages/google-cloud-automl/test/gapic-prediction_service-v1.ts b/packages/google-cloud-automl/test/gapic-prediction_service-v1.ts index 0db8ea518f7..ec477267a55 100644 --- a/packages/google-cloud-automl/test/gapic-prediction_service-v1.ts +++ b/packages/google-cloud-automl/test/gapic-prediction_service-v1.ts @@ -104,12 +104,30 @@ describe('v1.PredictionServiceClient', () => { }); assert(client); }); + it('has initialize method and supports deferred initialization', async () => { + const client = new predictionserviceModule.v1.PredictionServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.predictionServiceStub, undefined); + await client.initialize(); + assert(client.predictionServiceStub); + }); + it('has close method', () => { + const client = new predictionserviceModule.v1.PredictionServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); describe('predict', () => { it('invokes predict without error', done => { const client = new predictionserviceModule.v1.PredictionServiceClient({ credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IPredictRequest = {}; request.name = ''; @@ -133,6 +151,8 @@ describe('v1.PredictionServiceClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IPredictRequest = {}; request.name = ''; @@ -158,6 +178,8 @@ describe('v1.PredictionServiceClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IBatchPredictRequest = {}; request.name = ''; @@ -188,6 +210,8 @@ describe('v1.PredictionServiceClient', () => { credentials: {client_email: 'bogus', private_key: 'bogus'}, projectId: 'bogus', }); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1.IBatchPredictRequest = {}; request.name = ''; diff --git a/packages/google-cloud-automl/test/gapic-prediction_service-v1beta1.ts b/packages/google-cloud-automl/test/gapic-prediction_service-v1beta1.ts index 7668630686d..bdc58f12fa4 100644 --- a/packages/google-cloud-automl/test/gapic-prediction_service-v1beta1.ts +++ b/packages/google-cloud-automl/test/gapic-prediction_service-v1beta1.ts @@ -104,6 +104,22 @@ describe('v1beta1.PredictionServiceClient', () => { }); assert(client); }); + it('has initialize method and supports deferred initialization', async () => { + const client = new predictionserviceModule.v1beta1.PredictionServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + assert.strictEqual(client.predictionServiceStub, undefined); + await client.initialize(); + assert(client.predictionServiceStub); + }); + it('has close method', () => { + const client = new predictionserviceModule.v1beta1.PredictionServiceClient({ + credentials: {client_email: 'bogus', private_key: 'bogus'}, + projectId: 'bogus', + }); + client.close(); + }); describe('predict', () => { it('invokes predict without error', done => { const client = new predictionserviceModule.v1beta1.PredictionServiceClient( @@ -112,6 +128,8 @@ describe('v1beta1.PredictionServiceClient', () => { projectId: 'bogus', } ); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IPredictRequest = {}; request.name = ''; @@ -137,6 +155,8 @@ describe('v1beta1.PredictionServiceClient', () => { projectId: 'bogus', } ); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IPredictRequest = {}; request.name = ''; @@ -164,6 +184,8 @@ describe('v1beta1.PredictionServiceClient', () => { projectId: 'bogus', } ); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IBatchPredictRequest = {}; request.name = ''; @@ -196,6 +218,8 @@ describe('v1beta1.PredictionServiceClient', () => { projectId: 'bogus', } ); + // Initialize client before mocking + client.initialize(); // Mock request const request: protosTypes.google.cloud.automl.v1beta1.IBatchPredictRequest = {}; request.name = '';