Introduced a new `SignatureCacheService` class to handle cached signatures via API interactions. This includes methods for saving, retrieving, and deleting signatures using `HttpClient`. - Added dependency injection for `HttpClient` and `IOptions<ApiOptions>`. - Implemented `SaveSignatureAsync`, `GetSignatureAsync`, and `DeleteSignatureAsync` methods with error handling. - Utilized `Uri.EscapeDataString` for safe URL encoding. - Added support for HTTP operations with `System.Net.Http.Json`.
68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System.Net.Http.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using EnvelopeGenerator.ReceiverUI.Options;
|
|
using EnvelopeGenerator.ReceiverUI.Models;
|
|
|
|
namespace EnvelopeGenerator.ReceiverUI.Services;
|
|
|
|
/// <summary>
|
|
/// Client service for managing cached signatures via API.
|
|
/// </summary>
|
|
public class SignatureCacheService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
|
{
|
|
private readonly ApiOptions _api = apiOptions.Value;
|
|
|
|
public async Task SaveSignatureAsync(
|
|
string envelopeKey,
|
|
SignatureCaptureDto signature,
|
|
CancellationToken cancel = default)
|
|
{
|
|
var response = await http.PostAsJsonAsync(
|
|
$"{_api.BaseUrl}/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)
|
|
{
|
|
var response = await http.GetAsync(
|
|
$"{_api.BaseUrl}/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)
|
|
{
|
|
var response = await http.DeleteAsync(
|
|
$"{_api.BaseUrl}/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}");
|
|
}
|
|
}
|
|
}
|
|
|