Skip to content

Commit 8067ca0

Browse files
dajomasjishi
authored andcommitted
Changed function invokeWebhook to allow for additional settings (#487)
* 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
1 parent a1817a1 commit 8067ca0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/sonos-http-api.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,32 @@ function HttpAPI(discovery, settings) {
113113
}
114114

115115
function invokeWebhook(type, data) {
116+
var typeName = "type";
117+
var dataName = "data";
116118
if (!settings.webhook) return;
117119

120+
if (settings.webhookType) { typeName = settings.webhookType; }
121+
if (settings.webhookData) { dataName = settings.webhookData; }
122+
118123
const jsonBody = JSON.stringify({
119-
type: type,
120-
data: data
124+
[typeName]: type,
125+
[dataName]: data
121126
});
122127

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

130+
var headers = {
131+
'Content-Type': 'application/json',
132+
'Content-Length': body.length
133+
}
134+
if (settings.webhookHeaderName && settings.webhookHeaderContents) {
135+
headers[settings.webhookHeaderName] = settings.webhookHeaderContents;
136+
}
137+
125138
request({
126139
method: 'POST',
127140
uri: settings.webhook,
128-
headers: {
129-
'Content-Type': 'application/json',
130-
'Content-Length': body.length
131-
},
141+
headers: headers,
132142
body
133143
})
134144
.catch(function (err) {

0 commit comments

Comments
 (0)