Skip to content

Commit 51fbb23

Browse files
EG API Updates (Azure#18369)
* Update API based on review feedback * Update samples * fix * Update wording * Fix sample
1 parent ac31b7f commit 51fbb23

12 files changed

+723
-950
lines changed

sdk/eventgrid/Azure.Messaging.EventGrid/README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,12 @@ Using `CloudEvent`:
169169
// Parse the JSON payload into a list of events using CloudEvent.Parse
170170
CloudEvent[] cloudEvents = CloudEvent.Parse(jsonPayloadSampleTwo);
171171
```
172-
From here, one can access the event data by deserializing to a specific type using `GetData<T>()` and passing in a custom serializer if necessary. Below is an example calling `GetData<T>()` using CloudEvents. In order to deserialize to the correct type, the `EventType` property (`Type` for CloudEvents) helps distinguish between different events.
172+
From here, one can access the event data by deserializing to a specific type using `GetData<T>()`. Calling `GetData()` will either return the event data wrapped in `BinaryData`, which represents the serialized JSON event data as bytes.
173+
174+
Using `GetData<T>()`:
175+
176+
Below is an example calling `GetData<T>()` for CloudEvents. In order to deserialize to the correct type, the `EventType` property (`Type` for CloudEvents) helps distinguish between different events. Custom event data should be deserialized using the generic method `GetData<T>()`. There is also an overload for `GetData<T>()` that accepts a custom `ObjectSerializer` to deserialize the event data.
177+
173178
```C# Snippet:DeserializePayloadUsingGenericGetData
174179
foreach (CloudEvent cloudEvent in cloudEvents)
175180
{
@@ -182,7 +187,7 @@ foreach (CloudEvent cloudEvent in cloudEvents)
182187
break;
183188
case "MyApp.Models.CustomEventType":
184189
// One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
185-
TestPayload testPayload = await cloudEvent.GetDataAsync<TestPayload>(myCustomSerializer);
190+
TestPayload testPayload = cloudEvent.GetData().ToObject<TestPayload>(myCustomSerializer);
186191
Console.WriteLine(testPayload.Name);
187192
break;
188193
case SystemEventNames.StorageBlobDeleted:
@@ -193,14 +198,20 @@ foreach (CloudEvent cloudEvent in cloudEvents)
193198
}
194199
}
195200
```
196-
Below is an example using the `IsSystemEvent` property along with `AsSystemEventData()` to deserialize system events.
201+
202+
Using `TryGetSystemEventData()`:
203+
204+
If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null.
205+
206+
*As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData<T>` method so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).*
207+
197208
```C# Snippet:DeserializePayloadUsingAsSystemEventData
198209
foreach (EventGridEvent egEvent in egEvents)
199210
{
200-
// If the event is a system event, AsSystemEventData() should return the correct system event type
201-
if (egEvent.IsSystemEvent)
211+
// If the event is a system event, TryGetSystemEventData() will return the deserialized system event
212+
if (egEvent.TryGetSystemEventData(out object systemEvent))
202213
{
203-
switch (egEvent.AsSystemEventData())
214+
switch (systemEvent)
204215
{
205216
case SubscriptionValidationEventData subscriptionValidated:
206217
Console.WriteLine(subscriptionValidated.ValidationCode);

sdk/eventgrid/Azure.Messaging.EventGrid/api/Azure.Messaging.EventGrid.netstandard2.0.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
namespace Azure.Messaging.EventGrid
22
{
3-
public static partial class BinaryDataExtensions
4-
{
5-
public static Azure.Messaging.EventGrid.CloudEvent ToCloudEvent(this System.BinaryData binaryData) { throw null; }
6-
public static Azure.Messaging.EventGrid.EventGridEvent ToEventGridEvent(this System.BinaryData binaryData) { throw null; }
7-
}
83
public partial class CloudEvent
94
{
10-
public CloudEvent(string source, string type, object data, string dataContentType = null, System.Type dataSerializationType = null) { }
5+
public CloudEvent(string source, string type, object data, System.Type dataSerializationType = null) { }
6+
public CloudEvent(string source, string type, System.ReadOnlyMemory<byte> data, string dataContentType) { }
117
public string DataContentType { get { throw null; } set { } }
128
public string DataSchema { get { throw null; } set { } }
139
public System.Collections.Generic.Dictionary<string, object> ExtensionAttributes { get { throw null; } }
1410
public string Id { get { throw null; } set { } }
15-
public bool IsSystemEvent { get { throw null; } }
1611
public string Source { get { throw null; } set { } }
1712
public string Subject { get { throw null; } set { } }
1813
public System.DateTimeOffset? Time { get { throw null; } set { } }
1914
public string Type { get { throw null; } set { } }
20-
public object AsSystemEventData() { throw null; }
2115
public System.BinaryData GetData() { throw null; }
22-
public System.Threading.Tasks.Task<System.BinaryData> GetDataAsync() { throw null; }
23-
public System.Threading.Tasks.Task<T> GetDataAsync<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
24-
public T GetData<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
25-
public static Azure.Messaging.EventGrid.CloudEvent[] Parse(System.BinaryData requestContent) { throw null; }
16+
public T GetData<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
2617
public static Azure.Messaging.EventGrid.CloudEvent[] Parse(string requestContent) { throw null; }
18+
public bool TryGetSystemEventData(out object eventData) { throw null; }
2719
}
2820
public partial class EventGridEvent
2921
{
@@ -32,16 +24,17 @@ public EventGridEvent(string subject, string eventType, string dataVersion, obje
3224
public System.DateTimeOffset EventTime { get { throw null; } set { } }
3325
public string EventType { get { throw null; } set { } }
3426
public string Id { get { throw null; } set { } }
35-
public bool IsSystemEvent { get { throw null; } }
3627
public string Subject { get { throw null; } set { } }
3728
public string Topic { get { throw null; } set { } }
38-
public object AsSystemEventData() { throw null; }
3929
public System.BinaryData GetData() { throw null; }
40-
public System.Threading.Tasks.Task<System.BinaryData> GetDataAsync() { throw null; }
41-
public System.Threading.Tasks.Task<T> GetDataAsync<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
42-
public T GetData<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
43-
public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(System.BinaryData requestContent) { throw null; }
30+
public T GetData<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
4431
public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(string requestContent) { throw null; }
32+
public bool TryGetSystemEventData(out object eventData) { throw null; }
33+
}
34+
public static partial class EventGridExtensions
35+
{
36+
public static Azure.Messaging.EventGrid.CloudEvent ToCloudEvent(this System.BinaryData binaryData) { throw null; }
37+
public static Azure.Messaging.EventGrid.EventGridEvent ToEventGridEvent(this System.BinaryData binaryData) { throw null; }
4538
}
4639
public partial class EventGridPublisherClient
4740
{

sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample1_PublishEventsToTopic.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ EventGridPublisherClient client = new EventGridPublisherClient(
2424
new AzureKeyCredential(topicAccessKey),
2525
clientOptions);
2626
```
27-
Event Grid also supports authenticating with a shared access signature which allows for providing access to a resource that expires by a certain time without sharing your access key:
28-
```C# Snippet:CreateWithSas
27+
Event Grid also supports authenticating with a shared access signature which allows for providing access to a resource that expires by a certain time without sharing your access key.
28+
Generally, the workflow would be that one application would generate the SAS string and hand off the string to another application that would consume the string.
29+
Generate the SAS:
30+
```C# Snippet:GenerateSas
2931
var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1));
3032
var keyCredential = new AzureKeyCredential(topicAccessKey);
31-
var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential));
33+
string sasToken = builder.GenerateSas(keyCredential);
34+
```
35+
36+
Here is how it would be used from the consumer's perspective:
37+
```C# Snippet:AuthenticateWithSas
38+
var sasCredential = new AzureSasCredential(sasToken);
3239
EventGridPublisherClient client = new EventGridPublisherClient(
3340
new Uri(topicEndpoint),
3441
sasCredential);

