Introduced a new `ReadDocumentDetailsQuery` to fetch document metadata (`DocumentDetailsDto`) without the binary payload. Updated `MappingProfile` to map `Document` to `DocumentDetailsDto`. Refactored `DocumentService` to handle separate fetching of metadata and binary data, combining them into `DocumentDto`. Changed the metadata endpoint to `/api/Document/details`. Added a new `GetDocumentDetails` endpoint in `DocumentController` to return metadata without binary data. Updated `EnvelopeReceiverPageDataService` with a new method `GetDocumentMetadataOfSenderAsync` for fetching metadata. These changes improve efficiency by allowing clients to load metadata without transferring large binary data when unnecessary.
98 lines
4.1 KiB
C#
98 lines
4.1 KiB
C#
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
|
|
namespace EnvelopeGenerator.Server.Client.Services;
|
|
|
|
public class DocumentService(IHttpClientFactory httpClientFactory)
|
|
{
|
|
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
|
|
|
|
/// <summary>
|
|
/// Fetches the PDF bytes for the given envelope key from the API.
|
|
/// Throws HttpRequestException on failure with appropriate status code.
|
|
/// </summary>
|
|
/// <exception cref="HttpRequestException">Thrown when the API request fails.</exception>
|
|
public async Task<byte[]?> GetDocumentAsync(string envelopeKey, CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.GetAsync($"/api/Document/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var statusCode = (int)response.StatusCode;
|
|
var reasonPhrase = response.ReasonPhrase ?? "Unknown error";
|
|
throw new HttpRequestException(
|
|
$"Failed to load document. Status: {statusCode} ({reasonPhrase})",
|
|
null,
|
|
response.StatusCode);
|
|
}
|
|
|
|
var bytes = await response.Content.ReadAsByteArrayAsync(cancel);
|
|
return bytes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches sender document details (including signature elements) by envelope id.
|
|
/// Uses GET /api/Document/details?EnvelopeId={envelopeId}.
|
|
/// </summary>
|
|
public async Task<DocumentDto?> GetSenderDocumentDetailsByEnvelopeIdAsync(int envelopeId, CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var detailsResponse = await http.GetAsync($"/api/Document/details?EnvelopeId={envelopeId}", cancel);
|
|
|
|
if (!detailsResponse.IsSuccessStatusCode)
|
|
{
|
|
var statusCode = (int)detailsResponse.StatusCode;
|
|
var reasonPhrase = detailsResponse.ReasonPhrase ?? "Unknown error";
|
|
throw new HttpRequestException(
|
|
$"Failed to load sender document details. Status: {statusCode} ({reasonPhrase})",
|
|
null,
|
|
detailsResponse.StatusCode);
|
|
}
|
|
|
|
var details = await detailsResponse.Content.ReadFromJsonAsync<DocumentDetailsDto>(_jsonOptions, cancel)
|
|
?? throw new HttpRequestException("Failed to deserialize sender document details response.");
|
|
|
|
var bytesResponse = await http.GetAsync($"/api/Document?EnvelopeId={envelopeId}", cancel);
|
|
|
|
if (!bytesResponse.IsSuccessStatusCode)
|
|
{
|
|
var statusCode = (int)bytesResponse.StatusCode;
|
|
var reasonPhrase = bytesResponse.ReasonPhrase ?? "Unknown error";
|
|
throw new HttpRequestException(
|
|
$"Failed to load sender document bytes. Status: {statusCode} ({reasonPhrase})",
|
|
null,
|
|
bytesResponse.StatusCode);
|
|
}
|
|
|
|
var byteData = await bytesResponse.Content.ReadAsByteArrayAsync(cancel);
|
|
|
|
return new DocumentDto(byteData, details);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches sender document bytes by envelope id.
|
|
/// Uses GET /api/Document?EnvelopeId={envelopeId}.
|
|
/// </summary>
|
|
public async Task<byte[]?> GetSenderDocumentBytesByEnvelopeIdAsync(int envelopeId, CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.GetAsync($"/api/Document?EnvelopeId={envelopeId}", cancel);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var statusCode = (int)response.StatusCode;
|
|
var reasonPhrase = response.ReasonPhrase ?? "Unknown error";
|
|
throw new HttpRequestException(
|
|
$"Failed to load sender document bytes. Status: {statusCode} ({reasonPhrase})",
|
|
null,
|
|
response.StatusCode);
|
|
}
|
|
|
|
return await response.Content.ReadAsByteArrayAsync(cancel);
|
|
}
|
|
}
|