| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Intentum.Events; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Dispatches intent events to configured webhook URLs via HTTP POST with retry. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class WebhookIntentEventHandler : IIntentEventHandler |
| | | 12 | | { |
| | | 13 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 14 | | private readonly IntentumEventsOptions _options; |
| | 1 | 15 | | private static readonly JsonSerializerOptions JsonOptions = new() |
| | 1 | 16 | | { |
| | 1 | 17 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 1 | 18 | | WriteIndented = false |
| | 1 | 19 | | }; |
| | | 20 | | |
| | 6 | 21 | | public WebhookIntentEventHandler(IHttpClientFactory httpClientFactory, IOptions<IntentumEventsOptions>? options) |
| | 6 | 22 | | { |
| | 6 | 23 | | _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); |
| | 6 | 24 | | _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 6 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task HandleAsync( |
| | | 29 | | IntentEventPayload payload, |
| | | 30 | | IntentumEventType eventType, |
| | | 31 | | CancellationToken cancellationToken = default) |
| | 5 | 32 | | { |
| | 5 | 33 | | var eventTypeName = eventType.ToString(); |
| | 5 | 34 | | var webhooks = _options.Webhooks |
| | 6 | 35 | | .Where(w => w.EventTypes.Contains(eventTypeName)) |
| | 5 | 36 | | .ToList(); |
| | | 37 | | |
| | 25 | 38 | | foreach (var webhook in webhooks) |
| | 5 | 39 | | { |
| | 5 | 40 | | await SendWithRetryAsync(webhook.Url, payload, eventTypeName, cancellationToken).ConfigureAwait(false); |
| | 5 | 41 | | } |
| | 5 | 42 | | } |
| | | 43 | | |
| | | 44 | | private async Task SendWithRetryAsync( |
| | | 45 | | string url, |
| | | 46 | | IntentEventPayload payload, |
| | | 47 | | string eventType, |
| | | 48 | | CancellationToken cancellationToken) |
| | 5 | 49 | | { |
| | 5 | 50 | | var client = _httpClientFactory.CreateClient(IntentumEventsOptions.HttpClientName); |
| | 5 | 51 | | var dto = new WebhookPayloadDto( |
| | 5 | 52 | | payload.BehaviorSpaceId, |
| | 5 | 53 | | payload.Intent.Name, |
| | 5 | 54 | | payload.Intent.Confidence.Level, |
| | 5 | 55 | | payload.Intent.Confidence.Score, |
| | 5 | 56 | | payload.Decision.ToString(), |
| | 5 | 57 | | payload.RecordedAt, |
| | 5 | 58 | | eventType); |
| | | 59 | | |
| | 5 | 60 | | var maxAttempts = Math.Max(1, _options.RetryCount + 1); |
| | 10 | 61 | | for (var attempt = 0; attempt < maxAttempts; attempt++) |
| | 5 | 62 | | { |
| | | 63 | | try |
| | 5 | 64 | | { |
| | 5 | 65 | | var response = await client.PostAsJsonAsync(url, dto, JsonOptions, cancellationToken).ConfigureAwait(fal |
| | 5 | 66 | | response.EnsureSuccessStatusCode(); |
| | 5 | 67 | | return; |
| | | 68 | | } |
| | 0 | 69 | | catch (HttpRequestException) when (attempt < maxAttempts - 1) |
| | 0 | 70 | | { |
| | 0 | 71 | | await Task.Delay(TimeSpan.FromSeconds(Math.Pow(2, attempt)), cancellationToken).ConfigureAwait(false); |
| | 0 | 72 | | } |
| | 0 | 73 | | } |
| | 5 | 74 | | } |
| | | 75 | | |
| | | 76 | | [UsedImplicitly] |
| | 5 | 77 | | private sealed record WebhookPayloadDto( |
| | 2 | 78 | | string? BehaviorSpaceId, |
| | 2 | 79 | | string IntentName, |
| | 2 | 80 | | string ConfidenceLevel, |
| | 2 | 81 | | double ConfidenceScore, |
| | 2 | 82 | | string Decision, |
| | 2 | 83 | | DateTimeOffset RecordedAt, |
| | 7 | 84 | | string EventType); |
| | | 85 | | } |