Skip to content

Commit 4b8d3b7

Browse files
committed
fix nlu credentials from bluemix env
1 parent 2d0bcd2 commit 4b8d3b7

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v2.25.1
2+
* Natural Language Understanding: fixed credentials pulling from bluemix
3+
14
# v2.25.0
25
* Natural Language Understanding: new version_date and addition of listModels() and deleteModel() methods
36

natural-language-understanding/v1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const requestFactory = require('../lib/requestwrapper');
2020
const util = require('util');
2121
const BaseService = require('../lib/base_service');
22+
const extend = require('extend');
2223

2324
/**
2425
* NaturalLanguageUnderstanding
@@ -41,6 +42,23 @@ NaturalLanguageUnderstandingV1.VERSION_DATE_2016_01_23 = '2016-01-23';
4142
// https://www.ibm.com/watson/developercloud/doc/natural-language-understanding/release-notes.html
4243
NaturalLanguageUnderstandingV1.VERSION_DATE_2017_02_27 = '2017-02-27';
4344

45+
/**
46+
* Bluemix uses hyphens instead of underscores for NLU in the VCAP_SERVICES env property.
47+
* No idea why.
48+
*
49+
* This method also checks for the underscore'd version just in case they ever change it.
50+
*
51+
* @private
52+
* @override
53+
*/
54+
NaturalLanguageUnderstandingV1.prototype.getCredentialsFromBluemix = function(name) {
55+
return extend(
56+
{},
57+
BaseService.prototype.getCredentialsFromBluemix.call(this, name),
58+
BaseService.prototype.getCredentialsFromBluemix.call(this, name.replace(/_/g, '-'))
59+
);
60+
};
61+
4462
/**
4563
* Analyze the query.
4664
* @params {object} params for the query

test/unit/test.natural_language_understanding.v1.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,42 @@ describe('natural_language_understanding', function() {
3838
done();
3939
});
4040

41+
describe('env', function() {
42+
// create a new, empty env for each test, then restore it at the end
43+
const env = process.env;
44+
beforeEach(function() {
45+
process.env = {};
46+
});
47+
after(function() {
48+
process.env = env;
49+
});
50+
51+
it('should load its credentials from bluemix (hyphenated)', function() {
52+
process.env.VCAP_SERVICES = JSON.stringify({
53+
'natural-language-understanding': [
54+
{
55+
credentials: {
56+
url: 'https://gateway.watsonplatform.net/natural-language-understanding/api',
57+
username: 'hyphenated-user',
58+
password: 'hpyhenated-pass'
59+
},
60+
syslog_drain_url: null,
61+
label: 'natural-language-understanding',
62+
provider: null,
63+
plan: 'standard',
64+
name: 'my-nlu-service',
65+
tags: ['watson', 'ibm_created']
66+
}
67+
]
68+
});
69+
const nluHyphenated = new watson.NaturalLanguageUnderstandingV1({
70+
version_date: watson.NaturalLanguageUnderstandingV1.VERSION_DATE_2017_02_27
71+
});
72+
assert(nluHyphenated);
73+
assert.equal(nluHyphenated.getCredentials().username, 'hyphenated-user');
74+
});
75+
});
76+
4177
it('2016_01_23 version should work', function(done) {
4278
const mockApi = nock(watson.NaturalLanguageUnderstandingV1.URL)
4379
.post('/v1/analyze?version=' + watson.NaturalLanguageUnderstandingV1.VERSION_DATE_2016_01_23)

0 commit comments

Comments
 (0)