Skip to content

Instantly share code, notes, and snippets.

@antstanley
Last active May 29, 2018 10:06
Show Gist options
  • Save antstanley/bd8efd19907bf7caf318d6fbe2d561fd to your computer and use it in GitHub Desktop.
Save antstanley/bd8efd19907bf7caf318d6fbe2d561fd to your computer and use it in GitHub Desktop.
publishing to EventGrid using REST endpoints and Request module
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