Refactored `DocReceiverElementService` and `EnvelopeService` to use `IHttpClientFactory` instead of directly injecting `HttpClient`, improving flexibility and testability. Updated constructors to accept `IHttpClientFactory` and replaced direct `HttpClient` usage with named clients (`"EnvelopeGenerator.Server"`). Adjusted methods to use the factory-created clients for HTTP requests. Added `using Microsoft.Extensions.Options;` in `Program.cs` and registered `EnvelopeService` and `DocReceiverElementService` in the dependency injection container for proper resolution. Clarified their usage in SSR scenarios with comments. Removed redundant `using` directives and aligned imports with the updated implementation. These changes enhance maintainability, scalability, and testability by leveraging `IHttpClientFactory` for better HTTP client management and dependency injection.
25 lines
1002 B
C#
25 lines
1002 B
C#
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using EnvelopeGenerator.Server.Client.Models;
|
|
|
|
namespace EnvelopeGenerator.Server.Client.Services;
|
|
|
|
public class DocReceiverElementService(IHttpClientFactory clientFactory)
|
|
{
|
|
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
|
|
|
|
public async Task<IReadOnlyList<SignatureDto>> GetAsync(string envelopeKey, CancellationToken cancel = default)
|
|
{
|
|
var url = $"/api/DocReceiverElement/{Uri.EscapeDataString(envelopeKey)}";
|
|
|
|
var http = clientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.GetAsync(url, cancel);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
throw new HttpRequestException($"Failed to retrieve signatures for envelope {envelopeKey}: {response.StatusCode} {response.ReasonPhrase}");
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<List<SignatureDto>>(_jsonOptions, cancel);
|
|
return result ?? [];
|
|
}
|
|
}
|