Skip to content

Commit

Permalink
Refactor Elsa API client to use API key authentication
Browse files Browse the repository at this point in the history
The Elsa API client configuration has been refactored to use API key for authentication instead of HTTP handlers. Besides, the configuration method name was changed to 'AddElsaApiKeyClient' and its parameters were simplified for ease of use. Object 'ElsaClientBuilderOptions' has also been extended with additional properties for further customization.
  • Loading branch information
sfmskywalker committed Dec 29, 2023
1 parent 17452f7 commit d978151
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Elsa.Api.Client.Converters;
using Elsa.Api.Client.HttpMessageHandlers;
using Elsa.Api.Client.Options;
using Elsa.Api.Client.Resources.ActivityDescriptorOptions.Contracts;
using Elsa.Api.Client.Resources.ActivityDescriptors.Contracts;
Expand Down Expand Up @@ -31,39 +30,39 @@ namespace Elsa.Api.Client.Extensions;
public static class DependencyInjectionExtensions
{
/// <summary>
/// Adds the Elsa client to the service collection.
/// Adds the Elsa API client configured to use an API key to the service collection.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="baseAddress">The base address of the Elsa API.</param>
/// <param name="apiKey">The API key to use for authentication.</param>
/// <param name="configureHttpClient">An optional delegate that can be used to configure the HTTP client.</param>
/// <param name="configureBuilderOptions">An optional delegate that can be used to configure the client builder options.</param>
public static IServiceCollection AddElsaClient(this IServiceCollection services, Uri baseAddress, string apiKey, Action<IServiceProvider, HttpClient>? configureHttpClient = default, Action<ElsaClientBuilderOptions>? configureBuilderOptions = default)
public static IServiceCollection AddElsaApiKeyClient(this IServiceCollection services, Action<ElsaClientOptions> configureOptions)
{
services.AddScoped<ApiKeyHttpMessageHandler>();
return services.AddElsaClient(
options =>
{
options.BaseAddress = baseAddress;
options.ApiKey = apiKey;
options.ConfigureHttpClient = configureHttpClient;
},
configureBuilderOptions: options =>
{
options.ConfigureHttpClientBuilder = builder => builder.AddHttpMessageHandler<ApiKeyHttpMessageHandler>();
configureBuilderOptions?.Invoke(options);
});
var options = new ElsaClientOptions();
configureOptions(options);

return services.AddElsaClient(client =>
{
client.BaseAddress = options.BaseAddress;
client.ApiKey = options.ApiKey;
client.ConfigureHttpClient = options.ConfigureHttpClient;
});
}

/// <summary>
/// Adds the Elsa client to the service collection.
/// </summary>
public static IServiceCollection AddElsaClient(this IServiceCollection services, Action<ElsaClientOptions>? configureOptions = default, Action<ElsaClientBuilderOptions>? configureBuilderOptions = default)
public static IServiceCollection AddElsaClient(this IServiceCollection services, Action<ElsaClientBuilderOptions> configureClient)
{
var builderOptions = new ElsaClientBuilderOptions();
configureBuilderOptions?.Invoke(builderOptions);
configureClient.Invoke(builderOptions);

builderOptions.ConfigureHttpClientBuilder += builder => builder.AddHttpMessageHandler(sp => (DelegatingHandler)sp.GetRequiredService(builderOptions.AuthenticationHandler));

services.AddScoped(builderOptions.AuthenticationHandler);

services.Configure(configureOptions ?? (_ => { }));
services.Configure<ElsaClientOptions>(options =>
{
options.BaseAddress = builderOptions.BaseAddress;
options.ConfigureHttpClient = builderOptions.ConfigureHttpClient;
options.ApiKey = builderOptions.ApiKey;
});
services.AddApi<IWorkflowDefinitionsApi>(builderOptions);
services.AddApi<IWorkflowInstancesApi>(builderOptions);
services.AddApi<IActivityDescriptorsApi>(builderOptions);
Expand Down
26 changes: 23 additions & 3 deletions src/clients/Elsa.Api.Client/Options/ElsaClientBuilderOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Elsa.Api.Client.HttpMessageHandlers;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.Api.Client.Options;
Expand All @@ -7,10 +8,29 @@ namespace Elsa.Api.Client.Options;
/// </summary>
public class ElsaClientBuilderOptions
{
/// <summary>
/// Gets or sets the base address of the Elsa server.
/// </summary>
public Uri BaseAddress { get; set; } = default!;

/// <summary>
/// Gets or sets the API key function to use when authenticating with the Elsa server.
/// </summary>
public string? ApiKey { get; set; }

/// <summary>
/// A <see cref="DelegatingHandler"/> type that can be used to authenticate with the Elsa server.
/// Defaults to <see cref="ApiKeyHttpMessageHandler"/>.
/// </summary>
public Type AuthenticationHandler { get; set; } = typeof(ApiKeyHttpMessageHandler);

/// <summary>
/// Gets or sets a delegate that can be used to configure the HTTP client.
/// </summary>
public Action<IServiceProvider, HttpClient>? ConfigureHttpClient { get; set; }

/// <summary>
/// Gets or sets a delegate that can be used to configure the HTTP client builder.
/// </summary>
public Action<IHttpClientBuilder>? ConfigureHttpClientBuilder { get; set; }


public Action<IHttpClientBuilder> ConfigureHttpClientBuilder { get; set; } = _ => { };
}

0 comments on commit d978151

Please sign in to comment.