feat: Add DocumentService.Client base infrastructure
This commit is contained in:
93
DocumentService.Client/Client.cs
Normal file
93
DocumentService.Client/Client.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using DocumentService.Client.Configuration;
|
||||
using DocumentService.Client.Extensions;
|
||||
using DocumentService.Client.Interfaces;
|
||||
using DocumentService.Client.Models.ValueObjects;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DocumentService.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Static entry point for configuring and building the DocumentService client.
|
||||
/// </summary>
|
||||
public static class Client
|
||||
{
|
||||
private static Action<DocumentServiceClientOptions> Options { get; set; } = null!;
|
||||
|
||||
private static readonly Lazy<IServiceProvider> LazyProvider = new(() =>
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddDocumentServiceClients(Options);
|
||||
return services.BuildServiceProvider();
|
||||
});
|
||||
|
||||
public static bool IsConfigured => LazyProvider.IsValueCreated;
|
||||
|
||||
public static OnReconfigure OnReconfigure { get; set; } = OnReconfigure.ThrowException;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the client using a full <see cref="DocumentServiceClientOptions"/> action.
|
||||
/// </summary>
|
||||
/// <param name="configuration">Action that configures the client options.</param>
|
||||
public static void Configure(Action<DocumentServiceClientOptions> configuration)
|
||||
{
|
||||
if(IsConfigured)
|
||||
switch (OnReconfigure)
|
||||
{
|
||||
case OnReconfigure.ThrowException:
|
||||
throw new InvalidOperationException("DocumentService.Client is already configured. Reconfiguration is not allowed.");
|
||||
case OnReconfigure.Ignore:
|
||||
return;
|
||||
}
|
||||
Options = configuration;
|
||||
_ = LazyProvider.Value; // init service provider
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the client with only a base URL. Use the overload with options action for full control.
|
||||
/// </summary>
|
||||
/// <param name="baseUrl">Base URL of the DocumentService API.</param>
|
||||
public static void Configure(string baseUrl)
|
||||
{
|
||||
if (IsConfigured)
|
||||
switch (OnReconfigure)
|
||||
{
|
||||
case OnReconfigure.ThrowException:
|
||||
throw new InvalidOperationException("DocumentService.Client is already configured. Reconfiguration is not allowed.");
|
||||
case OnReconfigure.Ignore:
|
||||
return;
|
||||
}
|
||||
|
||||
Options = options => {
|
||||
options = new DocumentServiceClientOptions
|
||||
{
|
||||
BaseUrl = baseUrl
|
||||
};
|
||||
};
|
||||
_ = LazyProvider.Value; // init service provider
|
||||
}
|
||||
|
||||
private static IServiceProvider Provider => IsConfigured
|
||||
? LazyProvider.Value
|
||||
: throw new InvalidOperationException("DocumentService.Client is not configured.");
|
||||
|
||||
private static T GetRequiredServiceOfScope<T>() where T : notnull => Provider.CreateAsyncScope().ServiceProvider.GetRequiredService<T>();
|
||||
|
||||
#region Controllers
|
||||
public static IPdfAttachmentClient Attachment => GetRequiredServiceOfScope<IPdfAttachmentClient>();
|
||||
|
||||
public static IPdfConversionClient Conversion => GetRequiredServiceOfScope<IPdfConversionClient>();
|
||||
|
||||
public static IPdfOperationsClient Operations => GetRequiredServiceOfScope<IPdfOperationsClient>();
|
||||
|
||||
public static IPdfValidationClient Validation => GetRequiredServiceOfScope<IPdfValidationClient>();
|
||||
|
||||
public static ISwissQrCodeClient SwissQrCode => GetRequiredServiceOfScope<ISwissQrCodeClient>();
|
||||
|
||||
public static IZugferdClient Zugferd => GetRequiredServiceOfScope<IZugferdClient>();
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user