mirror of
https://github.com/bitwarden/server.git
synced 2025-04-18 03:28:15 -05:00

* [PM-17562] Refactor to Support Multiple Message Payloads * Change signature as per PR suggestion
31 lines
1.0 KiB
C#
31 lines
1.0 KiB
C#
using System.Net.Http.Json;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Settings;
|
|
|
|
namespace Bit.Core.Services;
|
|
|
|
public class WebhookEventHandler(
|
|
IHttpClientFactory httpClientFactory,
|
|
GlobalSettings globalSettings)
|
|
: IEventMessageHandler
|
|
{
|
|
private readonly HttpClient _httpClient = httpClientFactory.CreateClient(HttpClientName);
|
|
private readonly string _webhookUrl = globalSettings.EventLogging.WebhookUrl;
|
|
|
|
public const string HttpClientName = "WebhookEventHandlerHttpClient";
|
|
|
|
public async Task HandleEventAsync(EventMessage eventMessage)
|
|
{
|
|
var content = JsonContent.Create(eventMessage);
|
|
var response = await _httpClient.PostAsync(_webhookUrl, content);
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
|
|
public async Task HandleManyEventsAsync(IEnumerable<EventMessage> eventMessages)
|
|
{
|
|
var content = JsonContent.Create(eventMessages);
|
|
var response = await _httpClient.PostAsync(_webhookUrl, content);
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
}
|