Skip to content

Commit

Permalink
Changed function invokeWebhook to allow for additional settings (#487)
Browse files Browse the repository at this point in the history
* Changed function invokeWebhook to allow for additional settings

The invokeWebhook function was changed so that the following settings can be used in the settings.json file:
webhook_type - set the name of the entity that contains the type (defaults to type)
webhook_data - set the name of the entity that contains the data (defaults to data)
webhook_header_name - add a header (for instance for authorization) (defaults to none)
webhook_header_contents - set the contents of the additional header.

The reason for this change is that I want to point this webhook to Splunk. Splunk requires an event entity in the payload in stead of a data entity and the type entity is unknown as well. In addition, authentication is done through the header "Authentication".

I have programmed it such that it is backwards compatible with the previous version.

* Changed new property and variable names to camelCase
  • Loading branch information
dajomas authored and jishi committed May 7, 2017
1 parent a1817a1 commit 8067ca0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/sonos-http-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,32 @@ function HttpAPI(discovery, settings) {
}

function invokeWebhook(type, data) {
var typeName = "type";
var dataName = "data";
if (!settings.webhook) return;

if (settings.webhookType) { typeName = settings.webhookType; }
if (settings.webhookData) { dataName = settings.webhookData; }

const jsonBody = JSON.stringify({
type: type,
data: data
[typeName]: type,
[dataName]: data
});

const body = new Buffer(jsonBody, 'utf8');

var headers = {
'Content-Type': 'application/json',
'Content-Length': body.length
}
if (settings.webhookHeaderName && settings.webhookHeaderContents) {
headers[settings.webhookHeaderName] = settings.webhookHeaderContents;
}

request({
method: 'POST',
uri: settings.webhook,
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length
},
headers: headers,
body
})
.catch(function (err) {
Expand Down

0 comments on commit 8067ca0

Please sign in to comment.