Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EG API Updates #18369

Merged
merged 5 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sdk/eventgrid/Azure.Messaging.EventGrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ foreach (CloudEvent cloudEvent in cloudEvents)
break;
case "MyApp.Models.CustomEventType":
// One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
TestPayload testPayload = await cloudEvent.GetDataAsync<TestPayload>(myCustomSerializer);
TestPayload testPayload = cloudEvent.GetData<TestPayload>();
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
Console.WriteLine(testPayload.Name);
break;
case SystemEventNames.StorageBlobDeleted:
Expand All @@ -198,9 +198,9 @@ Below is an example using the `IsSystemEvent` property along with `AsSystemEvent
foreach (EventGridEvent egEvent in egEvents)
{
// If the event is a system event, AsSystemEventData() should return the correct system event type
if (egEvent.IsSystemEvent)
if (egEvent.TryGetSystemEventData(out object systemEvent))
{
switch (egEvent.AsSystemEventData())
switch (systemEvent)
{
case SubscriptionValidationEventData subscriptionValidated:
Console.WriteLine(subscriptionValidated.ValidationCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ public static partial class BinaryDataExtensions
}
public partial class CloudEvent
{
public CloudEvent(string source, string type, object data, string dataContentType = null, System.Type dataSerializationType = null) { }
public CloudEvent(string source, string type, object data, System.Type dataSerializationType = null) { }
public CloudEvent(string source, string type, System.ReadOnlyMemory<byte> data, string dataContentType) { }
public string DataContentType { get { throw null; } set { } }
public string DataSchema { get { throw null; } set { } }
public System.Collections.Generic.Dictionary<string, object> ExtensionAttributes { get { throw null; } }
public string Id { get { throw null; } set { } }
public bool IsSystemEvent { get { throw null; } }
public string Source { get { throw null; } set { } }
public string Subject { get { throw null; } set { } }
public System.DateTimeOffset? Time { get { throw null; } set { } }
public string Type { get { throw null; } set { } }
public object AsSystemEventData() { throw null; }
public System.BinaryData GetData() { throw null; }
public System.Threading.Tasks.Task<System.BinaryData> GetDataAsync() { throw null; }
public System.Threading.Tasks.Task<T> GetDataAsync<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public T GetData<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Messaging.EventGrid.CloudEvent[] Parse(System.BinaryData requestContent) { throw null; }
public T GetData<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Messaging.EventGrid.CloudEvent[] Parse(string requestContent) { throw null; }
public bool TryGetSystemEvent(out object systemEvent) { throw null; }
}
public partial class EventGridEvent
{
Expand All @@ -32,16 +29,12 @@ public EventGridEvent(string subject, string eventType, string dataVersion, obje
public System.DateTimeOffset EventTime { get { throw null; } set { } }
public string EventType { get { throw null; } set { } }
public string Id { get { throw null; } set { } }
public bool IsSystemEvent { get { throw null; } }
public string Subject { get { throw null; } set { } }
public string Topic { get { throw null; } set { } }
public object AsSystemEventData() { throw null; }
public System.BinaryData GetData() { throw null; }
public System.Threading.Tasks.Task<System.BinaryData> GetDataAsync() { throw null; }
public System.Threading.Tasks.Task<T> GetDataAsync<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public T GetData<T>(Azure.Core.Serialization.ObjectSerializer serializer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(System.BinaryData requestContent) { throw null; }
public T GetData<T>(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static Azure.Messaging.EventGrid.EventGridEvent[] Parse(string requestContent) { throw null; }
public bool TryGetSystemEventData(out object eventData) { throw null; }
}
public partial class EventGridPublisherClient
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ EventGridPublisherClient client = new EventGridPublisherClient(
new AzureKeyCredential(topicAccessKey),
clientOptions);
```
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:
```C# Snippet:CreateWithSas
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.
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.
Generate the SAS:
```C# Snippet:GenerateSas
var builder = new EventGridSasBuilder(new Uri(topicEndpoint), DateTimeOffset.Now.AddHours(1));
var keyCredential = new AzureKeyCredential(topicAccessKey);
var sasCredential = new AzureSasCredential(builder.GenerateSas(keyCredential));
string sasToken = builder.GenerateSas(keyCredential);
```

Here is how it would be used from the consumer's perspective:
```C# Snippet:AuthenticateWithSas
var sasCredential = new AzureSasCredential(sasToken);
EventGridPublisherClient client = new EventGridPublisherClient(
new Uri(topicEndpoint),
sasCredential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ foreach (CloudEvent cloudEvent in cloudEvents)
break;
case "MyApp.Models.CustomEventType":
// One can also specify a custom ObjectSerializer as needed to deserialize the payload correctly
TestPayload testPayload = await cloudEvent.GetDataAsync<TestPayload>(myCustomSerializer);
TestPayload testPayload = cloudEvent.GetData<TestPayload>();
Console.WriteLine(testPayload.Name);
break;
case SystemEventNames.StorageBlobDeleted:
Expand All @@ -52,16 +52,16 @@ foreach (CloudEvent cloudEvent in cloudEvents)
}
```

### Using `AsSytemEventData()`
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.
### Using `TryGetSystemEventData()`
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. 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 after upgrading.

```C# Snippet:DeserializePayloadUsingAsSystemEventData
foreach (EventGridEvent egEvent in egEvents)
{
// If the event is a system event, AsSystemEventData() should return the correct system event type
if (egEvent.IsSystemEvent)
if (egEvent.TryGetSystemEventData(out object systemEvent))
{
switch (egEvent.AsSystemEventData())
switch (systemEvent)
{
case SubscriptionValidationEventData subscriptionValidated:
Console.WriteLine(subscriptionValidated.ValidationCode);
Expand Down
Loading