Last active
May 29, 2018 10:06
publishing to EventGrid using REST endpoints and Request module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uuid = require("uuid").v4; | |
const topicCreds = "mytopickey"; | |
const topicEndpoint = "mytopic.westeurope-1.eventgrid.azure.net"; | |
const request = require('request'); | |
function publishEvent(topicURL, topicSecret, eventPayload, callback) { | |
const options = { | |
method: 'POST', | |
url: topicURL, | |
headers: { | |
'aeg-sas-key': topicSecret | |
}, | |
json: true, | |
body: [eventPayload] | |
} | |
request(options, (err, response, body) => { | |
if (err) { | |
callback(err) | |
} else { | |
callback(null, true); | |
} | |
}) | |
} | |
module.exports = function(context, req) { | |
context.log("JavaScript HTTP trigger function processed a request."); | |
let events = [ | |
{ | |
id: uuid(), | |
subject: "TestSubject", | |
dataVersion: "1.0", | |
eventType: "Microsoft.MockPublisher.TestEvent", | |
eventTime: new Date(), | |
data: { | |
field1: "value1", | |
filed2: "value2" | |
} | |
} | |
]; | |
publishEvent(topicEndpoint, topicCreds, events, (err,response)=> { | |
if (!err) { | |
context.res = { | |
body: "Success " + result | |
}; | |
context.done(); | |
} else { | |
context.log("An error ocurred. " + err); | |
context.res = { | |
body: "Failure " + err | |
}; | |
context.done(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment