Skip to content

Commit

Permalink
Implment switch between authentication mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
chughts committed May 24, 2018
1 parent 2bd3699 commit 42f56e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ Node-RED Watson Nodes for IBM Cloud

<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>

### New in version 0.6.14
- Visual Recognition instances created post May 22 2018, have a new authentication mechanism

### New in version 0.6.13
- Added opt-out option for collection parsing of strings in Natural Language Classifier Node.
- Added opt-out option for collection parsing of strings in Natural Language Classifier Node.

### New in version 0.6.12
- Fix to collection check in Natural Language Classification Node allowing for . in domain
Expand Down
38 changes: 36 additions & 2 deletions services/visual_recognition/v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ module.exports = function(RED) {
async = require('async'),
toArray = require('stream-to-array'),
sAPIKey = null,
service = null;
iamAPIKey = false,
service = null,
endpoint = '',
sEndpoint = '';

// temp is being used for file streaming to allow the file to arrive so it can be processed.
temp.track();
Expand All @@ -49,6 +52,7 @@ module.exports = function(RED) {

if (service) {
sAPIKey = service.api_key;
sEndpoint = service.url;
}

RED.httpAdmin.get('/watson-visual-recognition/vcap', function(req, res) {
Expand Down Expand Up @@ -154,13 +158,23 @@ module.exports = function(RED) {
}

var serviceSettings = {
api_key: node.apikey,
version_date: '2018-03-19',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
};

if (endpoint) {
serviceSettings.url = endpoint;
}

// VR instances created post 22 May 2018, are expecting an iam API Key
if (iamAPIKey) {
serviceSettings.iam_apikey = node.apikey;
} else {
serviceSettings.api_key = node.apikey;
}

// The change to watson-developer-cloud 3.0.x has resulted in a
// change in how the Accept-Language is specified. It now needs
// to go in as a header.
Expand Down Expand Up @@ -469,6 +483,7 @@ module.exports = function(RED) {
shape: 'dot',
text: 'Calling ' + feature + ' ...'
});

if (feature === 'classifyImage' || feature === 'detectFaces') {
return executeService(feature, params, node, msg);
//return Promise.resolve();
Expand All @@ -481,6 +496,22 @@ module.exports = function(RED) {
}
}

function determineEndpoint(config) {
// Any VR instances created post 22 May 2018, have a different endpoint
// and mechanism for authentication. This function detemines if this
// new authentication mechanism is being utlised.
iamAPIKey = false;

endpoint = sEndpoint;
if (!endpoint && config['vr-service-endpoint']) {
endpoint = config['vr-service-endpoint'];
}
if (endpoint && 'https://gateway.watsonplatform.net/visual-recognition/api' === endpoint) {
iamAPIKey = true;
}
return Promise.resolve();
}

// This is the Watson Visual Recognition V3 Node
function WatsonVisualRecognitionV3Node(config) {
var node = this,
Expand All @@ -506,6 +537,9 @@ module.exports = function(RED) {
.then(function() {
return verifyInputs(feature, msg);
})
.then(function() {
return determineEndpoint(config);
})
.then(function() {
return verifyServiceCredentials(node, msg);
})
Expand Down

0 comments on commit 42f56e0

Please sign in to comment.