diff --git a/packages/language/package.json b/packages/language/package.json index 3dc5d2e631d..3016cc471bc 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -56,10 +56,8 @@ "arrify": "^1.0.1", "extend": "^3.0.0", "google-gax": "^0.7.0", - "google-proto-files": "^0.8.0", "is": "^3.0.1", - "propprop": "^0.3.1", - "string-format-obj": "^1.0.0" + "propprop": "^0.3.1" }, "devDependencies": { "@google-cloud/storage": "*", diff --git a/packages/language/src/document.js b/packages/language/src/document.js index e567a45a88a..6fd4d48af74 100644 --- a/packages/language/src/document.js +++ b/packages/language/src/document.js @@ -68,42 +68,37 @@ var prop = require('propprop'); function Document(language, config) { var content = config.content || config; - // `reqOpts` is the payload passed to each `request()`. This object is used as - // the default for all API requests made with this Document. - this.reqOpts = { - document: {} - }; + this.api = language.api; + + this.document = {}; if (config.encoding) { - var encodingType = config.encoding.toUpperCase().replace(/[ -]/g, ''); - this.reqOpts.encodingType = encodingType; + this.encodingType = config.encoding.toUpperCase().replace(/[ -]/g, ''); } if (config.language) { - this.reqOpts.document.language = config.language; + this.document.language = config.language; } if (config.type) { - this.reqOpts.document.type = config.type.toUpperCase(); + this.document.type = config.type.toUpperCase(); - if (this.reqOpts.document.type === 'TEXT') { - this.reqOpts.document.type = 'PLAIN_TEXT'; + if (this.document.type === 'TEXT') { + this.document.type = 'PLAIN_TEXT'; } } else { // Default to plain text. - this.reqOpts.document.type = 'PLAIN_TEXT'; + this.document.type = 'PLAIN_TEXT'; } if (common.util.isCustomType(content, 'storage/file')) { - this.reqOpts.document.gcsContentUri = format('gs://{bucket}/{file}', { + this.document.gcsContentUri = format('gs://{bucket}/{file}', { bucket: encodeURIComponent(content.bucket.id), file: encodeURIComponent(content.id) }); } else { - this.reqOpts.document.content = content; + this.document.content = content; } - - this.request = language.request.bind(language); } /** @@ -392,16 +387,10 @@ Document.prototype.annotate = function(options, callback) { var verbose = options.verbose === true; - var grpcOpts = { - service: 'LanguageService', - method: 'annotateText' - }; + var doc = this.document; + var encType = this.encodingType; - var reqOpts = extend({ - features: features - }, this.reqOpts); - - this.request(grpcOpts, reqOpts, function(err, resp) { + this.api.Language.annotateText(doc, features, encType, function(err, resp) { if (err) { callback(err, null, resp); return; @@ -542,12 +531,10 @@ Document.prototype.detectEntities = function(options, callback) { var verbose = options.verbose === true; - var grpcOpts = { - service: 'LanguageService', - method: 'analyzeEntities' - }; + var doc = this.document; + var encType = this.encodingType; - this.request(grpcOpts, this.reqOpts, function(err, resp) { + this.api.Language.analyzeEntities(doc, encType, function(err, resp) { if (err) { callback(err, null, resp); return; @@ -610,12 +597,10 @@ Document.prototype.detectSentiment = function(options, callback) { var verbose = options.verbose === true; - var grpcOpts = { - service: 'LanguageService', - method: 'analyzeSentiment' - }; + var doc = this.document; + var encType = this.encodingType; - this.request(grpcOpts, this.reqOpts, function(err, resp) { + this.api.Language.analyzeSentiment(doc, encType, function(err, resp) { if (err) { callback(err, null, resp); return; diff --git a/packages/language/src/index.js b/packages/language/src/index.js index 9f9de43b35a..5d07b9e324e 100644 --- a/packages/language/src/index.js +++ b/packages/language/src/index.js @@ -22,9 +22,8 @@ var common = require('@google-cloud/common'); var extend = require('extend'); -var googleProtoFiles = require('google-proto-files'); var is = require('is'); -var util = require('util'); +var v1beta1 = require('./v1beta1'); /** * @type {module:language/document} @@ -64,27 +63,11 @@ function Language(options) { return new Language(options); } - var config = { - baseUrl: 'language.googleapis.com', - service: 'language', - apiVersion: 'v1beta1', - protoServices: { - LanguageService: { - path: googleProtoFiles.language.v1beta1, - service: 'cloud.language' - } - }, - scopes: [ - 'https://www.googleapis.com/auth/cloud-platform' - ], - packageJson: require('../package.json') + this.api = { + Language: v1beta1(options).languageServiceApi(options) }; - - common.GrpcService.call(this, config, options); } -util.inherits(Language, common.GrpcService); - /** * Run an annotation of a block of text. * @@ -478,4 +461,4 @@ Language.prototype.text = function(content, options) { }; module.exports = Language; -module.exports.v1beta1 = require('./v1beta1'); +module.exports.v1beta1 = v1beta1; diff --git a/packages/language/test/document.js b/packages/language/test/document.js index c6d69cc16ce..54ad37e451e 100644 --- a/packages/language/test/document.js +++ b/packages/language/test/document.js @@ -39,7 +39,7 @@ describe('Document', function() { var document; var LANGUAGE = { - request: util.noop + api: {} }; var CONFIG = 'inline content'; @@ -66,70 +66,53 @@ describe('Document', function() { }); describe('instantiation', function() { - it('should set the correct reqOpts for inline content', function() { - assert.deepEqual(document.reqOpts, { - document: { - content: CONFIG, - type: 'PLAIN_TEXT' - } + it('should expose the gax API', function() { + assert.strictEqual(document.api, LANGUAGE.api); + }); + + it('should set the correct document for inline content', function() { + assert.deepEqual(document.document, { + content: CONFIG, + type: 'PLAIN_TEXT' }); }); - it('should set the correct reqOpts for content with encoding', function() { + it('should set and uppercase the correct encodingType', function() { var document = new Document(LANGUAGE, { content: CONFIG, encoding: 'utf-8' }); - assert.deepEqual(document.reqOpts, { - document: { - content: CONFIG, - type: 'PLAIN_TEXT' - }, - encodingType: 'UTF8' - }); + assert.strictEqual(document.encodingType, 'UTF8'); }); - it('should set the correct reqOpts for content with language', function() { + it('should set the correct document for content with language', function() { var document = new Document(LANGUAGE, { content: CONFIG, language: 'EN' }); - assert.deepEqual(document.reqOpts, { - document: { - content: CONFIG, - type: 'PLAIN_TEXT', - language: 'EN' - } - }); + assert.strictEqual(document.document.language, 'EN'); }); - it('should set the correct reqOpts for content with type', function() { + it('should set the correct document for content with type', function() { var document = new Document(LANGUAGE, { content: CONFIG, type: 'html' }); - assert.deepEqual(document.reqOpts, { - document: { - content: CONFIG, - type: 'HTML' - } - }); + assert.strictEqual(document.document.type, 'HTML'); }); - it('should set the correct reqOpts for text', function() { + it('should set the correct document for text', function() { var document = new Document(LANGUAGE, { content: CONFIG, type: 'text' }); - assert.deepEqual(document.reqOpts, { - document: { - content: CONFIG, - type: 'PLAIN_TEXT' - } + assert.deepEqual(document.document, { + content: CONFIG, + type: 'PLAIN_TEXT' }); }); @@ -152,29 +135,12 @@ describe('Document', function() { content: file }); - assert.deepEqual(document.reqOpts, { - document: { - gcsContentUri: [ - 'gs://', - encodeURIComponent(file.bucket.id), - '/', - encodeURIComponent(file.id), - ].join(''), - type: 'PLAIN_TEXT' - } - }); - }); - - it('should create a request function', function(done) { - var LanguageInstance = { - request: function() { - assert.strictEqual(this, LanguageInstance); - done(); - } - }; - - var document = new Document(LanguageInstance, CONFIG); - document.request(); + assert.deepEqual(document.document.gcsContentUri, [ + 'gs://', + encodeURIComponent(file.bucket.id), + '/', + encodeURIComponent(file.id), + ].join('')); }); }); @@ -203,38 +169,37 @@ describe('Document', function() { describe('annotate', function() { it('should make the correct API request', function(done) { - document.request = function(grpcOpts, reqOpts) { - assert.deepEqual(grpcOpts, { - service: 'LanguageService', - method: 'annotateText' - }); + document.api.Language = { + annotateText: function(doc, features, encType) { + assert.strictEqual(doc, document.document); + + assert.deepEqual(features, { + extractDocumentSentiment: true, + extractEntities: true, + extractSyntax: true + }); - assert.deepEqual(reqOpts, extend( - { - features: { - extractDocumentSentiment: true, - extractEntities: true, - extractSyntax: true - } - }, - document.reqOpts - )); - - done(); + assert.strictEqual(encType, document.encodingType); + + done(); + } }; + document.encodingType = 'encoding-type'; document.annotate(assert.ifError); }); it('should allow specifying individual features', function(done) { - document.request = function(grpcOpts, reqOpts) { - assert.deepEqual(reqOpts.features, { - extractDocumentSentiment: false, - extractEntities: true, - extractSyntax: true - }); + document.api.Language = { + annotateText: function(doc, features) { + assert.deepEqual(features, { + extractDocumentSentiment: false, + extractEntities: true, + extractSyntax: true + }); - done(); + done(); + } }; document.annotate({ @@ -248,8 +213,10 @@ describe('Document', function() { var error = new Error('Error.'); beforeEach(function() { - document.request = function(grpcOpts, reqOpts, callback) { - callback(error, apiResponse); + document.api.Language = { + annotateText: function(doc, features, encType, callback) { + callback(error, apiResponse); + } }; }); @@ -293,9 +260,9 @@ describe('Document', function() { apiResponses.withSyntax ); - function createRequestWithResponse(apiResponse) { - return function(grpcOpts, reqOpts, callback) { - callback(null, apiResponse, apiResponse); + function createAnnotateTextWithResponse(apiResponse) { + return function(doc, features, encType, callback) { + callback(null, apiResponse); }; } @@ -308,7 +275,10 @@ describe('Document', function() { it('should always return the language', function(done) { var apiResponse = apiResponses.default; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; document.annotate(function(err, annotation, apiResponse_) { assert.ifError(err); @@ -320,7 +290,10 @@ describe('Document', function() { it('should return syntax when no features are requested', function(done) { var apiResponse = apiResponses.default; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; var formattedSentences = []; Document.formatSentences_ = function(sentences, verbose) { @@ -347,7 +320,10 @@ describe('Document', function() { it('should return the formatted sentiment if available', function(done) { var apiResponse = apiResponses.withSentiment; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; var formattedSentiment = {}; Document.formatSentiment_ = function(sentiment, verbose) { @@ -370,7 +346,10 @@ describe('Document', function() { it('should return the formatted entities if available', function(done) { var apiResponse = apiResponses.withEntities; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; var formattedEntities = []; Document.formatEntities_ = function(entities, verbose) { @@ -393,7 +372,10 @@ describe('Document', function() { it('should not return syntax analyses when not wanted', function(done) { var apiResponse = apiResponses.default; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; document.annotate({ entities: true, @@ -410,7 +392,10 @@ describe('Document', function() { it('should allow verbose mode', function(done) { var apiResponse = apiResponses.withAll; - document.request = createRequestWithResponse(apiResponse); + + document.api.Language = { + annotateText: createAnnotateTextWithResponse(apiResponse) + }; var numCallsWithCorrectVerbosityArgument = 0; @@ -440,17 +425,15 @@ describe('Document', function() { describe('detectEntities', function() { it('should make the correct API request', function(done) { - document.request = function(grpcOpts, reqOpts) { - assert.deepEqual(grpcOpts, { - service: 'LanguageService', - method: 'analyzeEntities' - }); - - assert.strictEqual(reqOpts, document.reqOpts); - - done(); + document.api.Language = { + analyzeEntities: function(doc, encType) { + assert.strictEqual(doc, document.document); + assert.strictEqual(encType, document.encodingType); + done(); + } }; + document.encodingType = 'encoding-type'; document.detectEntities(assert.ifError); }); @@ -459,8 +442,10 @@ describe('Document', function() { var error = new Error('Error.'); beforeEach(function() { - document.request = function(grpcOpts, reqOpts, callback) { - callback(error, apiResponse); + document.api.Language = { + analyzeEntities: function(doc, encType, callback) { + callback(error, apiResponse); + } }; }); @@ -482,8 +467,10 @@ describe('Document', function() { var originalApiResponse = extend({}, apiResponse); beforeEach(function() { - document.request = function(grpcOpts, reqOpts, callback) { - callback(null, apiResponse); + document.api.Language = { + analyzeEntities: function(doc, encType, callback) { + callback(null, apiResponse); + } }; }); @@ -527,17 +514,15 @@ describe('Document', function() { describe('detectSentiment', function() { it('should make the correct API request', function(done) { - document.request = function(grpcOpts, reqOpts) { - assert.deepEqual(grpcOpts, { - service: 'LanguageService', - method: 'analyzeSentiment' - }); - - assert.strictEqual(reqOpts, document.reqOpts); - - done(); + document.api.Language = { + analyzeSentiment: function(doc, encType) { + assert.strictEqual(doc, document.document); + assert.strictEqual(encType, document.encodingType); + done(); + } }; + document.encodingType = 'encoding-type'; document.detectSentiment(assert.ifError); }); @@ -546,8 +531,10 @@ describe('Document', function() { var error = new Error('Error.'); beforeEach(function() { - document.request = function(grpcOpts, reqOpts, callback) { - callback(error, apiResponse); + document.api.Language = { + analyzeSentiment: function(doc, encType, callback) { + callback(error, apiResponse); + } }; }); @@ -571,8 +558,10 @@ describe('Document', function() { beforeEach(function() { Document.formatSentiment_ = util.noop; - document.request = function(grpcOpts, reqOpts, callback) { - callback(null, apiResponse); + document.api.Language = { + analyzeSentiment: function(doc, encType, callback) { + callback(null, apiResponse); + } }; }); diff --git a/packages/language/test/index.js b/packages/language/test/index.js index 535a3e1ccd9..fa5b3707906 100644 --- a/packages/language/test/index.js +++ b/packages/language/test/index.js @@ -18,7 +18,6 @@ var assert = require('assert'); var extend = require('extend'); -var googleProtoFiles = require('google-proto-files'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; @@ -28,8 +27,15 @@ function FakeDocument() { this.calledWith_ = arguments; } -function FakeGrpcService() { - this.calledWith_ = arguments; +var fakeV1Beta1Override; +function fakeV1Beta1() { + if (fakeV1Beta1Override) { + return fakeV1Beta1Override.apply(null, arguments); + } + + return { + languageServiceApi: util.noop + }; } describe('Language', function() { @@ -41,14 +47,15 @@ describe('Language', function() { before(function() { Language = proxyquire('../src/index.js', { '@google-cloud/common': { - util: fakeUtil, - GrpcService: FakeGrpcService + util: fakeUtil }, - './document.js': FakeDocument + './document.js': FakeDocument, + './v1beta1': fakeV1Beta1 }); }); beforeEach(function() { + fakeV1Beta1Override = null; language = new Language(OPTIONS); }); @@ -78,25 +85,24 @@ describe('Language', function() { fakeUtil.normalizeArguments = normalizeArguments; }); - it('should inherit from GrpcService', function() { - assert(language instanceof FakeGrpcService); + it('should create a gax api client', function() { + var expectedLanguageService = {}; - var calledWith = language.calledWith_[0]; + fakeV1Beta1Override = function(options) { + assert.strictEqual(options, OPTIONS); - assert.deepEqual(calledWith, { - baseUrl: 'language.googleapis.com', - service: 'language', - apiVersion: 'v1beta1', - protoServices: { - LanguageService: { - path: googleProtoFiles.language.v1beta1, - service: 'cloud.language' + return { + languageServiceApi: function(options) { + assert.strictEqual(options, OPTIONS); + return expectedLanguageService; } - }, - scopes: [ - 'https://www.googleapis.com/auth/cloud-platform' - ], - packageJson: require('../package.json') + }; + }; + + var language = new Language(OPTIONS); + + assert.deepEqual(language.api, { + Language: expectedLanguageService }); }); });