feat: Add DocumentService.Client base infrastructure
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using DocumentService.Client.Clients;
|
||||
using DocumentService.Client.Configuration;
|
||||
using DocumentService.Client.Interfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace DocumentService.Client.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for registering DocumentService clients in DI container.
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers all six DocumentService HTTP clients as Scoped via HttpClientFactory.
|
||||
/// </summary>
|
||||
/// <param name="services">Service collection</param>
|
||||
/// <param name="configureOptions">Configuration action for client options</param>
|
||||
/// <returns>Service collection for chaining</returns>
|
||||
public static IServiceCollection AddDocumentServiceClients(
|
||||
this IServiceCollection services,
|
||||
Action<DocumentServiceClientOptions> configureOptions)
|
||||
{
|
||||
services.Configure(configureOptions);
|
||||
|
||||
var options = new DocumentServiceClientOptions();
|
||||
configureOptions(options);
|
||||
|
||||
var configureHandler = () => new HttpClientHandler
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||
};
|
||||
|
||||
void ConfigureClient(System.Net.Http.HttpClient client)
|
||||
{
|
||||
client.BaseAddress = new Uri(options.BaseUrl);
|
||||
client.Timeout = options.Timeout;
|
||||
}
|
||||
|
||||
services.AddHttpClient<IPdfValidationClient, PdfValidationClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
services.AddHttpClient<IPdfAttachmentClient, PdfAttachmentClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
services.AddHttpClient<IPdfOperationsClient, PdfOperationsClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
services.AddHttpClient<ISwissQrCodeClient, SwissQrCodeClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
services.AddHttpClient<IZugferdClient, ZugferdClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
// Conversion client is registered but all methods throw NotImplementedException
|
||||
// until the server-side endpoints are ready.
|
||||
services.AddHttpClient<IPdfConversionClient, PdfConversionClient>(ConfigureClient)
|
||||
.ConfigurePrimaryHttpMessageHandler(configureHandler);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
64
DocumentService.Client/Extensions/StreamExtensions.cs
Normal file
64
DocumentService.Client/Extensions/StreamExtensions.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DocumentService.Client.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for Stream operations.
|
||||
/// </summary>
|
||||
public static class StreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a stream to a Base64-encoded string.
|
||||
/// </summary>
|
||||
/// <param name="stream">Source stream</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>Base64-encoded string</returns>
|
||||
public static async Task<string> ToBase64StringAsync(this Stream stream, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (stream == null)
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
|
||||
byte[] bytes = await stream.ToBytesAsync(cancellationToken);
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a stream to a byte array.
|
||||
/// </summary>
|
||||
/// <param name="stream">Source stream</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>Byte array</returns>
|
||||
public static async Task<byte[]> ToBytesAsync(this Stream stream, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (stream == null)
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
|
||||
if (stream is MemoryStream ms)
|
||||
{
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
using var memoryStream = new MemoryStream();
|
||||
#if NET8_0
|
||||
await stream.CopyToAsync(memoryStream, cancellationToken);
|
||||
#else
|
||||
await stream.CopyToAsync(memoryStream);
|
||||
#endif
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets stream position to beginning if seekable.
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream to reset</param>
|
||||
/// <returns>The same stream (for chaining)</returns>
|
||||
public static Stream Reset(this Stream stream)
|
||||
{
|
||||
if (stream != null && stream.CanSeek)
|
||||
stream.Position = 0;
|
||||
|
||||
return stream!;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user