From 621bb75341852746e60c7e36bf6dd9d15e9e40b2 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:27:34 -0800 Subject: [PATCH 1/3] Fix EG samples --- .../Azure.Messaging.EventGrid/README.md | 20 +++++++++-- .../samples/Sample1_PublishEventsToTopic.md | 22 +++++++++--- .../Sample1_SendEventsToTopicAndDomain.cs | 34 +++++++++++++------ .../Sample2_ParseAndDeserializeEvents.cs | 2 +- 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index bc4061412c43..8333d6bb717c 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -127,20 +127,34 @@ await client.SendEventsAsync(eventsList); ### Publish CloudEvents to an Event Grid Topic Publishing events to Event Grid is performed using the `EventGridPublisherClient`. Use the provided `SendEvents`/`SendEventsAsync` method to publish events to the topic. ```C# Snippet:SendCloudEventsToTopic +// Example of a custom ObjectSerializer used to serialize the event payload to JSON +var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), "example/binary")}; // Send the events diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md index 280601e0a41f..0a79ac23f89b 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md @@ -66,21 +66,35 @@ Following that, invoke `SendEvents` or `SendEventsAsync` to publish the events t Note on `CloudEvent`: each `CloudEvent` has a set of required, non-nullable properties. However, `Data` is *not required*. `Time` and `SpecVersion` are required properties that are set by default, but can also be manually set if needed. `Time` is also set by default, but not required. ```C# Snippet:SendCloudEventsToTopic +// Example of a custom ObjectSerializer used to serialize the event payload to JSON +var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), - "example/binary")}; + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 2e8c2e19e9af..737471e1fee8 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -90,37 +90,43 @@ public async Task SendCloudEventsToTopic() string topicEndpoint = TestEnvironment.CloudEventTopicHost; string topicAccessKey = TestEnvironment.CloudEventTopicKey; - // Example of a custom ObjectSerializer used to serialize the event payload to JSON - var myCustomDataSerializer = new JsonObjectSerializer( - new JsonSerializerOptions() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); - // Create the publisher client using an AzureKeyCredential // Custom topic should be configured to accept events of the CloudEvents 1.0 schema #region Snippet:CreateClientWithOptions - EventGridPublisherClient client = new EventGridPublisherClient( new Uri(topicEndpoint), new AzureKeyCredential(topicAccessKey)); #endregion #region Snippet:SendCloudEventsToTopic + // Example of a custom ObjectSerializer used to serialize the event payload to JSON + var myCustomDataSerializer = new JsonObjectSerializer( + new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + // Add CloudEvents to a list to publish to the topic List eventsList = new List { - // CloudEvent with populated data + // CloudEvent with custom model serialized to JSON new CloudEvent( "/cloudevents/example/source", "Example.EventType", - myCustomDataSerializer.Serialize("This is the event data")), + new CustomModel() { A = 5, B = true }), + + // CloudEvent with custom model serialized to JSON using a custom serializer + new CloudEvent( + "/cloudevents/example/source", + "Example.EventType", + myCustomDataSerializer.Serialize(new CustomModel() { A = 5, B = true }), + "application/json"), // CloudEvents also supports sending binary-valued data new CloudEvent( "/cloudevents/example/binarydata", "Example.EventType", - new BinaryData(Encoding.UTF8.GetBytes("This is binary data")), + new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), "example/binary")}; // Send the events @@ -162,5 +168,11 @@ public async Task SendEventsToDomain() await client.SendEventsAsync(eventsList); #endregion } + + internal class CustomModel + { + public int A { get; set; } + public bool B { get; set; } + } } } diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs index f4071a119079..7a672653259b 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample2_ParseAndDeserializeEvents.cs @@ -15,7 +15,7 @@ public partial class EventGridSamples : SamplesBase // Example JSON payloads private readonly string jsonPayloadSampleOne = "[{ \"id\": \"2d1781af-3a4c\", \"topic\": \"/examples/test/payload\", \"subject\": \"\", \"data\": { \"Name\": \"example\",\"Age\": 20 },\"eventType\": \"MyApp.Models.CustomEventType\",\"eventTime\": \"2018-01-25T22:12:19.4556811Z\",\"dataVersion\": \"1\"}]"; - private readonly string jsonPayloadSampleTwo = "[{ \"id\": \"2d1781af-3a4c\", \"source\": \"/examples/test/payload\", \"data\": { \"name\": \"example\",\"age\": 20 },\"type\": \"MyApp.Models.CustomEventType\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"specversion\": \"1\"}]"; + private readonly string jsonPayloadSampleTwo = "[{ \"id\": \"2d1781af-3a4c\", \"source\": \"/examples/test/payload\", \"data\": { \"name\": \"example\",\"age\": 20 },\"type\": \"MyApp.Models.CustomEventType\",\"time\": \"2018-01-25T22:12:19.4556811Z\",\"specversion\": \"1.0\"}]"; // This sample demonstrates how to parse EventGridEvents from JSON and access event data using AsSystemEventData() [Test] From cecc063d2c30600286da09cdc57c4efca9483c02 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:38:42 -0800 Subject: [PATCH 2/3] Fix content type --- sdk/eventgrid/Azure.Messaging.EventGrid/README.md | 2 +- .../tests/Samples/Sample1_SendEventsToTopicAndDomain.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md index 8333d6bb717c..f84574a8a980 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/README.md +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/README.md @@ -155,7 +155,7 @@ List eventsList = new List "/cloudevents/example/binarydata", "Example.EventType", new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), - "example/binary")}; + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 737471e1fee8..4651a3889210 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -127,7 +127,7 @@ public async Task SendCloudEventsToTopic() "/cloudevents/example/binarydata", "Example.EventType", new BinaryData(Encoding.UTF8.GetBytes("This is treated as binary data")), - "example/binary")}; + "application/octet-stream")}; // Send the events await client.SendEventsAsync(eventsList); From e2924c948ea7d0aae9545f8db9d27e61c1705a2a Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Wed, 24 Feb 2021 14:41:33 -0800 Subject: [PATCH 3/3] Remove usings --- .../tests/Samples/Sample1_SendEventsToTopicAndDomain.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs index 4651a3889210..9b909ea4bb5d 100644 --- a/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs +++ b/sdk/eventgrid/Azure.Messaging.EventGrid/tests/Samples/Sample1_SendEventsToTopicAndDomain.cs @@ -8,9 +8,6 @@ using System.Threading.Tasks; using Azure.Core.Serialization; using Azure.Core.TestFramework; -using Azure.Messaging; -using Azure.Messaging.EventGrid; -using Azure.Messaging.EventGrid.Tests; using NUnit.Framework; namespace Azure.Messaging.EventGrid.Tests.Samples