Trying to cope with the different formats a webhook could retrieve the data

This commit is contained in:
Dominic Bosch 2014-04-24 09:38:23 +02:00
parent 5d1e4c4b61
commit ef0a105a08
2 changed files with 29 additions and 27 deletions

View file

@ -45,19 +45,23 @@ exports = module.exports = ( args ) =>
module.exports
indexEvent = ( event, body, resp ) ->
try
if typeof body is 'string'
obj = payload: JSON.parse body
try
obj = qs.parse body
catch err
try
obj = JSON.parse body
catch err
resp.send 400, 'Badly formed event!'
return
else
obj = payload: body
obj = body
timestamp = ( new Date() ).toISOString()
rand = ( Math.floor Math.random() * 10e9 ).toString( 16 ).toUpperCase()
obj.event = event
obj.eventid = "#{ obj.event }_UTC|#{ timestamp }_#{ rand }"
db.pushEvent obj
resp.send 200, "Thank you for the event: #{ obj.eventid }"
catch err
resp.send 400, 'Badly formed event!'
# Activate a webhook. the body will be JSON parsed, the name of the webhook will
# be the event name given to the event object, a timestamp will be added
@ -69,8 +73,6 @@ activateWebHook = ( app, name ) =>
body += data
req.on 'end', ->
console.log body
console.log typeof body
indexEvent name, body, resp
# This is a hack to quickly allow storing of public accessible data
if name is 'uptimestatistics'

View file

@ -50,26 +50,28 @@ HTTP Listener
indexEvent = function(event, body, resp) {
var err, obj, rand, timestamp;
try {
if (typeof body === 'string') {
obj = {
payload: JSON.parse(body)
};
} else {
obj = {
payload: body
};
if (typeof body === 'string') {
try {
obj = qs.parse(body);
} catch (_error) {
err = _error;
try {
obj = JSON.parse(body);
} catch (_error) {
err = _error;
resp.send(400, 'Badly formed event!');
return;
}
}
timestamp = (new Date()).toISOString();
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
obj.event = event;
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
db.pushEvent(obj);
return resp.send(200, "Thank you for the event: " + obj.eventid);
} catch (_error) {
err = _error;
return resp.send(400, 'Badly formed event!');
} else {
obj = body;
}
timestamp = (new Date()).toISOString();
rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase();
obj.event = event;
obj.eventid = "" + obj.event + "_UTC|" + timestamp + "_" + rand;
db.pushEvent(obj);
return resp.send(200, "Thank you for the event: " + obj.eventid);
};
activateWebHook = (function(_this) {
@ -83,8 +85,6 @@ HTTP Listener
});
return req.on('end', function() {
var fPath;
console.log(body);
console.log(typeof body);
indexEvent(name, body, resp);
if (name === 'uptimestatistics') {
fPath = path.resolve(__dirname, '..', 'webpages', 'public', 'data', 'histochart.json');