forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublishEvents.cs
46 lines (37 loc) · 1.91 KB
/
PublishEvents.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
using System.Text;
using System.Text.Json;
namespace MatchZy
{
public partial class MatchZy
{
public async Task SendEventAsync(MatchZyEvent @event)
{
try
{
if (string.IsNullOrEmpty(matchConfig.RemoteLogURL)) return;
Log($"[SendEventAsync] Sending Event: {@event.EventName} for matchId: {liveMatchId} mapNumber: {matchConfig.CurrentMapNumber} on {matchConfig.RemoteLogURL}");
using var httpClient = new HttpClient();
using var jsonContent = new StringContent(JsonSerializer.Serialize(@event, @event.GetType()), Encoding.UTF8, "application/json");
string jsonString = await jsonContent.ReadAsStringAsync();
Log($"[SendEventAsync] SENDING DATA: {jsonString}");
if (!string.IsNullOrEmpty(matchConfig.RemoteLogHeaderKey))
{
httpClient.DefaultRequestHeaders.Add(matchConfig.RemoteLogHeaderKey, matchConfig.RemoteLogHeaderValue);
}
var httpResponseMessage = await httpClient.PostAsync(matchConfig.RemoteLogURL, jsonContent);
if (httpResponseMessage.IsSuccessStatusCode)
{
Log($"[SendEventAsync] Sending {@event.EventName} for matchId: {liveMatchId} mapNumber: {matchConfig.CurrentMapNumber} successful with status code: {httpResponseMessage.StatusCode}");
}
else
{
Log($"[SendEventAsync] Sending {@event.EventName} for matchId: {liveMatchId} mapNumber: {matchConfig.CurrentMapNumber} failed with status code: {httpResponseMessage.StatusCode}, ResponseContent: {await httpResponseMessage.Content.ReadAsStringAsync()}");
}
}
catch (Exception e)
{
Log($"[SendEventAsync FATAL] An error occurred: {e.Message}");
}
}
}
}