-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSendEventsController.cs
More file actions
44 lines (40 loc) · 1.59 KB
/
SendEventsController.cs
File metadata and controls
44 lines (40 loc) · 1.59 KB
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SharpEventGrid;
namespace SharpMappingOverrideSample {
public class SendEventsController : Controller {
private readonly HttpClient _client;
public SendEventsController(HttpClient client) {
_client = client;
}
[HttpGet("api/test/subscription")]
public async Task<IActionResult> TestSubscription() {
var response = await SendEvent(EventTypes.SubscriptionValidationEvent, new ValidationEvent { ValidationCode = "foo" });
return Ok(response);
}
[HttpGet("api/test/event")]
public async Task<IActionResult> TestEvent(string eventType, string message) {
var response = await SendEvent(eventType, message);
return Ok(response);
}
private async Task<string> SendEvent(string eventType, object data) {
var url = $"{Request.Scheme}://{Request.Host.Value}/api/events?{Request.QueryString}";
var item = new Event {
EventType = eventType,
Subject = "mySubject",
Data = data
};
var json = JsonConvert.SerializeObject(new List<Event> { item });
var response = await _client.PostAsync(url, new StringContent(json));
var body = await response.Content.ReadAsStringAsync();
if (String.IsNullOrEmpty(body)) {
body = "OK";
}
return body;
}
}
}