1 4
// Copyright 2020 Google LLC
2 4
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License");
4 4
// you may not use this file except in compliance with the License.
5 4
// You may obtain a copy of the License at
6 4
//
7 4
//     https://www.apache.org/licenses/LICENSE-2.0
8 4
//
9 4
// Unless required by applicable law or agreed to in writing, software
10 4
// distributed under the License is distributed on an "AS IS" BASIS,
11 4
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 4
// See the License for the specific language governing permissions and
13 4
// limitations under the License.
14 4
//
15 4
// ** This file is automatically generated by gapic-generator-typescript. **
16 4
// ** https://github.com/googleapis/gapic-generator-typescript **
17 4
// ** All changes to this file may be overwritten. **
18 4

19 4
import * as gax from 'google-gax';
20 4
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
21 4
import * as path from 'path';
22 4

23 4
import * as protos from '../../protos/protos';
24 4
import * as gapicConfig from './image_annotator_client_config.json';
25 4

26 4
const version = require('../../../package.json').version;
27 4

28 4
/**
29 4
 *  Service that performs Google Cloud Vision API detection tasks over client
30 4
 *  images, such as face, landmark, logo, label, and text detection. The
31 4
 *  ImageAnnotator service returns detected entities from the images.
32 4
 * @class
33 4
 * @memberof v1p1beta1
34 4
 */
