-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
EventGridExtensions.cs
41 lines (39 loc) · 1.47 KB
/
EventGridExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text.Json;
using System.Threading;
using Azure.Core.Serialization;
using Azure.Messaging.EventGrid.Models;
namespace Azure.Messaging.EventGrid
{
/// <summary>
/// Extension methods for BinaryData to use for parsing JSON-encoded events.
/// </summary>
public static class EventGridExtensions
{
/// <summary>
/// Gets whether or not the event is a System defined event and returns the deserialized
/// system event data via out parameter.
/// </summary>
/// <param name="cloudEvent"></param>
/// <param name="eventData">If the event is a system event, this will be populated
/// with the deserialized system event data. Otherwise, this will be null.</param>
/// <returns> Whether or not the event is a system event.</returns>
public static bool TryGetSystemEventData(this CloudEvent cloudEvent, out object eventData)
{
BinaryData data = cloudEvent.Data;
try
{
JsonDocument requestDocument = JsonDocument.Parse(data.ToMemory());
eventData = SystemEventExtensions.AsSystemEventData(cloudEvent.Type, requestDocument.RootElement);
return eventData != null;
}
catch
{
eventData = null;
return false;
}
}
}
}