Replaced direct injection of HttpClient with IHttpClientFactory across the codebase to improve HTTP client management and align with best practices. Removed dependency on ApiOptions and IOptions<ApiOptions> in multiple services, simplifying constructors and reducing configuration complexity. Updated FontLoader to use IHttpClientFactory for font loading with relative paths. Adjusted comments and documentation to reflect these changes. Cleaned up unused using directives related to ApiOptions.
68 lines
2.4 KiB
C#
68 lines
2.4 KiB
C#
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using EnvelopeGenerator.Server.Client.Models;
|
|
|
|
namespace EnvelopeGenerator.Server.Client.Services;
|
|
|
|
/// <summary>
|
|
/// Client service for managing cached signatures via API.
|
|
/// </summary>
|
|
public class SignatureCacheService(IHttpClientFactory httpClientFactory)
|
|
{
|
|
|
|
public async Task SaveSignatureAsync(
|
|
string envelopeKey,
|
|
SignatureCaptureDto signature,
|
|
CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.PostAsJsonAsync(
|
|
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
|
signature,
|
|
cancel);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync(cancel);
|
|
throw new HttpRequestException($"Failed to cache signature: {response.StatusCode} - {error}");
|
|
}
|
|
}
|
|
|
|
public async Task<SignatureCaptureDto?> GetSignatureAsync(
|
|
string envelopeKey,
|
|
CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.GetAsync(
|
|
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
|
cancel);
|
|
|
|
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
|
|
return null;
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync(cancel);
|
|
throw new HttpRequestException($"Failed to retrieve signature: {response.StatusCode} - {error}");
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<SignatureCaptureDto>(cancellationToken: cancel);
|
|
}
|
|
|
|
public async Task DeleteSignatureAsync(
|
|
string envelopeKey,
|
|
CancellationToken cancel = default)
|
|
{
|
|
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
|
var response = await http.DeleteAsync(
|
|
$"/api/Cache/SignatureCapture/{Uri.EscapeDataString(envelopeKey)}",
|
|
cancel);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync(cancel);
|
|
throw new HttpRequestException($"Failed to delete signature: {response.StatusCode} - {error}");
|
|
}
|
|
}
|
|
}
|