Created
May 6, 2018 15:44
-
-
Save dbarkol/4e01287e8377a6dca0601fa0244f9296 to your computer and use it in GitHub Desktop.
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
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