BililiveRecorder/BililiveRecorder.Core/SimpleWebhook/BasicWebhookV1.cs
2021-02-26 21:57:10 +08:00

64 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using BililiveRecorder.Core.Config.V2;
using Newtonsoft.Json;
using Serilog;
namespace BililiveRecorder.Core.SimpleWebhook
{
public class BasicWebhookV1
{
private static readonly ILogger logger = Log.ForContext<BasicWebhookV1>();
private static readonly HttpClient client;
private readonly ConfigV2 Config;
static BasicWebhookV1()
{
client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", $"BililiveRecorder/{GitVersionInformation.FullSemVer}");
}
public BasicWebhookV1(ConfigV2 config)
{
this.Config = config ?? throw new ArgumentNullException(nameof(config));
}
public async Task SendAsync(RecordEndData data)
{
var urls = this.Config.Global.WebHookUrls;
if (string.IsNullOrWhiteSpace(urls)) return;
var dataStr = JsonConvert.SerializeObject(data, Formatting.None);
using var body = new ByteArrayContent(Encoding.UTF8.GetBytes(dataStr));
body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var tasks = urls!
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => this.SendImplAsync(x, body));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
private async Task SendImplAsync(string url, HttpContent data)
{
for (var i = 0; i < 3; i++)
try
{
var result = await client.PostAsync(url, data).ConfigureAwait(false);
result.EnsureSuccessStatusCode();
return;
}
catch (Exception ex)
{
logger.Warning(ex, <>é€<C3A9> Webhook 到 {Url} 失败", url);
}
}
}
}