| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Intentum.Events; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods for registering Intentum events with dependency injection. |
| | | 8 | | /// </summary> |
| | | 9 | | [UsedImplicitly] |
| | | 10 | | public static class EventsExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds Intentum event handling (webhooks) to the service collection. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="services">The service collection.</param> |
| | | 16 | | /// <param name="configure">Action to configure webhooks and retry.</param> |
| | | 17 | | /// <returns>The service collection for chaining.</returns> |
| | | 18 | | [UsedImplicitly] |
| | | 19 | | public static IServiceCollection AddIntentumEvents( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | Action<IntentumEventsOptions>? configure = null) |
| | 1 | 22 | | { |
| | 1 | 23 | | services.AddOptions<IntentumEventsOptions>(); |
| | 1 | 24 | | if (configure != null) |
| | 1 | 25 | | services.Configure(configure); |
| | | 26 | | |
| | 1 | 27 | | services.AddHttpClient(IntentumEventsOptions.HttpClientName); |
| | | 28 | | |
| | 1 | 29 | | services.AddScoped<IIntentEventHandler, WebhookIntentEventHandler>(); |
| | 1 | 30 | | return services; |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Adds a webhook to the events options. Call from within Configure(IntentumEventsOptions). |
| | | 35 | | /// </summary> |
| | | 36 | | [UsedImplicitly] |
| | | 37 | | public static IntentumEventsOptions AddWebhook( |
| | | 38 | | this IntentumEventsOptions options, |
| | | 39 | | string url, |
| | | 40 | | IEnumerable<string>? events = null) |
| | 9 | 41 | | { |
| | 9 | 42 | | var eventTypes = events?.ToHashSet(StringComparer.OrdinalIgnoreCase) |
| | 9 | 43 | | ?? new HashSet<string>(StringComparer.OrdinalIgnoreCase) |
| | 9 | 44 | | { |
| | 9 | 45 | | nameof(IntentumEventType.IntentInferred), |
| | 9 | 46 | | nameof(IntentumEventType.PolicyDecisionChanged) |
| | 9 | 47 | | }; |
| | 9 | 48 | | options.Webhooks.Add(new WebhookConfig { Url = url, EventTypes = eventTypes }); |
| | 9 | 49 | | return options; |
| | 9 | 50 | | } |
| | | 51 | | } |