sdk/eventgrid/Azure.Messaging.EventGrid/samples/Sample3_ParseAndDeserializeEvents.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ foreach (CloudEvent cloudEvent in cloudEvents)
4040
break;
4141
case "MyApp.Models.CustomEventType":
4242
// One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
43-
TestPayload testPayload = await cloudEvent.GetDataAsync<TestPayload>(myCustomSerializer);
43+
TestPayload testPayload = cloudEvent.GetData().ToObject<TestPayload>(myCustomSerializer);
4444
Console.WriteLine(testPayload.Name);
4545
break;
4646
case SystemEventNames.StorageBlobDeleted:
@@ -52,16 +52,18 @@ foreach (CloudEvent cloudEvent in cloudEvents)
5252
}
5353
```
5454

55-
### Using `AsSytemEventData()`
56-
If expecting mostly system events, it may be cleaner to switch on `AsSytemEventData()` and use pattern matching to act on the individual events. In the case where there are unrecognized event types, one can use the returned `BinaryData` to examine the event data.
55+
### Using `TryGetSystemEventData()`
56+
If expecting mostly system events, it may be cleaner to switch on `TryGetSystemEventData()` and use pattern matching to act on the individual events. If an event is not a system event, the method will return false and the out parameter will be null.
57+
58+
*As a caveat, if you are using a custom event type with an EventType value that later gets added as a system event by the service and SDK, the return value of `TryGetSystemEventData` would change from `false` to `true`. This could come up if you are pre-emptively creating your own custom events for events that are already being sent by the service, but have not yet been added to the SDK. In this case, it is better to use the generic `GetData<T>` method so that your code flow doesn't change automatically after upgrading (of course, you may still want to modify your code to consume the newly released system event model as opposed to your custom model).*
5759

5860
```C# Snippet:DeserializePayloadUsingAsSystemEventData
5961
foreach (EventGridEvent egEvent in egEvents)
6062
{
61-
// If the event is a system event, AsSystemEventData() should return the correct system event type
62-
if (egEvent.IsSystemEvent)
63+
// If the event is a system event, TryGetSystemEventData() will return the deserialized system event
64+
if (egEvent.TryGetSystemEventData(out object systemEvent))
6365
{
64-
switch (egEvent.AsSystemEventData())
66+
switch (systemEvent)
6567
{
6668
case SubscriptionValidationEventData subscriptionValidated:
6769
Console.WriteLine(subscriptionValidated.ValidationCode);

0 commit comments

Comments
 (0)