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.
90 lines
3.4 KiB
C#
90 lines
3.4 KiB
C#
using DocumentService.Application.Common.DTOs;
|
|
using DocumentService.Application.ExtractZugferd;
|
|
using DocumentService.Client.Interfaces;
|
|
using DocumentService.Application.Common.DTOs.Requests;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Net.Http;
|
|
|
|
namespace DocumentService.Client.Clients;
|
|
|
|
/// <summary>
|
|
/// Implementation of ZUGFeRD client (detection and extraction).
|
|
/// </summary>
|
|
public class ZugferdClient(HttpClient httpClient, ILogger<ZugferdClient> logger) : BaseDocumentClient(httpClient, logger), IZugferdClient
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task<ZugferdCheckResult> CheckAsync(Stream pdfStream, CancellationToken cancellationToken = default)
|
|
{
|
|
var result = await SendMultipartAsync<ZugferdCheckResult>(
|
|
"/api/pdf/zugferd/has-zugferd",
|
|
pdfStream,
|
|
"document.pdf",
|
|
cancellationToken);
|
|
|
|
return result ?? throw new InvalidOperationException("API returned null response");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<ZugferdCheckResult> CheckAsync(byte[] pdfBytes, CancellationToken cancellationToken = default)
|
|
{
|
|
var request = new HasZugferdRequest { Base64Pdf = ToBase64(pdfBytes) };
|
|
|
|
var result = await SendJsonAsync<HasZugferdRequest, ZugferdCheckResult>(
|
|
"/api/pdf/zugferd/has-zugferd",
|
|
request,
|
|
cancellationToken);
|
|
|
|
return result ?? throw new InvalidOperationException("API returned null response");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Stream> ExtractAsync(Stream pdfStream, CancellationToken cancellationToken = default)
|
|
{
|
|
// asFile=true returns the XML file directly (application/xml stream)
|
|
return await SendMultipartForStreamAsync(
|
|
"/api/pdf/zugferd/extract?asFile=true",
|
|
pdfStream,
|
|
"document.pdf",
|
|
cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Stream> ExtractAsync(byte[] pdfBytes, CancellationToken cancellationToken = default)
|
|
{
|
|
var request = new ExtractZugferdRequest { Base64Pdf = ToBase64(pdfBytes) };
|
|
|
|
// format=file returns the XML file directly (application/xml stream)
|
|
return await SendJsonForStreamAsync(
|
|
"/api/pdf/zugferd/extract?format=file",
|
|
request,
|
|
cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<ZugferdExtractionResult> ExtractAsResultAsync(Stream pdfStream, CancellationToken cancellationToken = default)
|
|
{
|
|
// asFile=false returns JSON with metadata + XML content
|
|
var result = await SendMultipartAsync<ZugferdExtractionResult>(
|
|
"/api/pdf/zugferd/extract?asFile=false",
|
|
pdfStream,
|
|
"document.pdf",
|
|
cancellationToken);
|
|
|
|
return result ?? throw new InvalidOperationException("API returned null response");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<ZugferdExtractionResult> ExtractAsResultAsync(byte[] pdfBytes, CancellationToken cancellationToken = default)
|
|
{
|
|
var request = new ExtractZugferdRequest { Base64Pdf = ToBase64(pdfBytes) };
|
|
|
|
// format=json returns JSON with metadata + XML content
|
|
var result = await SendJsonAsync<ExtractZugferdRequest, ZugferdExtractionResult>(
|
|
"/api/pdf/zugferd/extract?format=json",
|
|
request,
|
|
cancellationToken);
|
|
|
|
return result ?? throw new InvalidOperationException("API returned null response");
|
|
}
|
|
}
|