Skip to content
This repository was archived by the owner on Jun 15, 2026. It is now read-only.

Commit 1d3f41f

Browse files
authored
Merge pull request #6 from Azure-Samples/cloudevents
Cloudevents
2 parents 14c44ec + a3ed88e commit 1d3f41f

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

viewer/Controllers/UpdatesController.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public UpdatesController(IHubContext<GridEventsHub> gridEventsHubContext)
4343

4444
#region Public Methods
4545

46+
[HttpOptions]
47+
public async Task<IActionResult> Options()
48+
{
49+
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
50+
{
51+
var webhookRequestOrigin = HttpContext.Request.Headers["WebHook-Request-Origin"].FirstOrDefault();
52+
var webhookRequestCallback = HttpContext.Request.Headers["WebHook-Request-Callback"];
53+
var webhookRequestRate = HttpContext.Request.Headers["WebHook-Request-Rate"];
54+
HttpContext.Response.Headers.Add("WebHook-Allowed-Rate", "*");
55+
HttpContext.Response.Headers.Add("WebHook-Allowed-Origin", webhookRequestOrigin);
56+
}
57+
58+
return Ok();
59+
}
60+
4661
[HttpPost]
4762
public async Task<IActionResult> Post()
4863
{
@@ -122,16 +137,15 @@ await this._hubContext.Clients.All.SendAsync(
122137
private async Task<IActionResult> HandleCloudEvent(string jsonContent)
123138
{
124139
var details = JsonConvert.DeserializeObject<CloudEvent<dynamic>>(jsonContent);
140+
var eventData = JObject.Parse(jsonContent);
125141

126-
// CloudEvents schema and mapping to
127-
// Event Grid: https://docs.microsoft.com/en-us/azure/event-grid/cloudevents-schema
128142
await this._hubContext.Clients.All.SendAsync(
129143
"gridupdate",
130-
details.EventId,
131-
details.EventType,
132-
details.Source,
133-
details.EventTime,
134-
jsonContent
144+
details.Id,
145+
details.Type,
146+
details.Subject,
147+
details.Time,
148+
eventData.ToString()
135149
);
136150

137151
return Ok();
@@ -147,8 +161,8 @@ private static bool IsCloudEvent(string jsonContent)
147161
// Attempt to read one JSON object.
148162
var eventData = JObject.Parse(jsonContent);
149163

150-
// Check for the cloud events version property.
151-
var version = eventData["cloudEventsVersion"].Value<string>();
164+
// Check for the spec version property.
165+
var version = eventData["specversion"].Value<string>();
152166
if (!string.IsNullOrEmpty(version)) return true;
153167
}
154168
catch (Exception e)

viewer/Models/CloudEvent.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33

44
namespace viewer.Models
55
{
6+
// Reference: https://github.com/cloudevents/spec/tree/v1.0-rc1
7+
68
public class CloudEvent<T> where T : class
79
{
8-
[JsonProperty("eventID")]
9-
public string EventId { get; set; }
10-
11-
[JsonProperty("cloudEventsVersion")]
12-
public string CloudEventVersion { get; set; }
13-
14-
[JsonProperty("eventType")]
15-
public string EventType { get; set; }
10+
[JsonProperty("specversion")]
11+
public string SpecVersion { get; set; }
1612

17-
[JsonProperty("eventTypeVersion")]
18-
public string EventTypeVersion { get; set; }
13+
[JsonProperty("type")]
14+
public string Type { get; set; }
1915

2016
[JsonProperty("source")]
2117
public string Source { get; set; }
2218

23-
[JsonProperty("eventTime")]
24-
public string EventTime { get; set; }
19+
[JsonProperty("subject")]
20+
public string Subject { get; set; }
21+
22+
[JsonProperty("id")]
23+
public string Id { get; set; }
24+
25+
[JsonProperty("time")]
26+
public string Time { get; set; }
2527

2628
[JsonProperty("data")]
2729
public T Data { get; set; }

0 commit comments

Comments
 (0)