Skip to content

Commit

Permalink
language: use gax (#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and sofisl committed Oct 13, 2022
1 parent 08b53d9 commit 7669853
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 201 deletions.
4 changes: 1 addition & 3 deletions packages/google-cloud-language/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
55 changes: 20 additions & 35 deletions packages/google-cloud-language/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 4 additions & 21 deletions packages/google-cloud-language/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -478,4 +461,4 @@ Language.prototype.text = function(content, options) {
};

module.exports = Language;
module.exports.v1beta1 = require('./v1beta1');
module.exports.v1beta1 = v1beta1;
Loading

0 comments on commit 7669853

Please sign in to comment.