| | | 1 | | using Elsa.Api.Client.Options; |
| | | 2 | | using Elsa.Api.Client.Resources.ActivityDescriptorOptions.Contracts; |
| | | 3 | | using Elsa.Api.Client.Resources.ActivityDescriptors.Contracts; |
| | | 4 | | using Elsa.Api.Client.Resources.ActivityExecutions.Contracts; |
| | | 5 | | using Elsa.Api.Client.Resources.Alterations.Contracts; |
| | | 6 | | using Elsa.Api.Client.Resources.CommitStrategies.Contracts; |
| | | 7 | | using Elsa.Api.Client.Resources.Features.Contracts; |
| | | 8 | | using Elsa.Api.Client.Resources.Identity.Contracts; |
| | | 9 | | using Elsa.Api.Client.Resources.IncidentStrategies.Contracts; |
| | | 10 | | using Elsa.Api.Client.Resources.LogPersistenceStrategies; |
| | | 11 | | using Elsa.Api.Client.Resources.Resilience.Contracts; |
| | | 12 | | using Elsa.Api.Client.Resources.Scripting.Contracts; |
| | | 13 | | using Elsa.Api.Client.Resources.StorageDrivers.Contracts; |
| | | 14 | | using Elsa.Api.Client.Resources.Tasks.Contracts; |
| | | 15 | | using Elsa.Api.Client.Resources.Tests; |
| | | 16 | | using Elsa.Api.Client.Resources.VariableTypes.Contracts; |
| | | 17 | | using Elsa.Api.Client.Resources.WorkflowActivationStrategies.Contracts; |
| | | 18 | | using Elsa.Api.Client.Resources.WorkflowDefinitions.Contracts; |
| | | 19 | | using Elsa.Api.Client.Resources.WorkflowExecutionContexts.Contracts; |
| | | 20 | | using Elsa.Api.Client.Resources.WorkflowInstances.Contracts; |
| | | 21 | | using JetBrains.Annotations; |
| | | 22 | | using Microsoft.Extensions.DependencyInjection; |
| | | 23 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 24 | | using Microsoft.Extensions.Options; |
| | | 25 | | using Refit; |
| | | 26 | | using static Elsa.Api.Client.RefitSettingsHelper; |
| | | 27 | | |
| | | 28 | | namespace Elsa.Api.Client.Extensions; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Provides extension methods for dependency injection. |
| | | 32 | | /// </summary> |
| | | 33 | | [PublicAPI] |
| | | 34 | | public static class DependencyInjectionExtensions |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds default Elsa API clients configured to use an API key. |
| | | 38 | | /// </summary> |
| | | 39 | | public static IServiceCollection AddDefaultApiClientsUsingApiKey(this IServiceCollection services, Action<ElsaClient |
| | | 40 | | { |
| | 0 | 41 | | var options = new ElsaClientOptions(); |
| | 0 | 42 | | configureOptions(options); |
| | | 43 | | |
| | 0 | 44 | | return services.AddDefaultApiClients(client => |
| | 0 | 45 | | { |
| | 0 | 46 | | client.BaseAddress = options.BaseAddress; |
| | 0 | 47 | | client.ApiKey = options.ApiKey; |
| | 0 | 48 | | client.ConfigureHttpClient = options.ConfigureHttpClient; |
| | 0 | 49 | | }); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Adds default Elsa API clients. |
| | | 54 | | /// </summary> |
| | | 55 | | public static IServiceCollection AddDefaultApiClients(this IServiceCollection services, Action<ElsaClientBuilderOpti |
| | | 56 | | { |
| | 0 | 57 | | return services.AddApiClients(configureClient, builderOptions => |
| | 0 | 58 | | { |
| | 0 | 59 | | var builderOptionsWithoutRetryPolicy = new ElsaClientBuilderOptions |
| | 0 | 60 | | { |
| | 0 | 61 | | ApiKey = builderOptions.ApiKey, |
| | 0 | 62 | | AuthenticationHandler = builderOptions.AuthenticationHandler, |
| | 0 | 63 | | BaseAddress = builderOptions.BaseAddress, |
| | 0 | 64 | | ConfigureHttpClient = builderOptions.ConfigureHttpClient, |
| | 0 | 65 | | ConfigureHttpClientBuilder = builderOptions.ConfigureHttpClientBuilder, |
| | 0 | 66 | | ConfigureRetryPolicy = null |
| | 0 | 67 | | }; |
| | 0 | 68 | | |
| | 0 | 69 | | services.AddApi<IWorkflowDefinitionsApi>(builderOptions); |
| | 0 | 70 | | services.AddApi<IExecuteWorkflowApi>(builderOptionsWithoutRetryPolicy); |
| | 0 | 71 | | services.AddApi<IWorkflowInstancesApi>(builderOptions); |
| | 0 | 72 | | services.AddApi<IActivityDescriptorsApi>(builderOptions); |
| | 0 | 73 | | services.AddApi<IActivityDescriptorOptionsApi>(builderOptions); |
| | 0 | 74 | | services.AddApi<IActivityExecutionsApi>(builderOptions); |
| | 0 | 75 | | services.AddApi<IStorageDriversApi>(builderOptions); |
| | 0 | 76 | | services.AddApi<IVariableTypesApi>(builderOptions); |
| | 0 | 77 | | services.AddApi<IWorkflowActivationStrategiesApi>(builderOptions); |
| | 0 | 78 | | services.AddApi<IIncidentStrategiesApi>(builderOptions); |
| | 0 | 79 | | services.AddApi<ILogPersistenceStrategiesApi>(builderOptions); |
| | 0 | 80 | | services.AddApi<ICommitStrategiesApi>(builderOptions); |
| | 0 | 81 | | services.AddApi<IResilienceStrategiesApi>(builderOptions); |
| | 0 | 82 | | services.AddApi<IRetryAttemptsApi>(builderOptions); |
| | 0 | 83 | | services.AddApi<ILoginApi>(builderOptions); |
| | 0 | 84 | | services.AddApi<IFeaturesApi>(builderOptions); |
| | 0 | 85 | | services.AddApi<IJavaScriptApi>(builderOptions); |
| | 0 | 86 | | services.AddApi<IExpressionDescriptorsApi>(builderOptions); |
| | 0 | 87 | | services.AddApi<IWorkflowContextProviderDescriptorsApi>(builderOptions); |
| | 0 | 88 | | services.AddApi<IAlterationsApi>(builderOptions); |
| | 0 | 89 | | services.AddApi<ITasksApi>(builderOptions); |
| | 0 | 90 | | services.AddApi<ITestsApi>(builderOptions); |
| | 0 | 91 | | }); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Adds an API client to the service collection. Requires AddElsaClient to be called exactly once. |
| | | 96 | | /// </summary> |
| | | 97 | | public static IServiceCollection AddApiClient<T>(this IServiceCollection services, Action<ElsaClientBuilderOptions>? |
| | | 98 | | { |
| | 0 | 99 | | return services.AddApiClients(configureClient, builderOptions => services.AddApi<T>(builderOptions)); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Adds the Elsa client to the service collection. |
| | | 104 | | /// </summary> |
| | | 105 | | public static IServiceCollection AddApiClients(this IServiceCollection services, Action<ElsaClientBuilderOptions>? c |
| | | 106 | | { |
| | 0 | 107 | | var builderOptions = new ElsaClientBuilderOptions(); |
| | 0 | 108 | | configureClient?.Invoke(builderOptions); |
| | 0 | 109 | | builderOptions.ConfigureHttpClientBuilder += builder => builder.AddHttpMessageHandler(sp => (DelegatingHandler)s |
| | | 110 | | |
| | 0 | 111 | | services.TryAddScoped(builderOptions.AuthenticationHandler); |
| | | 112 | | |
| | 0 | 113 | | services.Configure<ElsaClientOptions>(options => |
| | 0 | 114 | | { |
| | 0 | 115 | | options.BaseAddress = builderOptions.BaseAddress; |
| | 0 | 116 | | options.ConfigureHttpClient = builderOptions.ConfigureHttpClient; |
| | 0 | 117 | | options.ApiKey = builderOptions.ApiKey; |
| | 0 | 118 | | }); |
| | | 119 | | |
| | 0 | 120 | | configureServices?.Invoke(builderOptions); |
| | 0 | 121 | | return services; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Adds a refit client for the specified API type. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="services">The service collection.</param> |
| | | 128 | | /// <param name="httpClientBuilderOptions">An options object that can be used to configure the HTTP client builder.< |
| | | 129 | | /// <typeparam name="T">The type representing the API.</typeparam> |
| | | 130 | | public static IServiceCollection AddApi<T>(this IServiceCollection services, ElsaClientBuilderOptions? httpClientBui |
| | | 131 | | { |
| | 0 | 132 | | return services.AddApi(typeof(T), httpClientBuilderOptions); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Adds a refit client for the specified API type. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="services">The service collection.</param> |
| | | 139 | | /// <param name="apiType">The type representing the API</param> |
| | | 140 | | /// <param name="httpClientBuilderOptions">An options object that can be used to configure the HTTP client builder.< |
| | | 141 | | public static IServiceCollection AddApi(this IServiceCollection services, Type apiType, ElsaClientBuilderOptions? ht |
| | | 142 | | { |
| | 0 | 143 | | var builder = services.AddRefitClient(apiType, sp => CreateRefitSettings(sp, httpClientBuilderOptions?.Configure |
| | 0 | 144 | | httpClientBuilderOptions?.ConfigureHttpClientBuilder(builder); |
| | 0 | 145 | | httpClientBuilderOptions?.ConfigureRetryPolicy?.Invoke(builder); |
| | 0 | 146 | | return services; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Adds a refit client for the specified API type. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="services">The service collection.</param> |
| | | 153 | | /// <param name="httpClientBuilderOptions">An options object that can be used to configure the HTTP client builder.< |
| | | 154 | | /// <typeparam name="T">The type representing the API.</typeparam> |
| | | 155 | | public static void AddApiWithoutRetryPolicy<T>(this IServiceCollection services, ElsaClientBuilderOptions? httpClien |
| | | 156 | | { |
| | 0 | 157 | | var builder = services |
| | 0 | 158 | | .AddRefitClient<T>(sp => CreateRefitSettings(sp), typeof(T).Name) |
| | 0 | 159 | | .ConfigureHttpClient(ConfigureElsaApiHttpClient); |
| | 0 | 160 | | httpClientBuilderOptions?.ConfigureHttpClientBuilder(builder); |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Creates an API client for the specified API type. |
| | | 165 | | /// </summary> |
| | | 166 | | public static T CreateApi<T>(this IServiceProvider serviceProvider, Uri baseAddress) where T : class |
| | | 167 | | { |
| | 0 | 168 | | var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); |
| | 0 | 169 | | var httpClient = httpClientFactory.CreateClient(typeof(T).Name); |
| | 0 | 170 | | httpClient.BaseAddress = baseAddress; |
| | 0 | 171 | | return CreateApi<T>(serviceProvider, httpClient); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Creates an API client for the specified API type. |
| | | 176 | | /// </summary> |
| | | 177 | | public static T CreateApi<T>(this IServiceProvider serviceProvider, HttpClient httpClient) where T : class |
| | | 178 | | { |
| | 0 | 179 | | return RestService.For<T>(httpClient, CreateRefitSettings(serviceProvider)); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private static void ConfigureElsaApiHttpClient(IServiceProvider serviceProvider, HttpClient httpClient) |
| | | 183 | | { |
| | 0 | 184 | | var options = serviceProvider.GetRequiredService<IOptions<ElsaClientOptions>>().Value; |
| | 0 | 185 | | httpClient.BaseAddress = options.BaseAddress; |
| | 0 | 186 | | options.ConfigureHttpClient?.Invoke(serviceProvider, httpClient); |
| | 0 | 187 | | } |
| | | 188 | | } |