|
| 1 | +/** |
| 2 | + * Copyright 2013,2016 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +module.exports = function (RED) { |
| 18 | + var watson = require('watson-developer-cloud'); |
| 19 | + var cfenv = require('cfenv'); |
| 20 | + var toneutils = require('../../utilities/tone-utils'); |
| 21 | + |
| 22 | + // Require the Cloud Foundry Module to pull credentials from bound service |
| 23 | + // If they are found then they are stored in sUsername and sPassword, as the |
| 24 | + // service credentials. This separation from sUsername and username to allow |
| 25 | + // the end user to modify the node credentials when the service is not bound. |
| 26 | + // Otherwise, once set username would never get reset, resulting in a frustrated |
| 27 | + // user who, when he errenously enters bad credentials, can't figure out why |
| 28 | + // the edited ones are not being taken. |
| 29 | + |
| 30 | + // Not ever used, and codeacy complains about it. |
| 31 | + |
| 32 | + var username, password, sUsername, sPassword; |
| 33 | + |
| 34 | + var service = cfenv.getAppEnv().getServiceCreds(/tone analyzer/i) |
| 35 | + |
| 36 | + if (service) { |
| 37 | + sUsername = service.username; |
| 38 | + sPassword = service.password; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + // Node RED Admin - fetch and set vcap services |
| 43 | + RED.httpAdmin.get('/watson-tone-analyzer/vcap', function (req, res) { |
| 44 | + res.json(service ? {bound_service: true} : null); |
| 45 | + }); |
| 46 | + |
| 47 | + |
| 48 | + // Check that the credentials have been provided |
| 49 | + // Credentials are needed for each the service. |
| 50 | + var checkCreds = function(credentials) { |
| 51 | + var taSettings = null; |
| 52 | + |
| 53 | + username = sUsername || credentials.username; |
| 54 | + password = sPassword || credentials.password; |
| 55 | + |
| 56 | + if (username && password) { |
| 57 | + taSettings = {}; |
| 58 | + taSettings.username = username; |
| 59 | + taSettings.password = password; |
| 60 | + } |
| 61 | + |
| 62 | + return taSettings; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + // Function that checks the configuration to make sure that credentials, |
| 67 | + // payload and options have been provied in the correct format. |
| 68 | + var checkConfiguration = function(msg, node, cb) { |
| 69 | + var message = null; |
| 70 | + var taSettings = null; |
| 71 | + |
| 72 | + taSettings = checkCreds(node.credentials); |
| 73 | + |
| 74 | + if (!taSettings) { |
| 75 | + message = 'Missing Tone Analyzer service credentials'; |
| 76 | + } else if (msg.payload) { |
| 77 | + message = toneutils.checkPayload(msg.payload); |
| 78 | + } else { |
| 79 | + message = 'Missing property: msg.payload'; |
| 80 | + } |
| 81 | + |
| 82 | + if (cb) { |
| 83 | + cb(message, taSettings); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + |
| 88 | + // function when the node recieves input inside a flow. |
| 89 | + // Configuration is first checked before the service is invoked. |
| 90 | + var processOnInput = function(msg, config, node) { |
| 91 | + checkConfiguration (msg, node, function(err, settings){ |
| 92 | + if (err) { |
| 93 | + node.status({fill:'red', shape:'dot', text:err}); |
| 94 | + node.error(err, msg); |
| 95 | + return; |
| 96 | + } else { |
| 97 | + var tone_analyzer = watson.tone_analyzer({ |
| 98 | + 'username': settings.username, |
| 99 | + 'password': settings.password, |
| 100 | + 'version': 'v3', |
| 101 | + 'version_date': '2016-05-19' |
| 102 | + }); |
| 103 | + |
| 104 | + var options = toneutils.parseOptions(msg, config); |
| 105 | + |
| 106 | + node.status({fill:'blue', shape:'dot', text:'requesting'}); |
| 107 | + tone_analyzer.tone(options, function (err, response) { |
| 108 | + node.status({}) |
| 109 | + if (err) { |
| 110 | + node.error(err, msg); |
| 111 | + } else { |
| 112 | + msg.response = response; |
| 113 | + } |
| 114 | + node.send(msg); |
| 115 | + }); |
| 116 | + |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + }; |
| 121 | + |
| 122 | + |
| 123 | + // This is the Tone Analyzer Node. |
| 124 | + function Node (config) { |
| 125 | + RED.nodes.createNode(this, config); |
| 126 | + var node = this; |
| 127 | + |
| 128 | + // Invoked whenb the node has received an input as part of a flow. |
| 129 | + this.on('input', function (msg) { |
| 130 | + processOnInput(msg, config, node); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + RED.nodes.registerType('watson-tone-analyzer-v3', Node, { |
| 136 | + credentials: { |
| 137 | + username: {type:'text'}, |
| 138 | + password: {type:'password'} |
| 139 | + } |
| 140 | + }); |
| 141 | +}; |
0 commit comments