Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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