< Summary

Information
Class: Elsa.Api.Client.Extensions.DependencyInjectionExtensions
Assembly: Elsa.Api.Client
File(s): /home/runner/work/elsa-core/elsa-core/src/clients/Elsa.Api.Client/Extensions/DependencyInjectionExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 75
Coverable lines: 75
Total lines: 188
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddDefaultApiClientsUsingApiKey(...)100%210%
AddDefaultApiClients(...)100%210%
AddApiClient(...)100%210%
AddApiClients(...)0%4260%
AddApi(...)100%210%
AddApi(...)0%7280%
AddApiWithoutRetryPolicy(...)0%2040%
CreateApi(...)100%210%
CreateApi(...)100%210%
ConfigureElsaApiHttpClient(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/clients/Elsa.Api.Client/Extensions/DependencyInjectionExtensions.cs

#LineLine coverage
 1using Elsa.Api.Client.Options;
 2using Elsa.Api.Client.Resources.ActivityDescriptorOptions.Contracts;
 3using Elsa.Api.Client.Resources.ActivityDescriptors.Contracts;
 4using Elsa.Api.Client.Resources.ActivityExecutions.Contracts;
 5using Elsa.Api.Client.Resources.Alterations.Contracts;
 6using Elsa.Api.Client.Resources.CommitStrategies.Contracts;
 7using Elsa.Api.Client.Resources.Features.Contracts;
 8using Elsa.Api.Client.Resources.Identity.Contracts;
 9using Elsa.Api.Client.Resources.IncidentStrategies.Contracts;
 10using Elsa.Api.Client.Resources.LogPersistenceStrategies;
 11using Elsa.Api.Client.Resources.Resilience.Contracts;
 12using Elsa.Api.Client.Resources.Scripting.Contracts;
 13using Elsa.Api.Client.Resources.StorageDrivers.Contracts;
 14using Elsa.Api.Client.Resources.Tasks.Contracts;
 15using Elsa.Api.Client.Resources.Tests;
 16using Elsa.Api.Client.Resources.VariableTypes.Contracts;
 17using Elsa.Api.Client.Resources.WorkflowActivationStrategies.Contracts;
 18using Elsa.Api.Client.Resources.WorkflowDefinitions.Contracts;
 19using Elsa.Api.Client.Resources.WorkflowExecutionContexts.Contracts;
 20using Elsa.Api.Client.Resources.WorkflowInstances.Contracts;
 21using JetBrains.Annotations;
 22using Microsoft.Extensions.DependencyInjection;
 23using Microsoft.Extensions.DependencyInjection.Extensions;
 24using Microsoft.Extensions.Options;
 25using Refit;
 26using static Elsa.Api.Client.RefitSettingsHelper;
 27
 28namespace Elsa.Api.Client.Extensions;
 29
 30/// <summary>
 31/// Provides extension methods for dependency injection.
 32/// </summary>
 33[PublicAPI]
 34public 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    {
 041        var options = new ElsaClientOptions();
 042        configureOptions(options);
 43
 044        return services.AddDefaultApiClients(client =>
 045        {
 046            client.BaseAddress = options.BaseAddress;
 047            client.ApiKey = options.ApiKey;
 048            client.ConfigureHttpClient = options.ConfigureHttpClient;
 049        });
 50    }
 51
 52    /// <summary>
 53    /// Adds default Elsa API clients.
 54    /// </summary>
 55    public static IServiceCollection AddDefaultApiClients(this IServiceCollection services, Action<ElsaClientBuilderOpti
 56    {
 057        return services.AddApiClients(configureClient, builderOptions =>
 058        {
 059            var builderOptionsWithoutRetryPolicy = new ElsaClientBuilderOptions
 060            {
 061                ApiKey = builderOptions.ApiKey,
 062                AuthenticationHandler = builderOptions.AuthenticationHandler,
 063                BaseAddress = builderOptions.BaseAddress,
 064                ConfigureHttpClient = builderOptions.ConfigureHttpClient,
 065                ConfigureHttpClientBuilder = builderOptions.ConfigureHttpClientBuilder,
 066                ConfigureRetryPolicy = null
 067            };
 068
 069            services.AddApi<IWorkflowDefinitionsApi>(builderOptions);
 070            services.AddApi<IExecuteWorkflowApi>(builderOptionsWithoutRetryPolicy);
 071            services.AddApi<IWorkflowInstancesApi>(builderOptions);
 072            services.AddApi<IActivityDescriptorsApi>(builderOptions);
 073            services.AddApi<IActivityDescriptorOptionsApi>(builderOptions);
 074            services.AddApi<IActivityExecutionsApi>(builderOptions);
 075            services.AddApi<IStorageDriversApi>(builderOptions);
 076            services.AddApi<IVariableTypesApi>(builderOptions);
 077            services.AddApi<IWorkflowActivationStrategiesApi>(builderOptions);
 078            services.AddApi<IIncidentStrategiesApi>(builderOptions);
 079            services.AddApi<ILogPersistenceStrategiesApi>(builderOptions);
 080            services.AddApi<ICommitStrategiesApi>(builderOptions);
 081            services.AddApi<IResilienceStrategiesApi>(builderOptions);
 082            services.AddApi<IRetryAttemptsApi>(builderOptions);
 083            services.AddApi<ILoginApi>(builderOptions);
 084            services.AddApi<IFeaturesApi>(builderOptions);
 085            services.AddApi<IJavaScriptApi>(builderOptions);
 086            services.AddApi<IExpressionDescriptorsApi>(builderOptions);
 087            services.AddApi<IWorkflowContextProviderDescriptorsApi>(builderOptions);
 088            services.AddApi<IAlterationsApi>(builderOptions);
 089            services.AddApi<ITasksApi>(builderOptions);
 090            services.AddApi<ITestsApi>(builderOptions);
 091        });
 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    {
 099        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    {
 0107        var builderOptions = new ElsaClientBuilderOptions();
 0108        configureClient?.Invoke(builderOptions);
 0109        builderOptions.ConfigureHttpClientBuilder += builder => builder.AddHttpMessageHandler(sp => (DelegatingHandler)s
 110
 0111        services.TryAddScoped(builderOptions.AuthenticationHandler);
 112
 0113        services.Configure<ElsaClientOptions>(options =>
 0114        {
 0115            options.BaseAddress = builderOptions.BaseAddress;
 0116            options.ConfigureHttpClient = builderOptions.ConfigureHttpClient;
 0117            options.ApiKey = builderOptions.ApiKey;
 0118        });
 119
 0120        configureServices?.Invoke(builderOptions);
 0121        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    {
 0132        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    {
 0143        var builder = services.AddRefitClient(apiType, sp => CreateRefitSettings(sp, httpClientBuilderOptions?.Configure
 0144        httpClientBuilderOptions?.ConfigureHttpClientBuilder(builder);
 0145        httpClientBuilderOptions?.ConfigureRetryPolicy?.Invoke(builder);
 0146        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    {
 0157        var builder = services
 0158            .AddRefitClient<T>(sp => CreateRefitSettings(sp), typeof(T).Name)
 0159            .ConfigureHttpClient(ConfigureElsaApiHttpClient);
 0160        httpClientBuilderOptions?.ConfigureHttpClientBuilder(builder);
 0161    }
 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    {
 0168        var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
 0169        var httpClient = httpClientFactory.CreateClient(typeof(T).Name);
 0170        httpClient.BaseAddress = baseAddress;
 0171        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    {
 0179        return RestService.For<T>(httpClient, CreateRefitSettings(serviceProvider));
 180    }
 181
 182    private static void ConfigureElsaApiHttpClient(IServiceProvider serviceProvider, HttpClient httpClient)
 183    {
 0184        var options = serviceProvider.GetRequiredService<IOptions<ElsaClientOptions>>().Value;
 0185        httpClient.BaseAddress = options.BaseAddress;
 0186        options.ConfigureHttpClient?.Invoke(serviceProvider, httpClient);
 0187    }
 188}