-
Notifications
You must be signed in to change notification settings - Fork 193
/
UpdatesController.cs
178 lines (149 loc) · 5.67 KB
/
UpdatesController.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Mvc;
using viewer.Hubs;
using viewer.Models;
namespace viewer.Controllers
{
[Route("api/[controller]")]
public class UpdatesController : Controller
{
#region Data Members
private bool EventTypeSubcriptionValidation
=> HttpContext.Request.Headers["aeg-event-type"].FirstOrDefault() ==
"SubscriptionValidation";
private bool EventTypeNotification
=> HttpContext.Request.Headers["aeg-event-type"].FirstOrDefault() ==
"Notification";
private readonly IHubContext<GridEventsHub> _hubContext;
#endregion
#region Constructors
public UpdatesController(IHubContext<GridEventsHub> gridEventsHubContext)
{
this._hubContext = gridEventsHubContext;
}
#endregion
#region Public Methods
[HttpOptions]
public async Task<IActionResult> Options()
{
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var webhookRequestOrigin = HttpContext.Request.Headers["WebHook-Request-Origin"].FirstOrDefault();
var webhookRequestCallback = HttpContext.Request.Headers["WebHook-Request-Callback"];
var webhookRequestRate = HttpContext.Request.Headers["WebHook-Request-Rate"];
HttpContext.Response.Headers.Add("WebHook-Allowed-Rate", "*");
HttpContext.Response.Headers.Add("WebHook-Allowed-Origin", webhookRequestOrigin);
}
return Ok();
}
[HttpPost]
public async Task<IActionResult> Post()
{
using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var jsonContent = await reader.ReadToEndAsync();
// Check the event type.
// Return the validation code if it's
// a subscription validation request.
if (EventTypeSubcriptionValidation)
{
return await HandleValidation(jsonContent);
}
else if (EventTypeNotification)
{
// Check to see if this is passed in using
// the CloudEvents schema
if (IsCloudEvent(jsonContent))
{
return await HandleCloudEvent(jsonContent);
}
return await HandleGridEvents(jsonContent);
}
return BadRequest();
}
}
#endregion
#region Private Methods
private async Task<JsonResult> HandleValidation(string jsonContent)
{
var gridEvent =
JsonConvert.DeserializeObject<List<GridEvent<Dictionary<string, string>>>>(jsonContent)
.First();
await this._hubContext.Clients.All.SendAsync(
"gridupdate",
gridEvent.Id,
gridEvent.EventType,
gridEvent.Subject,
gridEvent.EventTime.ToLongTimeString(),
jsonContent.ToString());
// Retrieve the validation code and echo back.
var validationCode = gridEvent.Data["validationCode"];
return new JsonResult(new
{
validationResponse = validationCode
});
}
private async Task<IActionResult> HandleGridEvents(string jsonContent)
{
var events = JArray.Parse(jsonContent);
foreach (var e in events)
{
// Invoke a method on the clients for
// an event grid notiification.
var details = JsonConvert.DeserializeObject<GridEvent<dynamic>>(e.ToString());
await this._hubContext.Clients.All.SendAsync(
"gridupdate",
details.Id,
details.EventType,
details.Subject,
details.EventTime.ToLongTimeString(),
e.ToString());
}
return Ok();
}
private async Task<IActionResult> HandleCloudEvent(string jsonContent)
{
var details = JsonConvert.DeserializeObject<CloudEvent<dynamic>>(jsonContent);
var eventData = JObject.Parse(jsonContent);
await this._hubContext.Clients.All.SendAsync(
"gridupdate",
details.Id,
details.Type,
details.Subject,
details.Time,
eventData.ToString()
);
return Ok();
}
private static bool IsCloudEvent(string jsonContent)
{
// Cloud events are sent one at a time, while Grid events
// are sent in an array. As a result, the JObject.Parse will
// fail for Grid events.
try
{
// Attempt to read one JSON object.
var eventData = JObject.Parse(jsonContent);
// Check for the spec version property.
var version = eventData["specversion"].Value<string>();
if (!string.IsNullOrEmpty(version)) return true;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return false;
}
#endregion
}
}