Skip to content

Commit

Permalink
Merge pull request #410 from chughts/vrauth
Browse files Browse the repository at this point in the history
Visual Recognition Authentication
  • Loading branch information
chughts committed May 25, 2018
2 parents cf48783 + 42f56e0 commit 778ae06
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 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
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.6.13",
"version": "0.6.14",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"async": "^1.5.2",
Expand All @@ -9,13 +9,13 @@
"request": "~2.86.0",
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^3.3.0",
"image-type": "^2.0.2",
"watson-developer-cloud": "^3.4.2",
"kuromoji": "^0.1.1",
"word-count": "^0.2.2",
"is-docx": "^0.0.3",
"stream-to-array" : "^2.3.0",
"ws" : "^4.1.0"
"stream-to-array": "^2.3.0",
"ws": "^4.1.0"
},
"repository": {
"type": "git",
Expand All @@ -27,18 +27,19 @@
"bluemix",
"watson"
],
"contributors": [ {
"contributors": [
{
"name": "Soheel Chughtai",
"email": "[email protected]"
}
],
"node-red": {
"nodes": {
"watson-conversation-v1":"services/conversation/v1.js",
"watson-conversation-workspace-manager-v1":"services/conversation/v1-workspace-manager.js",
"watson-discovery-v1":"services/discovery/v1.js",
"watson-discovery-v1-document-loader":"services/discovery/v1-document-loader.js",
"watson-discovery-v1-query-builder":"services/discovery/v1-query-builder.js",
"watson-conversation-v1": "services/conversation/v1.js",
"watson-conversation-workspace-manager-v1": "services/conversation/v1-workspace-manager.js",
"watson-discovery-v1": "services/discovery/v1.js",
"watson-discovery-v1-document-loader": "services/discovery/v1-document-loader.js",
"watson-discovery-v1-query-builder": "services/discovery/v1-query-builder.js",
"watson-language-translator-v2": "services/language_translator/v2.js",
"watson-language-translator-identify-v2": "services/language_translator_identify/v2.js",
"watson-language-translator-util-v2": "services/language_translator_util/v2.js",
Expand Down
18 changes: 18 additions & 0 deletions services/visual_recognition/v3.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey" placeholder="API Key">
</div>
<div class="form-row">
<label for="node-input-vr-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
<select type="text" id="node-input-vr-service-endpoint" style="display: inline-block; width: 70%;" >
<option value="https://gateway-a.watsonplatform.net/visual-recognition/api">https://gateway-a.watsonplatform.net/visual-recognition/api</option>
<option value="https://gateway.watsonplatform.net/visual-recognition/api">https://gateway.watsonplatform.net/visual-recognition/api</option>
</select>
</div>

<div class="form-row">
<label for="node-input-image-feature"><i class="fa fa-book"></i> Detect: </label>
<select type="text" id="node-input-image-feature" style="display: inline-block; width: 70%;">
Expand Down Expand Up @@ -61,6 +69,14 @@
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey" placeholder="API Key">
</div>
<div class="form-row">
<label for="node-input-vr-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
<select type="text" id="node-input-vr-service-endpoint" style="display: inline-block; width: 70%;" >
<option value="https://gateway-a.watsonplatform.net/visual-recognition/api">https://gateway-a.watsonplatform.net/visual-recognition/api</option>
<option value="https://gateway.watsonplatform.net/visual-recognition/api">https://gateway.watsonplatform.net/visual-recognition/api</option>
</select>
</div>

<div class="form-row">
<label for="node-input-image-feature"><i class="fa fa-book"></i> Detect: </label>
<select type="text" id="node-input-image-feature" style="display: inline-block; width: 70%;">
Expand Down Expand Up @@ -148,6 +164,7 @@
defaults: {
name: {value: ""},
apikey: {value: ""},
'vr-service-endpoint' :{value: 'https://gateway-a.watsonplatform.net/visual-recognition/api'},
"image-feature": {value: ""},
"lang": {value: ""},
},
Expand Down Expand Up @@ -216,6 +233,7 @@
defaults: {
name: {value: ""},
apikey: {value: ""},
'vr-service-endpoint' :{value: 'https://gateway-a.watsonplatform.net/visual-recognition/api'},
"image-feature": {value: ""}
},
credentials: {
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 778ae06

Please sign in to comment.