Refactor services to remove ApiOptions dependency

Simplified `DocReceiverElementService` and `EnvelopeService` by removing the `ApiOptions` dependency. Updated constructors to eliminate the `IOptions<ApiOptions>` parameter and switched to using relative URLs directly for API requests.

Removed unused `BaseUrl` and `UsePredefinedReports` properties from `ApiOptions`. Cleaned up redundant fields, constructor logic, and unused `using` directives in affected services.

Registered `EnvelopeService` in `Program.cs` with `AddScoped`. These changes improve maintainability, reduce configuration overhead, and make the services more self-contained.
This commit is contained in:
2026-06-28 20:16:34 +02:00
parent a4b218b9f3
commit 5a30bc050b
4 changed files with 6 additions and 20 deletions

View File

@@ -4,7 +4,5 @@ public class ApiOptions
{ {
public const string SectionName = "Api"; public const string SectionName = "Api";
public string BaseUrl { get; set; } = string.Empty;
public bool UsePredefinedReports { get; set; } = false; public bool UsePredefinedReports { get; set; } = false;
} }

View File

@@ -32,6 +32,7 @@ builder.Services.AddScoped<EnvelopeReceiverService>();
builder.Services.AddScoped<SignatureService>(); builder.Services.AddScoped<SignatureService>();
builder.Services.AddScoped<SignatureCacheService>(); builder.Services.AddScoped<SignatureCacheService>();
builder.Services.AddSingleton<AppVersionService>(); builder.Services.AddSingleton<AppVersionService>();
builder.Services.AddScoped<EnvelopeService>();
// DevExpress WASM // DevExpress WASM
builder.Services.AddDevExpressWebAssemblyBlazorPdfViewer(); builder.Services.AddDevExpressWebAssemblyBlazorPdfViewer();

View File

@@ -1,18 +1,16 @@
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
using EnvelopeGenerator.Server.Client.Models; using EnvelopeGenerator.Server.Client.Models;
using EnvelopeGenerator.Server.Client.Options;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Server.Client.Services; namespace EnvelopeGenerator.Server.Client.Services;
public class DocReceiverElementService(HttpClient http, IOptions<ApiOptions> apiOptions) public class DocReceiverElementService(HttpClient http)
{ {
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
public async Task<IReadOnlyList<SignatureDto>> GetAsync(string envelopeKey, CancellationToken cancel = default) public async Task<IReadOnlyList<SignatureDto>> GetAsync(string envelopeKey, CancellationToken cancel = default)
{ {
var url = $"{apiOptions.Value.BaseUrl}/api/DocReceiverElement/{Uri.EscapeDataString(envelopeKey)}"; var url = $"/api/DocReceiverElement/{Uri.EscapeDataString(envelopeKey)}";
var response = await http.GetAsync(url, cancel); var response = await http.GetAsync(url, cancel);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)

View File

@@ -1,28 +1,17 @@
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json; using System.Text.Json;
using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Server.Client.Models;
using EnvelopeGenerator.Server.Client.Options;
using Microsoft.AspNetCore.WebUtilities; using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Server.Client.Services; namespace EnvelopeGenerator.Server.Client.Services;
/// <summary> /// <summary>
/// Retrieves <see cref="EnvelopeDto"/>s from the API. /// Retrieves <see cref="EnvelopeDto"/>s from the API.
/// </summary> /// </summary>
public class EnvelopeService public class EnvelopeService(HttpClient http)
{ {
private readonly HttpClient _http;
private readonly ApiOptions _apiOptions;
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
public EnvelopeService(HttpClient http, IOptions<ApiOptions> apiOptions)
{
_http = http;
_apiOptions = apiOptions.Value;
}
/// <summary> /// <summary>
/// Fetches envelopes from the API with optional filters. /// Fetches envelopes from the API with optional filters.
/// </summary> /// </summary>
@@ -34,7 +23,7 @@ public class EnvelopeService
bool? onlyCompleted = null, bool? onlyCompleted = null,
CancellationToken cancel = default) CancellationToken cancel = default)
{ {
var baseUrl = $"{_apiOptions.BaseUrl}/api/Envelope"; var baseUrl = $"/api/Envelope";
var queryParams = new Dictionary<string, string?>(); var queryParams = new Dictionary<string, string?>();
if (id.HasValue) if (id.HasValue)
@@ -56,7 +45,7 @@ public class EnvelopeService
var url = QueryHelpers.AddQueryString(baseUrl, queryParams); var url = QueryHelpers.AddQueryString(baseUrl, queryParams);
var response = await _http.GetAsync(url, cancel); var response = await http.GetAsync(url, cancel);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {