Last active
January 10, 2021 11:41
-
-
Save justinyoo/8282de7244bccca562cd508e64d89470 to your computer and use it in GitHub Desktop.
Dealing CloudEvents with Azure Functions for Azure EventGrid
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
dotnet add package Azure.Messaging.EventGrid --version 4.0.0-beta.4 |
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
az extension add -n eventgrid |
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
endpoint=$(az eventgrid topic show \ | |
-g <resource_group_name> \ | |
-n <eventgrid_topic_name> \ | |
--query "endpoint" -o tsv) |
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
id=$(az eventgrid topic show \ | |
-g <resource_group_name> \ | |
-n <eventgrid_topic_name> \ | |
--query "id" -o tsv) | |
key=$(az rest \ | |
-m POST \ | |
-u "https://management.azure.com$id/listKeys?api-version=2020-06-01" \ | |
--query "key1" -o tsv) |
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
var topicEndpoint = new Uri("<endpoint>"); | |
var credential = new AzureKeyCredential("<key>"); | |
var publisher = new EventGridPublisherClient(topicEndpoint, credential); |
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
var source = "<source>"; | |
var type = "<type>"; | |
var dataContentType = "application/cloudevents+json"; | |
var data = new MyData() { Hello = "World" }; | |
var @event = new CloudEvent(source, type, data, dataContentType); | |
var events = new List<CloudEvent>() { @event }; | |
await publisher.SendEventsAsync(events).ConfigureAwait(false); |
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
[FunctionName("HttpTrigger")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", "OPTIONS", Route = "handler")] HttpRequest req, | |
ILogger log) | |
{ | |
if (HttpMethods.IsOptions(req.Method)) | |
{ | |
string header = req.Headers["WebHook-Request-Origin"]; | |
req.HttpContext.Response.Headers.Add("Webhook-Allowed-Origin", header); | |
return new OkResult(); | |
} | |
... | |
} |
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
[FunctionName("HttpTrigger")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", "OPTIONS", Route = "handler")] HttpRequest req, | |
ILogger log) | |
{ | |
if (HttpMethods.IsOptions(req.Method)) | |
{ | |
... | |
} | |
using (var reader = new StreamReader(req.Body)) | |
{ | |
var payload = await reader.ReadToEndAsync().ConfigureAwait(false); | |
var events = CloudEvent.Parse(payload); | |
foreach (var @event in events) | |
{ | |
var timestamp = @event.Time; | |
var data = await @event.GetDataAsync<MyData>().ConfigureAwait(false); | |
... | |
} | |
} | |
return new OkResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment