From 5bdc552492f83ac5f109c620da15672c744a549e Mon Sep 17 00:00:00 2001 From: TekH Date: Sat, 30 May 2026 17:03:25 +0200 Subject: [PATCH] Add DocumentService for fetching PDF documents Introduced a new `DocumentService` class to handle fetching PDF document bytes from an API endpoint. The service uses `HttpClient` for HTTP communication and `IOptions` for accessing API configuration. Added the `GetDocumentAsync` method to perform the HTTP GET request, handle responses, and return the document bytes along with the HTTP status code. Included necessary `using` directives and encapsulated the service in the `EnvelopeGenerator.ReceiverUI.Services` namespace. --- .../Services/DocumentService.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 EnvelopeGenerator.ReceiverUI/Services/DocumentService.cs diff --git a/EnvelopeGenerator.ReceiverUI/Services/DocumentService.cs b/EnvelopeGenerator.ReceiverUI/Services/DocumentService.cs new file mode 100644 index 00000000..57d44bfb --- /dev/null +++ b/EnvelopeGenerator.ReceiverUI/Services/DocumentService.cs @@ -0,0 +1,27 @@ +using System.Net; +using System.Net.Http; +using Microsoft.Extensions.Options; +using EnvelopeGenerator.ReceiverUI.Options; + +namespace EnvelopeGenerator.ReceiverUI.Services; + +public class DocumentService(HttpClient http, IOptions apiOptions) +{ + private readonly ApiOptions _api = apiOptions.Value; + + /// + /// Fetches the PDF bytes for the given envelope key from the API. + /// Returns null bytes with the HTTP status code on failure. + /// + public async Task<(byte[]? Bytes, HttpStatusCode StatusCode)> GetDocumentAsync(string envelopeKey, CancellationToken cancel = default) + { + var response = await http.GetAsync($"{_api.BaseUrl}/api/Document/{Uri.EscapeDataString(envelopeKey)}", cancel); + + if (!response.IsSuccessStatusCode) + return (null, response.StatusCode); + + var bytes = await response.Content.ReadAsByteArrayAsync(cancel); + return (bytes, response.StatusCode); + } +} +