ApiEase is a .NET library designed to simplify API client creation with built-in support for session-based authentication, retry policies, and flexible configuration. Leveraging Refit for defining API interfaces, the package provides base classes and extension methods that streamline the registration of custom HTTP clients with dependency injection.
- Dynamic API Client Registration:: Automatically register multiple API clients with custom settings.
- Session-Based Authentication: Automatically refresh session tokens on
401 Unauthorizedresponses. - Retry Policies: Integrated retry mechanism using
Polly. - Flexible Configuration: Configure base URLs, authentication handlers, and settings using
IOptions.
To install ApiEase via NuGet, use:
dotnet add package ApiEaseEach API client requires its own configuration class that implements IBaseSettings:
public class ExampleSettings : IBaseSettings
{
public string? BaseUrl { get; set; }
public string? Token { get; set; } // Additional configuration properties as needed
}Define an interface for your API using Refit attributes to specify the endpoints, and implement IBaseClient or one of its generic variants for greater control over settings and handler types.
using Refit;
public interface IExampleClient : IBaseClient<ExampleSettings>
{
[Get("/items")]
Task<ApiResponse<List<Item>>> GetItemsAsync();
}The IBaseClient interfaces come in three variations for different levels of customization:
IBaseClient: Basic interface, serves as a marker for all clients.IBaseClient<TSettings>: Allows you to specify a settings class, which should implementIBaseSettings.IBaseClient<TSettings, TDelegatingHandler>: Adds support for a custom delegating handler.TDelegatingHandlermust inherit fromDelegatingHandler, which can be used for custom authentication or middleware.
Example:
public interface IExampleClient : IBaseClient<ExampleSettings, CustomAuthHandler>
{
[Get("/items")]
Task<ApiResponse<List<Item>>> GetItemsAsync();
}In your Program.cs or Startup.cs, register the clients using AddDynamicClients:
using ApiEase.Core.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register dynamic API clients
builder.Services.AddDynamicClients(builder.Configuration);
var app = builder.Build();In your appsettings.json, add the configuration settings for your API client(s).
{
"ApiSettings": {
"Example": {
"BaseUrl": "https://api.example.com",
"Token": "thisIsAToken"
}
}
}Once registered, inject your client into any service or controller:
public class MyService
{
private readonly IExampleClient _exampleClient;
public MyService(IExampleClient exampleClient)
{
_exampleClient = exampleClient;
}
public async Task DoSomethingAsync()
{
var items = await _exampleClient.GetItemsAsync();
// Process items
}
}You can create a custom delegating handler by implementing DelegatingHandler and specifying it when registering the client. For example:
public class CustomAuthHandler<TSettings> : DelegatingHandler where TSettings : IBaseSettings
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "your-token");
return await base.SendAsync(request, cancellationToken);
}
}In AddDynamicClients, ApiEase will automatically detect and apply these handlers. If your settings implement IBasicAuth or IBearerAuthSettings, the appropriate HTTP header will also be added automatically.
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.