35 4
export class ImageAnnotatorClient {
36 4
  private _terminated = false;
37 4
  private _opts: ClientOptions;
38 4
  private _gaxModule: typeof gax | typeof gax.fallback;
39 4
  private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
40 4
  private _protos: {};
41 4
  private _defaults: {[method: string]: gax.CallSettings};
42 4
  auth: gax.GoogleAuth;
43 4
  descriptors: Descriptors = {
44 4
    page: {},
45 4
    stream: {},
46 4
    longrunning: {},
47 4
    batching: {},
48 4
  };
49 4
  innerApiCalls: {[name: string]: Function};
50 4
  imageAnnotatorStub?: Promise<{[name: string]: Function}>;
51 4

52 4
  /**
53 4
   * Construct an instance of ImageAnnotatorClient.
54 4
   *
55 4
   * @param {object} [options] - The configuration object. See the subsequent
56 4
   *   parameters for more details.
57 4
   * @param {object} [options.credentials] - Credentials object.
58 4
   * @param {string} [options.credentials.client_email]
59 4
   * @param {string} [options.credentials.private_key]
60 4
   * @param {string} [options.email] - Account email address. Required when
61 4
   *     using a .pem or .p12 keyFilename.
62 4
   * @param {string} [options.keyFilename] - Full path to the a .json, .pem, or
63 4
   *     .p12 key downloaded from the Google Developers Console. If you provide
64 4
   *     a path to a JSON file, the projectId option below is not necessary.
65 4
   *     NOTE: .pem and .p12 require you to specify options.email as well.
66 4
   * @param {number} [options.port] - The port on which to connect to
67 4
   *     the remote host.
68 4
   * @param {string} [options.projectId] - The project ID from the Google
69 4
   *     Developer's Console, e.g. 'grape-spaceship-123'. We will also check
70 4
   *     the environment variable GCLOUD_PROJECT for your project ID. If your
71 4
   *     app is running in an environment which supports
72 4
   *     {@link https://developers.google.com/identity/protocols/application-default-credentials Application Default Credentials},
73 4
   *     your project ID will be detected automatically.
74 4
   * @param {string} [options.apiEndpoint] - The domain name of the
75 4
   *     API remote host.
76 4
   */
77 4

78 4
  constructor(opts?: ClientOptions) {
79 4
    // Ensure that options include the service address and port.
80 4
    const staticMembers = this.constructor as typeof ImageAnnotatorClient;
81 4
    const servicePath =
82 4
      opts && opts.servicePath
83 4
        ? opts.servicePath
84 4
        : opts && opts.apiEndpoint
85 4
        ? opts.apiEndpoint
86 4
        : staticMembers.servicePath;
87 4
    const port = opts && opts.port ? opts.port : staticMembers.port;
88 4

89 4
    if (!opts) {
90 4
      opts = {servicePath, port};
91 4
    }
92 4
    opts.servicePath = opts.servicePath || servicePath;
93 4
    opts.port = opts.port || port;
94 4

95 4
    // users can override the config from client side, like retry codes name.
96 4
    // The detailed structure of the clientConfig can be found here: https://github.com/googleapis/gax-nodejs/blob/master/src/gax.ts#L546
97 4
    // The way to override client config for Showcase API:
98 4
    //
99 4
    // const customConfig = {"interfaces": {"google.showcase.v1beta1.Echo": {"methods": {"Echo": {"retry_codes_name": "idempotent", "retry_params_name": "default"}}}}}
100 4
    // const showcaseClient = new showcaseClient({ projectId, customConfig });
101 4
    opts.clientConfig = opts.clientConfig || {};
102 4

103 4
    // If we're running in browser, it's OK to omit `fallback` since
104 4
    // google-gax has `browser` field in its `package.json`.
105 4
    // For Electron (which does not respect `browser` field),
106 4
    // pass `{fallback: true}` to the ImageAnnotatorClient constructor.
107 4
    this._gaxModule = opts.fallback ? gax.fallback : gax;
108 4

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

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

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

120 4
    // Determine the client header string.
121 4
    const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
122 4
    if (typeof process !== 'undefined' && 'versions' in process) {
123 4
      clientHeader.push(`gl-node/${process.versions.node}`);
124 4
    } else {
125 0
      clientHeader.push(`gl-web/${this._gaxModule.version}`);
126 0
    }
127 4
    if (!opts.fallback) {
128 4
      clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
129 4
    }
130 4
    if (opts.libName && opts.libVersion) {
131 0
      clientHeader.push(`${opts.libName}/${opts.libVersion}`);
132 0
    }
133 4
    // Load the applicable protos.
134 4
    // For Node.js, pass the path to JSON proto file.
135 4
    // For browsers, pass the JSON content.
136 4

137 4
    const nodejsProtoPath = path.join(
138 4
      __dirname,
139 4
      '..',
140 4
      '..',
141 4
      'protos',
142 4
      'protos.json'
143 4
    );
144 4
    this._protos = this._gaxGrpc.loadProto(
145 4
      opts.fallback
146 4
        ? // eslint-disable-next-line @typescript-eslint/no-var-requires
147 4
          require('../../protos/protos.json')
148 4
        : nodejsProtoPath
149 4
    );
150 4

151 4
    // Put together the default options sent with requests.
152 4
    this._defaults = this._gaxGrpc.constructSettings(
153 4
      'google.cloud.vision.v1p1beta1.ImageAnnotator',
154 4
      gapicConfig as gax.ClientConfig,
155 4
      opts.clientConfig || {},
156 4
      {'x-goog-api-client': clientHeader.join(' ')}
157 4
    );
158 4

159 4
    // Set up a dictionary of "inner API calls"; the core implementation
160 4
    // of calling the API is handled in `google-gax`, with this code
161 4
    // merely providing the destination and request information.
162 4
    this.innerApiCalls = {};
163 4
  }
164 4

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

182 4
    // Put together the "service stub" for
183 4
    // google.cloud.vision.v1p1beta1.ImageAnnotator.
184 4
    this.imageAnnotatorStub = this._gaxGrpc.createStub(
185 4
      this._opts.fallback
186 4
        ? (this._protos as protobuf.Root).lookupService(
187 0
            'google.cloud.vision.v1p1beta1.ImageAnnotator'
188 4
          )
189 4
        : // eslint-disable-next-line @typescript-eslint/no-explicit-any
190 4
          (this._protos as any).google.cloud.vision.v1p1beta1.ImageAnnotator,
191 4
      this._opts
192 4
    ) as Promise<{[method: string]: Function}>;
193 4

194 4
    // Iterate over each of the methods that the service provides
195 4
    // and create an API call method for each.
196 4
    const imageAnnotatorStubMethods = ['batchAnnotateImages'];
197 4
    for (const methodName of imageAnnotatorStubMethods) {
198 4
      const callPromise = this.imageAnnotatorStub.then(
199 4
        stub => (...args: Array<{}>) => {
200 1
          if (this._terminated) {
201 1
            return Promise.reject('The client has already been closed.');
202 1
          }
203 1
          const func = stub[methodName];
204 1
          return func.apply(stub, args);
205 4
        },
206 4
        (err: Error | null | undefined) => () => {
207 1
          throw err;
208 1
        }
209 4
      );
210 4

211 4
      const descriptor = undefined;
212 4
      const apiCall = this._gaxModule.createApiCall(
213 4
        callPromise,
214 4
        this._defaults[methodName],
215 4
        descriptor
216 4
      );
217 4

218 4
      this.innerApiCalls[methodName] = apiCall;
219 4
    }
220 4

221 4
    return this.imageAnnotatorStub;
222 4
  }
223 4

224 4
  /**
225 4
   * The DNS address for this API service.
226 4
   */
227 4
  static get servicePath() {
228 4
    return 'vision.googleapis.com';
229 4
  }
230 4

231 4
  /**
232 4
   * The DNS address for this API service - same as servicePath(),
233 4
   * exists for compatibility reasons.
234 4
   */
235 4
  static get apiEndpoint() {
236 4
    return 'vision.googleapis.com';
237 4
  }
238 4

239 4
  /**
240 4
   * The port for this API service.
241 4
   */
242 4
  static get port() {
243 4
    return 443;
244 4
  }
245 4

