Skip to content

Instantly share code, notes, and snippets.

@dbarkol
Created May 6, 2018 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbarkol/4e01287e8377a6dca0601fa0244f9296 to your computer and use it in GitHub Desktop.
Save dbarkol/4e01287e8377a6dca0601fa0244f9296 to your computer and use it in GitHub Desktop.
public static class NewBand
{
[FunctionName("newband")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequest req,
TraceWriter log)
{
log.Info("newband function triggered");
var requestBody = new StreamReader(req.Body).ReadToEnd();
// Check the header for the event type.
if (!req.Headers.TryGetValue("Aeg-Event-Type", out var headerValues))
return new BadRequestObjectResult("Not a valid request");
var eventTypeHeaderValue = headerValues.FirstOrDefault();
if (eventTypeHeaderValue == "SubscriptionValidation")
{
// Subscription Validation event
// Echo back the validation code
// to confirm the subscription.
var events = JsonConvert.DeserializeObject<EventGridEvent[]>(requestBody);
dynamic data = events[0].Data;
var validationCode = data["validationCode"];
return new JsonResult(new
{
validationResponse = validationCode
});
}
else if (eventTypeHeaderValue == "Notification")
{
// Notification event
// Deserialize the cloud event
// and access the event data.
log.Info(requestBody);
var newBandEvent = JsonConvert.DeserializeObject<CloudEvent<Band>>(requestBody);
log.Info($"Welcome, {newBandEvent.Data.Name}!");
return new OkObjectResult("");
}
return new BadRequestObjectResult("Not a valid request");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment