Consolidated DTOs under `DocumentService.Application.Common.DTOs.Requests` to centralize and align them with the application layer. Introduced `PdfBase64RequestBase` to encapsulate shared properties, reducing redundancy across request DTOs. Updated controllers, clients, and tests to use the new DTO structure. Removed redundant DTOs and cleaned up unused namespaces and comments. Improved documentation and ensured consistent naming conventions across the codebase.
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> HasZugferdAsync(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> HasZugferdAsync(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> ExtractZugferdAsync(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> ExtractZugferdAsync(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> ExtractZugferdAsResultAsync(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> ExtractZugferdAsResultAsync(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");
|
|
}
|
|
}
|