Exemple #1
0
function v1(options) {
  options = extend({
    scopes: v1.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  return speechClient(gaxGrpc);
}
Exemple #2
0
function v1beta1(options) {
  options = extend({
    scopes: v1beta1.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  return languageServiceApi(gaxGrpc);
}
Exemple #3
0
function v2(options) {
  options = extend({
    scopes: v2.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  return fooClient(gaxGrpc);
}
Exemple #4
0
function v2beta1(options) {
  options = extend({
    scopes: v2beta1.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  return dlpServiceClient(gaxGrpc);
}
Exemple #5
0
function v3(options) {
  options = extend({
    scopes: v3.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  var result = {};
  extend(result, groupServiceClient(gaxGrpc));
  extend(result, metricServiceClient(gaxGrpc));
  return result;
}
Exemple #6
0
function v2(options) {
  options = extend({
    scopes: v2.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  var result = {};
  extend(result, fooApi(gaxGrpc));
  extend(result, barApi(gaxGrpc));
  return result;
}
Exemple #7
0
function v2(options) {
  options = extend({
    scopes: v2.ALL_SCOPES
  }, options);
  var gaxGrpc = gax.grpc(options);
  return extend(
      {},
      loggingServiceV2Client(gaxGrpc),
      configServiceV2Client(gaxGrpc),
      metricsServiceV2Client(gaxGrpc));
}
Exemple #8
0
module.exports = apiVersion => {
  var methods = {};

  /**
   * Annotate a single image with the requested features.
   *
   * @param {Object=} request
   *   A representation of the request being sent to the Vision API.
   * @param {Object=} request.image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Array} request.features
   *   An array of the specific annotation features being requested.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var request = {
   *   image: {source: {imageUri: 'gs://path/to/image.jpg'}},
   *   features: [],
   * };
   * vision.annotateImage(request).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.annotateImage = promisify(function(request, options, callback) {
    // If a callback was provided and options were skipped, normalize
    // the argument names.
    if (is.undefined(callback) && is.function(options)) {
      callback = options;
      options = undefined;
    }

    // If there is no image, throw an exception.
    if (is.undefined(request.image)) {
      throw new Error('Attempted to call `annotateImage` with no image.');
    }

    // If we got a filename for the image, open the file and transform
    // it to content.
    return coerceImage(request.image, (err, image) => {
      if (err) {
        return callback(err);
      }
      request.image = image;

      // Call the GAPIC batch annotation function.
      return this.batchAnnotateImages([request], options, (err, r) => {
        // If there is an error, handle it.
        if (err) {
          return callback(err);
        }

        // We are guaranteed to only have one response element, since we
        // only sent one image.
        var response = r.responses[0];

        // Fire the callback if applicable.
        return callback(undefined, response);
      });
    });
  });

  // Get a list of features available on the API. Although we could iterate over
  // them and create single-feature methods for each dynamically, for
  // documentation purpose, we manually list all the single-feature methods
  // below.
  const features = gax.grpc().load([{
    root: protoFiles('..'),
    file: `google/cloud/vision/${apiVersion}/image_annotator.proto`,
  }]).google.cloud.vision[apiVersion].Feature.Type;

  /**
   * Annotate a single image with face detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.faceDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.faceDetection =
    promisify(_createSingleFeatureMethod(features.FACE_DETECTION));

  /**
   * Annotate a single image with landmark detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.landmarkDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.landmarkDetection =
    promisify(_createSingleFeatureMethod(features.LANDMARK_DETECTION));

  /**
   * Annotate a single image with logo detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.logoDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.logoDetection =
    promisify(_createSingleFeatureMethod(features.LOGO_DETECTION));

  /**
   * Annotate a single image with label detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.labelDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.labelDetection =
    promisify(_createSingleFeatureMethod(features.LABEL_DETECTION));

  /**
   * Annotate a single image with text detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.textDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.textDetection =
    promisify(_createSingleFeatureMethod(features.TEXT_DETECTION));

  /**
   * Annotate a single image with document text detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.documentTextDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.documentTextDetection =
    promisify(_createSingleFeatureMethod(features.DOCUMENT_TEXT_DETECTION));

  /**
   * Annotate a single image with safe search detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.safeSearchDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.safeSearchDetection =
    promisify(_createSingleFeatureMethod(features.SAFE_SEARCH_DETECTION));

  /**
   * Annotate a single image with image properties.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.imageProperties(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.imageProperties =
    promisify(_createSingleFeatureMethod(features.IMAGE_PROPERTIES));

  /**
   * Annotate a single image with crop hints.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.cropHints(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.cropHints =
    promisify(_createSingleFeatureMethod(features.CROP_HINTS));

  /**
   * Annotate a single image with web detection.
   *
   * @param {Object=} image
   *   A dictionary-like object representing the image. This should have a
   *   single key (`source`, `content`).
   *
   *   If the key is `source`, the value should be another object containing
   *   `imageUri` or `filename` as a key and a string as a value.
   *
   *   If the key is `content`, the value should be a Buffer.
   * @param {Object=} options
   *   Optional parameters. You can override the default settings for this
   *   call, e.g, timeout, retries, paginations, etc. See
   *   [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
   *   for the details.
   * @param {function(?Error, ?Object)=} callback
   *   The function which will be called with the result of the API call.
   *
   *   The second parameter to the callback is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   * @return {Promise} - The promise which resolves to an array.
   *   The first element of the array is an object representing
   *   [BatchAnnotateImagesResponse]{@link BatchAnnotateImagesResponse}.
   *   The promise has a method named "cancel" which cancels the ongoing
   *   API call.
   *
   * @example
   * var image = {
   *   source: {imageUri: 'gs://path/to/image.jpg'}
   * };
   * vision.webDetection(image).then(response => {
   *   // doThingsWith(response);
   * }).catch(err => {
   *   console.error(err);
   * });
   */
  methods.webDetection =
    promisify(_createSingleFeatureMethod(features.WEB_DETECTION));

  return methods;
};
  /**
   * Construct an instance of SpeechClient.
   *
   * @param {object} [options] - The configuration object. See the subsequent
   *   parameters for more details.
   * @param {object} [options.credentials] - Credentials object.
   * @param {string} [options.credentials.client_email]
   * @param {string} [options.credentials.private_key]
   * @param {string} [options.email] - Account email address. Required when
   *     using a .pem or .p12 keyFilename.
   * @param {string} [options.keyFilename] - Full path to the a .json, .pem, or
   *     .p12 key downloaded from the Google Developers Console. If you provide
   *     a path to a JSON file, the projectId option below is not necessary.
   *     NOTE: .pem and .p12 require you to specify options.email as well.
   * @param {number} [options.port] - The port on which to connect to
   *     the remote host.
   * @param {string} [options.projectId] - The project ID from the Google
   *     Developer's Console, e.g. 'grape-spaceship-123'. We will also check
   *     the environment variable GCLOUD_PROJECT for your project ID. If your
   *     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.servicePath] - The domain name of the
   *     API remote host.
   */
  constructor(opts) {
    this._descriptors = {};

    // Ensure that options include the service address and port.
    opts = Object.assign(
      {
        clientConfig: {},
        port: this.constructor.port,
        servicePath: this.constructor.servicePath,
      },
      opts
    );

    // Create a `gaxGrpc` object, with any grpc-specific options
    // sent to the client.
    opts.scopes = this.constructor.scopes;
    var gaxGrpc = gax.grpc(opts);

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

    // Determine the client header string.
    var clientHeader = [
      `gl-node/${process.version.node}`,
      `grpc/${gaxGrpc.grpcVersion}`,
      `gax/${gax.version}`,
      `gapic/${VERSION}`,
    ];
    if (opts.libName && opts.libVersion) {
      clientHeader.push(`${opts.libName}/${opts.libVersion}`);
    }

    // Load the applicable protos.
    var protos = merge(
      {},
      gaxGrpc.loadProto(
        path.join(__dirname, '..', '..', 'protos'),
        'google/cloud/speech/v1p1beta1/cloud_speech.proto'
      )
    );

    // Some of the methods on this service provide streaming responses.
    // Provide descriptors for these.
    this._descriptors.stream = {
      streamingRecognize: new gax.StreamDescriptor(
        gax.StreamType.BIDI_STREAMING
      ),
    };
    var protoFilesRoot = new gax.grpc.GoogleProtoFilesRoot();
    protoFilesRoot = protobuf.loadSync(
      path.join(
        __dirname,
        '..',
        '..',
        'protos',
        'google/cloud/speech/v1p1beta1/cloud_speech.proto'
      ),
      protoFilesRoot
    );

    // This API contains "long-running operations", which return a
    // an Operation object that allows for tracking of the operation,
    // rather than holding a request open.
    this.operationsClient = new gax.lro({
      auth: gaxGrpc.auth,
      grpc: gaxGrpc.grpc,
    }).operationsClient(opts);

    var longRunningRecognizeResponse = protoFilesRoot.lookup(
      'google.cloud.speech.v1p1beta1.LongRunningRecognizeResponse'
    );
    var longRunningRecognizeMetadata = protoFilesRoot.lookup(
      'google.cloud.speech.v1p1beta1.LongRunningRecognizeMetadata'
    );

    this._descriptors.longrunning = {
      longRunningRecognize: new gax.LongrunningDescriptor(
        this.operationsClient,
        longRunningRecognizeResponse.decode.bind(longRunningRecognizeResponse),
        longRunningRecognizeMetadata.decode.bind(longRunningRecognizeMetadata)
      ),
    };

    // Put together the default options sent with requests.
    var defaults = gaxGrpc.constructSettings(
      'google.cloud.speech.v1p1beta1.Speech',
      gapicConfig,
      opts.clientConfig,
      {'x-goog-api-client': clientHeader.join(' ')}
    );

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

    // Put together the "service stub" for
    // google.cloud.speech.v1p1beta1.Speech.
    var speechStub = gaxGrpc.createStub(
      protos.google.cloud.speech.v1p1beta1.Speech,
      opts
    );

    // Iterate over each of the methods that the service provides
    // and create an API call method for each.
    var speechStubMethods = [
      'recognize',
      'longRunningRecognize',
      'streamingRecognize',
    ];
    for (let methodName of speechStubMethods) {
      this._innerApiCalls[methodName] = gax.createApiCall(
        speechStub.then(
          stub =>
            function() {
              var args = Array.prototype.slice.call(arguments, 0);
              return stub[methodName].apply(stub, args);
            }
        ),
        defaults[methodName],
        this._descriptors.stream[methodName] ||
          this._descriptors.longrunning[methodName]
      );
    }
  }