-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendToEventGrid.cs
69 lines (61 loc) · 2.44 KB
/
SendToEventGrid.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using TollBooth.Models;
namespace TollBooth
{
public class SendToEventGrid
{
private readonly HttpClient _client;
private readonly ILogger _log;
public SendToEventGrid(ILogger log, HttpClient client)
{
_log = log;
_client = client;
}
public async Task SendLicensePlateData(LicensePlateData data)
{
// Will send to one of two routes, depending on success.
// Event listeners will filter and act on events they need to
// process (save to database, move to manual checkup queue, etc.)
if (data.LicensePlateFound)
{
// TODO 3: Modify send method to include the proper eventType name value for saving plate data.
await Send("savePlateData", "TollBooth/CustomerService", data);
}
else
{
// TODO 4: Modify send method to include the proper eventType name value for queuing plate for manual review.
await Send("queuePlateForManualCheckup", "TollBooth/CustomerService", data);
}
}
private async Task Send(string eventType, string subject, LicensePlateData data)
{
// Get the API URL and the API key from settings.
var uri = Environment.GetEnvironmentVariable("eventGridTopicEndpoint");
var key = Environment.GetEnvironmentVariable("eventGridTopicKey");
_log.LogInformation($"Sending license plate data to the {eventType} Event Grid type");
var events = new List<Event<LicensePlateData>>
{
new Event<LicensePlateData>()
{
Data = data,
EventTime = DateTime.UtcNow,
EventType = eventType,
Id = Guid.NewGuid().ToString(),
Subject = subject
}
};
_client.DefaultRequestHeaders.Clear();
_client.DefaultRequestHeaders.Add("aeg-sas-key", key);
await _client.PostAsJsonAsync(uri, events);
_log.LogInformation($"Sent the following to the Event Grid topic: {events[0]}");
}
}
}