Skip to content

Commit

Permalink
Language: Expose an apiVersion parameter (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored and stephenplusplus committed Apr 11, 2017
1 parent 0ffcce9 commit 03e6373
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/language/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var is = require('is');
var v1 = require('./v1');
var v1beta2 = require('./v1beta2');

var GAPIC_APIS = {
v1: v1,
v1beta2: v1beta2
};

/**
* @type {module:language/document}
* @private
Expand All @@ -51,6 +56,10 @@ var Document = require('./document.js');
* @resource [Cloud Natural Language API Documentation]{@link https://cloud.google.com/natural-language/docs}
*
* @param {object} options - [Configuration object](#/docs).
* @param {string} options.apiVersion - Either `v1` or `v1beta2`. `v1beta2` is a
* *newer* release than `v1`, and still in beta. By choosing it, you are
* opting into new, back-end behavior which is not officially supported by
* the design of this API. (Default: `v1`)
*/
function Language(options) {
if (!(this instanceof Language)) {
Expand All @@ -63,8 +72,16 @@ function Language(options) {
libVersion: require('../package.json').version
});

var gapic = GAPIC_APIS[options.apiVersion || 'v1'];

if (!gapic) {
throw new Error('Invalid `apiVersion` specified. Use "v1" or "v1beta2".');
}

delete options.apiVersion;

this.api = {
Language: v1(options).languageServiceClient(options)
Language: gapic(options).languageServiceClient(options)
};
}

Expand Down
54 changes: 53 additions & 1 deletion packages/language/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ function fakeV1() {
};
}

var fakeV1Beta2Override;
function fakeV1Beta2() {
if (fakeV1Beta2Override) {
return fakeV1Beta2Override.apply(null, arguments);
}

return {
languageServiceClient: util.noop
};
}

describe('Language', function() {
var Language;
var language;
Expand All @@ -60,12 +71,14 @@ describe('Language', function() {
util: fakeUtil
},
'./document.js': FakeDocument,
'./v1': fakeV1
'./v1': fakeV1,
'./v1beta2': fakeV1Beta2
});
});

beforeEach(function() {
fakeV1Override = null;
fakeV1Beta2Override = null;
language = new Language(OPTIONS);
});

Expand Down Expand Up @@ -123,6 +136,45 @@ describe('Language', function() {
Language: expectedLanguageService
});
});

it('should create a gax api client for v1beta2', function() {
var expectedLanguageService = {};

fakeV1Override = function() {
throw new Error('v1 should not be initialized.');
};

fakeV1Beta2Override = function(options) {
var expected = {
libName: 'gccl',
libVersion: require('../package.json').version
};
assert.deepStrictEqual(options, expected);

return {
languageServiceClient: function(options) {
assert.deepStrictEqual(options, expected);
return expectedLanguageService;
}
};
};

var language = new Language({
apiVersion: 'v1beta2'
});

assert.deepEqual(language.api, {
Language: expectedLanguageService
});
});

it('should not accept an unrecognized version', function() {
assert.throws(function() {
new Language({
apiVersion: 'v1beta42'
});
}, new RegExp('Invalid `apiVersion` specified. Use "v1" or "v1beta2".'));
});
});

describe('annotate', function() {
Expand Down

0 comments on commit 03e6373

Please sign in to comment.