From 95c8e15887ae3980b43ea64314d4e15cc57eff18 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 15 Jun 2026 15:40:59 +0200 Subject: [PATCH] Add EnvelopeDto and EnvelopeService for API integration Introduced the `EnvelopeDto` class to represent envelope data with JSON property mappings. Added the `EnvelopeService` class to handle API interactions, including fetching envelopes with optional filters and query string construction using `Microsoft.AspNetCore.WebUtilities`. Updated the project file to include the required package reference for query string manipulation. --- .../EnvelopeGenerator.ReceiverUI.csproj | 1 + .../Models/EnvelopeDto.cs | 24 +++++++ .../Services/EnvelopeService.cs | 72 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 EnvelopeGenerator.ReceiverUI/Models/EnvelopeDto.cs create mode 100644 EnvelopeGenerator.ReceiverUI/Services/EnvelopeService.cs diff --git a/EnvelopeGenerator.ReceiverUI/EnvelopeGenerator.ReceiverUI.csproj b/EnvelopeGenerator.ReceiverUI/EnvelopeGenerator.ReceiverUI.csproj index aa225212..b8190cc9 100644 --- a/EnvelopeGenerator.ReceiverUI/EnvelopeGenerator.ReceiverUI.csproj +++ b/EnvelopeGenerator.ReceiverUI/EnvelopeGenerator.ReceiverUI.csproj @@ -29,6 +29,7 @@ + diff --git a/EnvelopeGenerator.ReceiverUI/Models/EnvelopeDto.cs b/EnvelopeGenerator.ReceiverUI/Models/EnvelopeDto.cs new file mode 100644 index 00000000..787742d1 --- /dev/null +++ b/EnvelopeGenerator.ReceiverUI/Models/EnvelopeDto.cs @@ -0,0 +1,24 @@ +using System.Text.Json.Serialization; + +namespace EnvelopeGenerator.ReceiverUI.Models; + +public class EnvelopeDto +{ + [JsonPropertyName("id")] + public int Id { get; set; } + + [JsonPropertyName("uuid")] + public string? Uuid { get; set; } + + [JsonPropertyName("title")] + public string? Title { get; set; } + + [JsonPropertyName("status")] + public int Status { get; set; } + + [JsonPropertyName("docResult")] + public byte[]? DocResult { get; set; } + + [JsonPropertyName("envelopeReceivers")] + public List EnvelopeReceivers { get; set; } = new(); +} diff --git a/EnvelopeGenerator.ReceiverUI/Services/EnvelopeService.cs b/EnvelopeGenerator.ReceiverUI/Services/EnvelopeService.cs new file mode 100644 index 00000000..2e61f47d --- /dev/null +++ b/EnvelopeGenerator.ReceiverUI/Services/EnvelopeService.cs @@ -0,0 +1,72 @@ +using System.Net.Http.Json; +using System.Text.Json; +using EnvelopeGenerator.ReceiverUI.Models; +using EnvelopeGenerator.ReceiverUI.Options; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.Extensions.Options; + +namespace EnvelopeGenerator.ReceiverUI.Services; + +/// +/// Retrieves s from the API. +/// +public class EnvelopeService +{ + private readonly HttpClient _http; + private readonly ApiOptions _apiOptions; + private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); + + public EnvelopeService(HttpClient http, IOptions apiOptions) + { + _http = http; + _apiOptions = apiOptions.Value; + } + + /// + /// Fetches envelopes from the API with optional filters. + /// + /// Thrown when the API request fails. + public async Task?> GetAsync( + int? id = null, + string? uuid = null, + bool? onlyActive = null, + bool? onlyCompleted = null, + CancellationToken cancel = default) + { + var baseUrl = $"{_apiOptions.BaseUrl}/api/Envelope"; + var queryParams = new Dictionary(); + + if (id.HasValue) + { + queryParams["Id"] = id.Value.ToString(); + } + if (!string.IsNullOrEmpty(uuid)) + { + queryParams["Uuid"] = uuid; + } + if (onlyActive.HasValue) + { + queryParams["OnlyActive"] = onlyActive.Value.ToString(); + } + if (onlyCompleted.HasValue) + { + queryParams["OnlyCompleted"] = onlyCompleted.Value.ToString(); + } + + var url = QueryHelpers.AddQueryString(baseUrl, queryParams); + + var response = await _http.GetAsync(url, cancel); + + if (!response.IsSuccessStatusCode) + { + var statusCode = (int)response.StatusCode; + var reasonPhrase = response.ReasonPhrase ?? "Unknown error"; + throw new HttpRequestException( + $"Failed to load envelopes. Status: {statusCode} ({reasonPhrase})", + null, + response.StatusCode); + } + + return await response.Content.ReadFromJsonAsync>(_jsonOptions, cancel); + } +}