BililiveRecorder/BililiveRecorder.Core/SimpleWebhook/BasicWebhookV2.cs

84 lines
3.2 KiB
C#
Raw Normal View History

2021-02-23 18:03:37 +08:00
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
2021-12-19 21:10:34 +08:00
using BililiveRecorder.Core.Config.V3;
2021-02-23 18:03:37 +08:00
using BililiveRecorder.Core.Event;
using Newtonsoft.Json;
using Serilog;
namespace BililiveRecorder.Core.SimpleWebhook
{
internal class BasicWebhookV2
2021-02-23 18:03:37 +08:00
{
private static readonly ILogger logger = Log.ForContext<BasicWebhookV2>();
private readonly HttpClient client;
private readonly GlobalConfig config;
public BasicWebhookV2(GlobalConfig config)
{
this.config = config ?? throw new ArgumentNullException(nameof(config));
this.client = new HttpClient();
2021-02-26 21:57:10 +08:00
this.client.DefaultRequestHeaders.Add("User-Agent", $"BililiveRecorder/{GitVersionInformation.FullSemVer}");
2021-02-23 18:03:37 +08:00
}
public Task SendSessionStartedAsync(RecordSessionStartedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordSessionStartedEventArgs>(args) { EventType = EventType.SessionStarted });
public Task SendSessionEndedAsync(RecordSessionEndedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordSessionEndedEventArgs>(args) { EventType = EventType.SessionEnded });
public Task SendFileOpeningAsync(RecordFileOpeningEventArgs args) =>
this.SendAsync(new EventWrapper<RecordFileOpeningEventArgs>(args) { EventType = EventType.FileOpening });
public Task SendFileClosedAsync(RecordFileClosedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordFileClosedEventArgs>(args) { EventType = EventType.FileClosed });
private async Task SendAsync(object data)
{
var urls = this.config.WebHookUrlsV2;
2022-05-02 23:26:43 +08:00
if (string.IsNullOrWhiteSpace(urls))
return;
2021-02-23 18:03:37 +08:00
var dataStr = JsonConvert.SerializeObject(data, Formatting.None);
2022-05-02 23:26:43 +08:00
logger.Debug("尝试发送 WebhookV2 数据 {WebhookData}, 地址 {Urls}", dataStr, urls);
var bytes = Encoding.UTF8.GetBytes(dataStr);
2021-02-23 18:03:37 +08:00
var tasks = urls!
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
2022-05-02 23:26:43 +08:00
.Select(x => this.SendImplAsync(x, bytes));
2021-02-23 18:03:37 +08:00
await Task.WhenAll(tasks).ConfigureAwait(false);
}
2022-05-02 23:26:43 +08:00
private async Task SendImplAsync(string url, byte[] data)
2021-02-23 18:03:37 +08:00
{
for (var i = 0; i < 3; i++)
try
{
2022-05-02 23:26:43 +08:00
if (i > 0)
await Task.Delay(i * 1000);
using var body = new ByteArrayContent(data);
body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var result = await this.client.PostAsync(url, body).ConfigureAwait(false);
2021-02-23 18:03:37 +08:00
result.EnsureSuccessStatusCode();
2022-05-02 23:26:43 +08:00
logger.Debug("发送 WebhookV2 到 {Url} 成功", url);
2021-02-23 18:03:37 +08:00
return;
}
catch (Exception ex)
{
2022-05-02 23:26:43 +08:00
logger.Warning(ex, "发送 WebhookV2 到 {Url} 失败", url);
2021-02-23 18:03:37 +08:00
}
}
}
}