diff --git a/packages/google-cloud-language/package.json b/packages/google-cloud-language/package.json index d10c8f29d89..aa78ba4beb7 100644 --- a/packages/google-cloud-language/package.json +++ b/packages/google-cloud-language/package.json @@ -54,8 +54,10 @@ "dependencies": { "@google-cloud/common": "^0.1.0", "@google-cloud/storage": "^0.1.0", + "arguejs": "^0.2.3", "arrify": "^1.0.1", "extend": "^3.0.0", + "google-gax": "^0.6.0", "google-proto-files": "^0.4.0", "is": "^3.0.1", "propprop": "^0.3.1", diff --git a/packages/google-cloud-language/src/index.js b/packages/google-cloud-language/src/index.js index 5c7ee6fbedd..3eb43b5f9c8 100644 --- a/packages/google-cloud-language/src/index.js +++ b/packages/google-cloud-language/src/index.js @@ -434,3 +434,4 @@ Language.prototype.text = function(content, options) { }; module.exports = Language; +module.exports.v1beta1 = require('./v1beta1'); diff --git a/packages/google-cloud-language/src/v1beta1/index.js b/packages/google-cloud-language/src/v1beta1/index.js new file mode 100644 index 00000000000..2a00e520c12 --- /dev/null +++ b/packages/google-cloud-language/src/v1beta1/index.js @@ -0,0 +1,32 @@ +/*! + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +var languageServiceApi = require('./language_service_api'); +var extend = require('extend'); +var gax = require('google-gax'); + +function v1beta1(options) { + options = extend({ + scopes: v1beta1.ALL_SCOPES + }, options); + var gaxGrpc = gax.grpc(options); + return languageServiceApi(gaxGrpc); +} + +v1beta1.SERVICE_ADDRESS = languageServiceApi.SERVICE_ADDRESS; +v1beta1.ALL_SCOPES = languageServiceApi.ALL_SCOPES; +module.exports = v1beta1; diff --git a/packages/google-cloud-language/src/v1beta1/language_service_api.js b/packages/google-cloud-language/src/v1beta1/language_service_api.js new file mode 100644 index 00000000000..14069ed8f67 --- /dev/null +++ b/packages/google-cloud-language/src/v1beta1/language_service_api.js @@ -0,0 +1,251 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * EDITING INSTRUCTIONS + * This file was generated from the file + * https://github.com/googleapis/googleapis/blob/master/library.proto, + * and updates to that file get reflected here through a refresh process. + * For the short term, the refresh process will only be runnable by Google + * engineers. + * + * The only allowed edits are to method and file documentation. A 3-way + * merge preserves those additions if the generated source changes. + */ +/* TODO: introduce line-wrapping so that it never exceeds the limit. */ +/* jscs: disable maximumLineLength */ +'use strict'; + +var arguejs = require('arguejs'); +var configData = require('./language_service_client_config'); +var extend = require('extend'); +var gax = require('google-gax'); + +var SERVICE_ADDRESS = 'language.googleapis.com'; + +var DEFAULT_SERVICE_PORT = 443; + +var CODE_GEN_NAME_VERSION = 'gapic/0.1.0'; + +var DEFAULT_TIMEOUT = 30; + +/** + * The scopes needed to make gRPC calls to all of the methods defined in + * this service. + */ +var ALL_SCOPES = [ + 'https://www.googleapis.com/auth/cloud-platform' +]; + +/** + * Provides text analysis operations such as sentiment analysis and entity + * recognition. + * + * This will be created through a builder function which can be obtained by the module. + * See the following example of how to initialize the module and how to access to the builder. + * + * @example + * var languageV1beta1 = require('@google-cloud/language').v1beta1({ + * // optional auth parameters. + * }); + * var api = languageV1beta1.languageServiceApi(); + * + * @class + * @param {Object=} opts - The optional parameters. + * @param {String=} opts.servicePath + * The domain name of the API remote host. + * @param {number=} opts.port + * The port on which to connect to the remote host. + * @param {grpc.ClientCredentials=} opts.sslCreds + * A ClientCredentials for use with an SSL-enabled channel. + * @param {Object=} opts.clientConfig + * The customized config to build the call settings. See + * {@link gax.constructSettings} for the format. + * @param {number=} opts.timeout + * The default timeout, in seconds, for calls made through this client. + * @param {number=} opts.appName + * The codename of the calling service. + * @param {String=} opts.appVersion + * The version of the calling service. + */ +function LanguageServiceApi(gaxGrpc, grpcClient, opts) { + opts = opts || {}; + var servicePath = opts.servicePath || SERVICE_ADDRESS; + var port = opts.port || DEFAULT_SERVICE_PORT; + var sslCreds = opts.sslCreds || null; + var clientConfig = opts.clientConfig || {}; + var timeout = opts.timeout || DEFAULT_TIMEOUT; + var appName = opts.appName || 'gax'; + var appVersion = opts.appVersion || gax.Version; + + var googleApiClient = [ + appName + '/' + appVersion, + CODE_GEN_NAME_VERSION, + 'nodejs/' + process.version].join(' '); + + var defaults = gaxGrpc.constructSettings( + 'google.cloud.language.v1beta1.LanguageService', + configData, + clientConfig, + timeout, + null, + null, + {'x-goog-api-client': googleApiClient}); + + var stub = gaxGrpc.createStub( + servicePath, + port, + grpcClient.google.cloud.language.v1beta1.LanguageService, + {sslCreds: sslCreds}); + var methods = [ + 'analyzeSentiment', + 'analyzeEntities', + 'annotateText' + ]; + methods.forEach(function(methodName) { + this['_' + methodName] = gax.createApiCall( + stub.then(function(stub) { return stub[methodName].bind(stub); }), + defaults[methodName]); + }.bind(this)); +} + +// Callback types + +/** + * @callback APICallback + * @param {?Error} error - the error object if something goes wrong. + * Null if API succeeds. + * @param {?T} response + * The response object when API succeeds. + * @template T + */ + +/** + * @callback EmptyCallback + * @param {?Error} error - the error object if something goes wrong. + * Null if API succeeds. + */ + +// Service calls + +/** + * Analyzes the sentiment of the provided text. + * + * @param {google.cloud.language.v1beta1.Document} document + * Input document. Currently, `analyzeSentiment` only supports English text + * ({@link Document.language}="EN"). + * @param {gax.CallOptions=} options + * Overrides the default settings for this call, e.g, timeout, + * retries, etc. + * @param {APICallback=} callback + * The function which will be called with the result of the API call. + * @returns {gax.EventEmitter} - the event emitter to handle the call + * status. + * @throws an error if the RPC is aborted. + */ +LanguageServiceApi.prototype.analyzeSentiment = function analyzeSentiment() { + var args = arguejs({ + document: Object, + options: [gax.CallOptions], + callback: [Function] + }, arguments); + var req = { + document: args.document + }; + return this._analyzeSentiment(req, args.options, args.callback); +}; + +/** + * Finds named entities (currently finds proper names) in the text, + * entity types, salience, mentions for each entity, and other properties. + * + * @param {google.cloud.language.v1beta1.Document} document + * Input document. + * @param {google.cloud.language.v1beta1.EncodingType} encodingType + * The encoding type used by the API to calculate offsets. + * @param {gax.CallOptions=} options + * Overrides the default settings for this call, e.g, timeout, + * retries, etc. + * @param {APICallback=} callback + * The function which will be called with the result of the API call. + * @returns {gax.EventEmitter} - the event emitter to handle the call + * status. + * @throws an error if the RPC is aborted. + */ +LanguageServiceApi.prototype.analyzeEntities = function analyzeEntities() { + var args = arguejs({ + document: Object, + encodingType: Number, + options: [gax.CallOptions], + callback: [Function] + }, arguments); + var req = { + document: args.document, + encoding_type: args.encodingType + }; + return this._analyzeEntities(req, args.options, args.callback); +}; + +/** + * Advanced API that analyzes the document and provides a full set of text + * annotations, including semantic, syntactic, and sentiment information. This + * API is intended for users who are familiar with machine learning and need + * in-depth text features to build upon. + * + * @param {google.cloud.language.v1beta1.Document} document + * Input document. + * @param {google.cloud.language.v1beta1.AnnotateTextRequest.Features} features + * The enabled features. + * @param {google.cloud.language.v1beta1.EncodingType} encodingType + * The encoding type used by the API to calculate offsets. + * @param {gax.CallOptions=} options + * Overrides the default settings for this call, e.g, timeout, + * retries, etc. + * @param {APICallback=} callback + * The function which will be called with the result of the API call. + * @returns {gax.EventEmitter} - the event emitter to handle the call + * status. + * @throws an error if the RPC is aborted. + */ +LanguageServiceApi.prototype.annotateText = function annotateText() { + var args = arguejs({ + document: Object, + features: Object, + encodingType: Number, + options: [gax.CallOptions], + callback: [Function] + }, arguments); + var req = { + document: args.document, + features: args.features, + encoding_type: args.encodingType + }; + return this._annotateText(req, args.options, args.callback); +}; + +module.exports = function build(gaxGrpc) { + var grpcClient = gaxGrpc.load([{ + root: require('google-proto-files')('..'), + file: 'google/cloud/language/v1beta1/language_service.proto' + }]); + var built = grpcClient.google.cloud.language.v1beta1; + + built.languageServiceApi = function(opts) { + return new LanguageServiceApi(gaxGrpc, grpcClient, opts); + }; + extend(built.languageServiceApi, LanguageServiceApi); + return built; +}; +module.exports.SERVICE_ADDRESS = SERVICE_ADDRESS; +module.exports.ALL_SCOPES = ALL_SCOPES; \ No newline at end of file diff --git a/packages/google-cloud-language/src/v1beta1/language_service_client_config.json b/packages/google-cloud-language/src/v1beta1/language_service_client_config.json new file mode 100644 index 00000000000..6ac3a7004ff --- /dev/null +++ b/packages/google-cloud-language/src/v1beta1/language_service_client_config.json @@ -0,0 +1,43 @@ +{ + "interfaces": { + "google.cloud.language.v1beta1.LanguageService": { + "retry_codes": { + "retry_codes_def": { + "idempotent": [ + "DEADLINE_EXCEEDED", + "UNAVAILABLE" + ], + "non_idempotent": [] + } + }, + "retry_params": { + "default": { + "initial_retry_delay_millis": 100, + "retry_delay_multiplier": 1.3, + "max_retry_delay_millis": 60000, + "initial_rpc_timeout_millis": 60000, + "rpc_timeout_multiplier": 1.0, + "max_rpc_timeout_millis": 60000, + "total_timeout_millis": 600000 + } + }, + "methods": { + "AnalyzeSentiment": { + "timeout_millis": 30000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + }, + "AnalyzeEntities": { + "timeout_millis": 30000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + }, + "AnnotateText": { + "timeout_millis": 30000, + "retry_codes_name": "idempotent", + "retry_params_name": "default" + } + } + } + } +}