2023-06-10 18:32:06 +00:00
|
|
|
using System.Text.Json;
|
2023-06-19 13:32:26 +00:00
|
|
|
using System.Text.Json.Serialization;
|
2023-03-26 21:53:17 +00:00
|
|
|
using Elsa.Api.Client.Contracts;
|
2023-06-19 13:32:26 +00:00
|
|
|
using Elsa.Api.Client.Converters;
|
2023-03-26 21:53:17 +00:00
|
|
|
using Elsa.Api.Client.Options;
|
2023-06-10 13:33:32 +00:00
|
|
|
using Elsa.Api.Client.Resources.ActivityDescriptors.Contracts;
|
2023-07-22 19:05:33 +00:00
|
|
|
using Elsa.Api.Client.Resources.ActivityExecutions.Contracts;
|
2023-07-25 13:12:27 +00:00
|
|
|
using Elsa.Api.Client.Resources.Features.Contracts;
|
2023-05-19 12:36:16 +00:00
|
|
|
using Elsa.Api.Client.Resources.Identity.Contracts;
|
2023-07-01 20:18:04 +00:00
|
|
|
using Elsa.Api.Client.Resources.StorageDrivers.Contracts;
|
|
|
|
|
using Elsa.Api.Client.Resources.VariableTypes.Contracts;
|
2023-07-02 16:59:13 +00:00
|
|
|
using Elsa.Api.Client.Resources.WorkflowActivationStrategies.Contracts;
|
2023-05-16 18:03:12 +00:00
|
|
|
using Elsa.Api.Client.Resources.WorkflowDefinitions.Contracts;
|
2023-07-07 11:53:44 +00:00
|
|
|
using Elsa.Api.Client.Resources.WorkflowInstances.Contracts;
|
2023-03-26 21:53:17 +00:00
|
|
|
using Elsa.Api.Client.Services;
|
2023-04-01 14:15:35 +00:00
|
|
|
using JetBrains.Annotations;
|
2023-03-26 21:53:17 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Refit;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Api.Client.Extensions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides extension methods for dependency injection.
|
|
|
|
|
/// </summary>
|
2023-04-01 14:15:35 +00:00
|
|
|
[PublicAPI]
|
2023-03-26 21:53:17 +00:00
|
|
|
public static class DependencyInjectionExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the Elsa client to the service collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IServiceCollection AddElsaClient(this IServiceCollection services, Action<ElsaClientOptions> configureOptions)
|
|
|
|
|
{
|
|
|
|
|
services.Configure(configureOptions);
|
2023-07-31 20:37:57 +00:00
|
|
|
services.AddScoped<IElsaClient, ElsaClient>();
|
2023-06-19 13:32:26 +00:00
|
|
|
services.AddActivityTypeService();
|
2023-06-10 18:32:06 +00:00
|
|
|
|
2023-06-19 13:32:26 +00:00
|
|
|
services.AddApi<IWorkflowDefinitionsApi>(CreateRefitSettings);
|
2023-07-07 11:53:44 +00:00
|
|
|
services.AddApi<IWorkflowInstancesApi>(CreateRefitSettings);
|
2023-06-19 13:32:26 +00:00
|
|
|
services.AddApi<IActivityDescriptorsApi>(CreateRefitSettings);
|
2023-07-22 19:05:33 +00:00
|
|
|
services.AddApi<IActivityExecutionsApi>(CreateRefitSettings);
|
2023-07-01 20:18:04 +00:00
|
|
|
services.AddApi<IStorageDriversApi>(CreateRefitSettings);
|
|
|
|
|
services.AddApi<IVariableTypesApi>(CreateRefitSettings);
|
2023-07-02 16:59:13 +00:00
|
|
|
services.AddApi<IWorkflowActivationStrategiesApi>(CreateRefitSettings);
|
2023-06-19 13:32:26 +00:00
|
|
|
services.AddApi<ILoginApi>(CreateRefitSettings);
|
2023-07-25 13:12:27 +00:00
|
|
|
services.AddApi<IFeaturesApi>(CreateRefitSettings);
|
2023-03-26 21:53:17 +00:00
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 13:32:26 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds activity type services to the service collection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IServiceCollection AddActivityTypeService(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 14:15:35 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a refit client for the specified API type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services">The service collection.</param>
|
|
|
|
|
/// <param name="settings">The refit settings.</param>
|
|
|
|
|
/// <typeparam name="T">The API type.</typeparam>
|
2023-06-19 13:32:26 +00:00
|
|
|
public static void AddApi<T>(this IServiceCollection services, Func<IServiceProvider, RefitSettings> settings) where T : class
|
2023-03-26 21:53:17 +00:00
|
|
|
{
|
2023-05-16 20:37:08 +00:00
|
|
|
services.AddRefitClient<T>(settings)
|
|
|
|
|
.ConfigureHttpClient(ConfigureElsaApiHttpClient);
|
2023-03-26 21:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-01 14:15:35 +00:00
|
|
|
private static void ConfigureElsaApiHttpClient(IServiceProvider serviceProvider, HttpClient httpClient)
|
2023-03-26 21:53:17 +00:00
|
|
|
{
|
|
|
|
|
var options = serviceProvider.GetRequiredService<IOptions<ElsaClientOptions>>().Value;
|
|
|
|
|
httpClient.BaseAddress = options.BaseAddress;
|
2023-08-11 21:02:36 +00:00
|
|
|
options.ConfigureHttpClient?.Invoke(serviceProvider, httpClient);
|
2023-03-26 21:53:17 +00:00
|
|
|
}
|
2023-06-19 13:32:26 +00:00
|
|
|
|
|
|
|
|
private static RefitSettings CreateRefitSettings(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
2023-07-04 09:57:01 +00:00
|
|
|
JsonSerializerOptions serializerOptions = new()
|
2023-06-19 13:32:26 +00:00
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-04 09:57:01 +00:00
|
|
|
serializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
|
|
|
serializerOptions.Converters.Add(new VersionOptionsJsonConverter());
|
|
|
|
|
serializerOptions.Converters.Add(new ExpressionJsonConverterFactory());
|
2023-07-09 10:33:26 +00:00
|
|
|
|
2023-06-19 13:32:26 +00:00
|
|
|
var settings = new RefitSettings
|
|
|
|
|
{
|
2023-07-04 09:57:01 +00:00
|
|
|
ContentSerializer = new SystemTextJsonContentSerializer(serializerOptions)
|
2023-06-19 13:32:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 21:53:17 +00:00
|
|
|
}
|