Renamed methods across multiple client classes for consistency, shortening and aligning naming conventions (e.g., `CheckAttachmentsAsync` → `CheckAsync`, `ValidatePdfAsync` → `ValidateAsync`). Updated corresponding interfaces, unit tests, and documentation to reflect the new method names. Standardized method signatures to support both `Stream` and `byte[]` overloads consistently. Improved error handling in tests and updated examples in the README for batch processing and validation scenarios. Enhanced API endpoint overview for clarity.
85 lines
3.5 KiB
C#
85 lines
3.5 KiB
C#
using DocumentService.Application.Common.DTOs;
|
||
using DocumentService.Client.Interfaces;
|
||
|
||
namespace DocumentService.Client.Clients;
|
||
|
||
/// <summary>
|
||
/// Orchestrated multi-step workflows that compose multiple DocumentService clients
|
||
/// into single, higher-level operations.
|
||
/// </summary>
|
||
public class WorkflowsClient(IPdfValidationClient validation, ISwissQrCodeClient swissQrCode) : IWorkflowsClient
|
||
{
|
||
/// <inheritdoc />
|
||
public async Task<SwissQrCodeExtractionResult?> ExtractSwissQrCodeAsync(
|
||
byte[] pdfBytes,
|
||
bool raw = false,
|
||
bool throwIfInvalid = true,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var validationResult = await validation.ValidateAsync(pdfBytes, cancellationToken);
|
||
|
||
if (!validationResult.IsValid)
|
||
return throwIfInvalid
|
||
? throw new InvalidOperationException(
|
||
$"PDF validation failed (IsEncrypted={validationResult.IsEncrypted}, " +
|
||
$"PageCount={validationResult.PageCount}, FileSize={validationResult.FileSizeBytes}).")
|
||
: (SwissQrCodeExtractionResult?)null;
|
||
|
||
return await swissQrCode.ExtractAsync(pdfBytes, raw, cancellationToken);
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public async Task<SwissQrCodeExtractionResult?> ExtractSwissQrCodeAsync(
|
||
Stream pdfStream,
|
||
bool raw = false,
|
||
bool throwIfInvalid = true,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var validationResult = await validation.ValidateAsync(pdfStream, cancellationToken);
|
||
|
||
if (!validationResult.IsValid)
|
||
return throwIfInvalid
|
||
? throw new InvalidOperationException(
|
||
$"PDF validation failed (IsEncrypted={validationResult.IsEncrypted}, " +
|
||
$"PageCount={validationResult.PageCount}, FileSize={validationResult.FileSizeBytes}).")
|
||
: (SwissQrCodeExtractionResult?)null;
|
||
|
||
// Stream was consumed by validation – reset if possible, otherwise re-open is caller's responsibility.
|
||
// Since callers typically pass a FileStream or MemoryStream, Position reset covers the common case.
|
||
if (pdfStream.CanSeek)
|
||
pdfStream.Position = 0;
|
||
|
||
return await swissQrCode.ExtractAsync(pdfStream, raw, cancellationToken);
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public async Task<SwissQrCodeExtractionResult?> ExtractSwissQrCodeAsync(
|
||
string filePath,
|
||
bool raw = false,
|
||
bool throwIfInvalid = true,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
#if NETFRAMEWORK
|
||
using var stream = File.OpenRead(filePath);
|
||
var validationResult = await validation.ValidateAsync(stream, cancellationToken);
|
||
#else
|
||
var pdfBytes = await File.ReadAllBytesAsync(filePath, cancellationToken);
|
||
var validationResult = await validation.ValidateAsync(pdfBytes, cancellationToken);
|
||
#endif
|
||
|
||
if (!validationResult.IsValid)
|
||
return throwIfInvalid
|
||
? throw new InvalidOperationException(
|
||
$"PDF validation failed (IsEncrypted={validationResult.IsEncrypted}, " +
|
||
$"PageCount={validationResult.PageCount}, FileSize={validationResult.FileSizeBytes}).")
|
||
: (SwissQrCodeExtractionResult?)null;
|
||
|
||
#if NETFRAMEWORK
|
||
using var extractStream = File.OpenRead(filePath);
|
||
return await swissQrCode.ExtractAsync(extractStream, raw, cancellationToken);
|
||
#else
|
||
return await swissQrCode.ExtractAsync(pdfBytes, raw, cancellationToken);
|
||
#endif
|
||
}
|
||
}
|