246 4
  /**
247 4
   * The scopes needed to make gRPC calls for every method defined
248 4
   * in this service.
249 4
   */
250 4
  static get scopes() {
251 4
    return [
252 4
      'https://www.googleapis.com/auth/cloud-platform',
253 4
      'https://www.googleapis.com/auth/cloud-vision',
254 4
    ];
255 4
  }
256 4

257 4
  getProjectId(): Promise<string>;
258 4
  getProjectId(callback: Callback<string, undefined, undefined>): void;
259 4
  /**
260 4
   * Return the project ID used by this class.
261 4
   * @param {function(Error, string)} callback - the callback to
262 4
   *   be called with the current project Id.
263 4
   */
264 4
  getProjectId(
265 4
    callback?: Callback<string, undefined, undefined>
266 4
  ): Promise<string> | void {
267 4
    if (callback) {
268 4
      this.auth.getProjectId(callback);
269 4
      return;
270 4
    }
271 4
    return this.auth.getProjectId();
272 4
  }
273 4

274 4
  // -------------------
275 4
  // -- Service calls --
276 4
  // -------------------
277 4
  batchAnnotateImages(
278 4
    request: protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest,
279 4
    options?: gax.CallOptions
280 4
  ): Promise<
281 4
    [
282 4
      protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
283 4
      (
284 4
        | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
285 4
        | undefined
286 4
      ),
287 4
      {} | undefined
288 4
    ]
289 4
  >;
290 4
  batchAnnotateImages(
291 4
    request: protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest,
292 4
    options: gax.CallOptions,
293 4
    callback: Callback<
294 4
      protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
295 4
      | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
296 4
      | null
297 4
      | undefined,
298 4
      {} | null | undefined
299 4
    >
300 4
  ): void;
301 4
  batchAnnotateImages(
302 4
    request: protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest,
303 4
    callback: Callback<
304 4
      protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
305 4
      | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
306 4
      | null
307 4
      | undefined,
308 4
      {} | null | undefined
309 4
    >
310 4
  ): void;
311 4
  /**
312 4
   * Run image detection and annotation for a batch of images.
313 4
   *
314 4
   * @param {Object} request
315 4
   *   The request object that will be sent.
316 4
   * @param {number[]} request.requests
317 4
   *   Required. Individual image annotation requests for this batch.
318 4
   * @param {object} [options]
319 4
   *   Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
320 4
   * @returns {Promise} - The promise which resolves to an array.
321 4
   *   The first element of the array is an object representing [BatchAnnotateImagesResponse]{@link google.cloud.vision.v1p1beta1.BatchAnnotateImagesResponse}.
322 4
   *   The promise has a method named "cancel" which cancels the ongoing API call.
323 4
   */
324 4
  batchAnnotateImages(
325 4
    request: protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest,
326 4
    optionsOrCallback?:
327 4
      | gax.CallOptions
328 4
      | Callback<
329 4
          protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
330 4
          | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
331 4
          | null
332 4
          | undefined,
333 4
          {} | null | undefined
334 4
        >,
335 4
    callback?: Callback<
336 4
      protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
337 4
      | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
338 4
      | null
339 4
      | undefined,
340 4
      {} | null | undefined
341 4
    >
342 4
  ): Promise<
343 4
    [
344 4
      protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesResponse,
345 4
      (
346 4
        | protos.google.cloud.vision.v1p1beta1.IBatchAnnotateImagesRequest
347 4
        | undefined
348 4
      ),
349 4
      {} | undefined
350 4
    ]
351 4
  > | void {
352 4
    request = request || {};
353 4
    let options: gax.CallOptions;
354 4
    if (typeof optionsOrCallback === 'function' && callback === undefined) {
355 4
      callback = optionsOrCallback;
356 4
      options = {};
357 4
    } else {
358 4
      options = optionsOrCallback as gax.CallOptions;
359 4
    }
360 4
    options = options || {};
361 4
    this.initialize();
362 4
    return this.innerApiCalls.batchAnnotateImages(request, options, callback);
363 4
  }
364 4

365 4
  /**
366 4
   * Terminate the GRPC channel and close the client.
367 4
   *
368 4
   * The client will no longer be usable and all future behavior is undefined.
369 4
   */
370 4
  close(): Promise<void> {
371 4
    this.initialize();
372 4
    if (!this._terminated) {
373 4
      return this.imageAnnotatorStub!.then(stub => {
374 4
        this._terminated = true;
375 4
        stub.close();
376 4
      });
377 4
    }
378 0
    return Promise.resolve();
379 0
  }
380 4
}
381 4
import {FeaturesMethod} from '../helpers';
382 4
// eslint-disable-next-line @typescript-eslint/no-empty-interface
383 4
export interface ImageAnnotatorClient extends FeaturesMethod {}

Read our documentation on viewing source code .

Loading