From d67a54a66ab713e448b5f2a9aaa118f32209b6a7 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 19 Jul 2014 00:22:58 +0100 Subject: [PATCH] Update HTTP Request node to new credentials api --- nodes/core/io/21-httpin.html | 77 ++++++++---------------------------- nodes/core/io/21-httpin.js | 47 ++++------------------ 2 files changed, 24 insertions(+), 100 deletions(-) diff --git a/nodes/core/io/21-httpin.html b/nodes/core/io/21-httpin.html index f4ebbbd88..a574fcfc7 100644 --- a/nodes/core/io/21-httpin.html +++ b/nodes/core/io/21-httpin.html @@ -103,12 +103,12 @@
- - + +
- - + +
@@ -206,6 +206,10 @@ //user -> credentials //pass -> credentials }, + credentials: { + user: {type:"text"}, + password: {type: "password"} + }, inputs:1, outputs:1, align: "right", @@ -217,70 +221,23 @@ return this.name?"node_label_italic":""; }, oneditprepare: function() { - $.getJSON('http-request/'+this.id,function(data) { - if (data.user) { - $('#node-input-useAuth').prop('checked', true); - $(".node-input-useAuth-row").show(); - $('#node-config-input-user').data("v",data.user); - $('#node-config-input-user').val(data.user); - } else { - $('#node-input-useAuth').prop('checked', false); - $(".node-input-useAuth-row").hide(); - $('#node-config-input-user').data("v",''); - } - if (data.hasPassword) { - $('#node-input-useAuth').prop('checked', true); - $(".node-input-useAuth-row").show(); - $('#node-config-input-pass').data("v",'__PWRD__'); - $('#node-config-input-pass').val('__PWRD__'); - } else { - $('#node-config-input-pass').data("v",''); - $('#node-config-input-pass').val(''); - } - - }); + if (this.credentials.user || this.credentials.haspassword) { + $('#node-input-useAuth').prop('checked', true); + $(".node-input-useAuth-row").show(); + } else { + $('#node-input-useAuth').prop('checked', false); + $(".node-input-useAuth-row").hide(); + } $("#node-input-useAuth").change(function() { if ($(this).is(":checked")) { $(".node-input-useAuth-row").show(); } else { $(".node-input-useAuth-row").hide(); + $('#node-input-user').val(''); + $('#node-input-password').val(''); } }); }, - oneditsave: function() { - var oldUser = $('#node-config-input-user').data("v"); - var oldPass = $('#node-config-input-pass').data("v"); - var newUser = $('#node-config-input-user').val(); - var newPass = $('#node-config-input-pass').val(); - - if (!$("#node-input-useAuth").is(":checked")) { - newUser = ""; - newPass = ""; - } - - if (oldUser != newUser || oldPass != newPass) { - if (newUser == "" && newPass == "") { - $.ajax({ - url: 'http-request/'+this.id, - type: 'DELETE', - success: function(result) {} - }); - } else { - var credentials = {}; - credentials.user = newUser; - if (newPass != '__PWRD__') { - credentials.password = newPass; - } - $.ajax({ - url: 'http-request/'+this.id, - type: 'POST', - data: credentials, - success:function(result){} - }); - } - return true; - } - } }); diff --git a/nodes/core/io/21-httpin.js b/nodes/core/io/21-httpin.js index 099e80e1b..87152fe11 100644 --- a/nodes/core/io/21-httpin.js +++ b/nodes/core/io/21-httpin.js @@ -149,7 +149,6 @@ module.exports = function(RED) { var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; var nodeMethod = n.method || "GET"; var node = this; - var credentials = RED.nodes.getCredentials(n.id); this.on("input",function(msg) { node.status({fill:"blue",shape:"dot",text:"requesting"}); var url; @@ -170,10 +169,9 @@ module.exports = function(RED) { opts.headers[v.toLowerCase()] = msg.headers[v]; } } - if (credentials) { - opts.auth = credentials.user+":"+(credentials.password||""); + if (this.credentials.user) { + opts.auth = this.credentials.user+":"+(this.credentials.password||""); } - var payload = null; if (msg.payload && (method == "POST" || method == "PUT") ) { @@ -221,42 +219,11 @@ module.exports = function(RED) { req.end(); }); } - RED.nodes.registerType("http request",HTTPRequest); - - RED.httpAdmin.get('/http-request/:id',function(req,res) { - var credentials = RED.nodes.getCredentials(req.params.id); - if (credentials) { - res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!=="")})); - } else { - res.send(JSON.stringify({})); + + RED.nodes.registerType("http request",HTTPRequest,{ + credentials: { + user: {type:"text"}, + password: {type: "password"} } }); - - RED.httpAdmin.delete('/http-request/:id',function(req,res) { - RED.nodes.deleteCredentials(req.params.id); - res.send(200); - }); - - RED.httpAdmin.post('/http-request/:id',function(req,res) { - var body = ""; - req.on('data', function(chunk) { - body+=chunk; - }); - req.on('end', function(){ - var newCreds = querystring.parse(body); - var credentials = RED.nodes.getCredentials(req.params.id)||{}; - if (newCreds.user == null || newCreds.user == "") { - delete credentials.user; - } else { - credentials.user = newCreds.user; - } - if (newCreds.password == "") { - delete credentials.password; - } else { - credentials.password = newCreds.password||credentials.password; - } - RED.nodes.addCredentials(req.params.id,credentials); - res.send(200); - }); - }); }