From 1f0e8a1178962172a095729005b091ca98210f01 Mon Sep 17 00:00:00 2001 From: Jun Mukai Date: Thu, 11 Aug 2016 16:24:21 -0700 Subject: [PATCH] Modify the structure. - the generated code will now export a function, which returns grpc and API class upon specified authentication context. - v1beta1 also exports as a function, which builds API classes. (currently it's same as LanguageServiceApi itself, because language API is a single-service API). The new usage would be: ``` var language = require('@google-cloud/language'); var v1beta1 = language.v1beta1({keyFile: ...}); var api = v1beta1.LanguageServiceApi(); api.annotateText({type: v1beta1.grpc.Document.Type.PLAIN_TEXT, ...}); ``` --- packages/language/package.json | 2 +- packages/language/src/v1beta1/index.js | 9 +- .../src/v1beta1/language_service_api.js | 392 +++++++++--------- 3 files changed, 197 insertions(+), 206 deletions(-) diff --git a/packages/language/package.json b/packages/language/package.json index d2009963c749..09907a3b3bb6 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -57,7 +57,7 @@ "arguejs": "^0.2.3", "arrify": "^1.0.1", "extend": "^3.0.0", - "google-gax": "^0.5.2", + "google-gax": "^0.6.0", "google-proto-files": "^0.4.0", "is": "^3.0.1", "propprop": "^0.3.1", diff --git a/packages/language/src/v1beta1/index.js b/packages/language/src/v1beta1/index.js index b18108db6dda..3c863f6d5000 100644 --- a/packages/language/src/v1beta1/index.js +++ b/packages/language/src/v1beta1/index.js @@ -2,7 +2,10 @@ var languageServiceApi = require('./language_service_api'); -exports.grpc = function(optGrpc) { - return languageServiceApi.grpc(optGrpc).google.cloud.language.v1beta1; +function v1beta1(options) { + return languageServiceApi(options); }; -exports.languageServiceApi = languageServiceApi.LanguageServiceApi; + +v1beta1.SERVICE_ADDRESS = languageServiceApi.SERVICE_ADDRESS; +v1beta1.ALL_SCOPES = languageServiceApi.ALL_SCOPES; +module.exports = v1beta1; diff --git a/packages/language/src/v1beta1/language_service_api.js b/packages/language/src/v1beta1/language_service_api.js index 5e13a7f9af6f..3cdc475e7a78 100644 --- a/packages/language/src/v1beta1/language_service_api.js +++ b/packages/language/src/v1beta1/language_service_api.js @@ -47,214 +47,202 @@ var ALL_SCOPES = [ 'https://www.googleapis.com/auth/cloud-platform' ]; -function loadGrpc(optGrpc) { - return gax.loadGrpc(optGrpc, [{ +module.exports = function build(options) { + var built = {}; + options = options || {}; + if (!('scopes' in options)) { + options.scopes = ALL_SCOPES; + } + var gaxGrpc = gax.grpc(options); + + var grpcClient = gaxGrpc.load([{ root: require('google-proto-files')('..'), file: 'google/cloud/language/v1beta1/language_service.proto' }]); -} -exports.grpc = loadGrpc; - -/** - * Provides text analysis operations such as sentiment analysis and entity - * recognition. - * - * @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.grpc - * When specified, this is used as the grpc module. Otherwise - * the grpc package will be loaded from the dependency (typically - * the one within 'google-gax' will be loaded). This will be useful - * to share channels among multiple APIs. - * @param {Auth} opts.auth - * When specified, this auth object (an instance of 'google-auto-auth') - * will be used for the further authentication. scopes, keyFile, and - * credentials will be ignored. - * @param {String[]} opts.scopes - * The list of scopes to be used for the authentication. - * @param {String} opts.keyFile - * The name of auth key file. - * @param {Object} opts.credentials - * The credential data (normally the loaded data from the JSON file). - * @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(opts) { - if (!(this instanceof LanguageServiceApi)) { - return new LanguageServiceApi(opts); + built.grpc = grpcClient.google.cloud.language.v1beta1; + /** + * Provides text analysis operations such as sentiment analysis and entity + * recognition. + * + * @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(opts) { + if (!(this instanceof LanguageServiceApi)) { + return new LanguageServiceApi(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)); } - opts = opts || {}; - var servicePath = opts.servicePath || SERVICE_ADDRESS; - var port = opts.port || DEFAULT_SERVICE_PORT; - var sslCreds = opts.sslCreds || null; - var scopes = opts.scopes || ALL_SCOPES; - var clientConfig = opts.clientConfig || {}; - var timeout = opts.timeout || DEFAULT_TIMEOUT; - var appName = opts.appName || 'gax'; - var appVersion = opts.appVersion || gax.Version; - - var grpcClient = loadGrpc(opts.grpc); - - var googleApiClient = [ - appName + '/' + appVersion, - CODE_GEN_NAME_VERSION, - 'nodejs/' + process.version].join(' '); - - var defaults = gax.constructSettingsGrpc( - 'google.cloud.language.v1beta1.LanguageService', - configData, - clientConfig, - timeout, - null, - null, - {'x-goog-api-client': googleApiClient}, - opts.grpc); - - this.stub = gax.createStub( - servicePath, - port, - grpcClient.google.cloud.language.v1beta1.LanguageService, - {'grpc': opts.grpc, - 'sslCreds': sslCreds, - 'scopes': scopes, - 'keyFile': opts.keyFile || opts.keyFilename, - 'credentials': opts.credentials}); - var methods = [ - 'analyzeSentiment', - 'analyzeEntities', - 'annotateText' - ]; - methods.forEach(function(methodName) { - this['_' + methodName] = gax.createApiCall( - this.stub.then(function(stub) { return stub[methodName].bind(stub); }), - defaults[methodName]); - }.bind(this)); -} -exports.LanguageServiceApi = LanguageServiceApi; - -// 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 + built.LanguageServiceApi = LanguageServiceApi; + + // 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); }; - 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 + /** + * 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); }; - 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 + /** + * 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); }; - return this._annotateText(req, args.options, args.callback); -}; + return built; +} + +module.exports.SERVICE_ADDRESS; +module.exports.ALL_SCOPES; \ No newline at end of file