Refactor AuthService and add new service classes
Refactored `AuthService` to introduce a reusable `CreateDefaultClient` method, reducing code duplication. Updated all relevant methods in `AuthService` to use this new method. Added `CultureService` to manage application culture/localization, including support for setting, getting, and initializing culture from `localStorage` or browser settings. Introduced `DocReceiverElementService` for retrieving document receiver elements (signatures) and `EnvelopeService` for managing envelope data retrieval with optional filters. Both services include error handling and consistent JSON deserialization. These changes improve code maintainability, reusability, and adhere to the single responsibility principle.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
@@ -10,6 +9,7 @@ public enum SenderLoginResult { Success, InvalidCredentials, Error }
|
||||
|
||||
public class AuthService(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
private HttpClient CreateDefaultClient() => httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the current user holds a valid receiver token for the given envelope key.
|
||||
@@ -17,7 +17,7 @@ public class AuthService(IHttpClientFactory httpClientFactory)
|
||||
/// </summary>
|
||||
public async Task<bool> CheckEnvelopeAccessAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
using var http = CreateDefaultClient();
|
||||
var response = await http.GetAsync($"/api/auth/check/envelope/{Uri.EscapeDataString(envelopeKey)}", cancel);
|
||||
return response.StatusCode == HttpStatusCode.OK;
|
||||
}
|
||||
@@ -29,9 +29,11 @@ public class AuthService(IHttpClientFactory httpClientFactory)
|
||||
/// </summary>
|
||||
public async Task<EnvelopeLoginResult> LoginEnvelopeReceiverAsync(string envelopeKey, string accessCode, CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var form = new MultipartFormDataContent();
|
||||
form.Add(new StringContent(accessCode), "AccessCode");
|
||||
using var http = CreateDefaultClient();
|
||||
var form = new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(accessCode), "AccessCode" }
|
||||
};
|
||||
|
||||
var response = await http.PostAsync(
|
||||
$"/api/Auth/envelope-receiver/{Uri.EscapeDataString(envelopeKey)}",
|
||||
@@ -52,7 +54,7 @@ public class AuthService(IHttpClientFactory httpClientFactory)
|
||||
/// </summary>
|
||||
public async Task<bool> LogoutEnvelopeReceiverAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
using var http = CreateDefaultClient();
|
||||
var response = await http.PostAsync(
|
||||
$"/api/auth/logout/envelope/{Uri.EscapeDataString(envelopeKey)}",
|
||||
null, cancel);
|
||||
@@ -66,7 +68,7 @@ public class AuthService(IHttpClientFactory httpClientFactory)
|
||||
/// </summary>
|
||||
public async Task<SenderLoginResult> LoginSenderAsync(string username, string password, CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
using var http = CreateDefaultClient();
|
||||
var requestBody = new { username, password };
|
||||
|
||||
var response = await http.PostAsJsonAsync(
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.Globalization;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for managing application culture/localization.
|
||||
/// </summary>
|
||||
public class CultureService
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
private const string CULTURE_KEY = "AppCulture";
|
||||
|
||||
public CultureService(IJSRuntime jsRuntime)
|
||||
{
|
||||
_jsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of supported cultures.
|
||||
/// </summary>
|
||||
public static CultureInfo[] SupportedCultures { get; } = new[]
|
||||
{
|
||||
new CultureInfo("de-DE"),
|
||||
new CultureInfo("en-US"),
|
||||
new CultureInfo("fr-FR")
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Sets the application culture and stores it in localStorage.
|
||||
/// </summary>
|
||||
public async Task SetCultureAsync(string culture)
|
||||
{
|
||||
if (!SupportedCultures.Any(c => c.Name == culture))
|
||||
throw new ArgumentException($"Culture '{culture}' is not supported.", nameof(culture));
|
||||
|
||||
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", CULTURE_KEY, culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the stored culture from localStorage.
|
||||
/// </summary>
|
||||
public async Task<string?> GetCultureAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", CULTURE_KEY);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the culture from localStorage or browser settings.
|
||||
/// </summary>
|
||||
public async Task<CultureInfo> InitializeCultureAsync()
|
||||
{
|
||||
var storedCulture = await GetCultureAsync();
|
||||
|
||||
if (!string.IsNullOrEmpty(storedCulture) &&
|
||||
SupportedCultures.Any(c => c.Name == storedCulture))
|
||||
{
|
||||
return new CultureInfo(storedCulture);
|
||||
}
|
||||
|
||||
// Fallback to browser culture or default
|
||||
var browserCulture = CultureInfo.CurrentCulture.Name;
|
||||
var matchedCulture = SupportedCultures.FirstOrDefault(c => c.Name == browserCulture);
|
||||
|
||||
return matchedCulture ?? SupportedCultures[0]; // Default to German
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using EnvelopeGenerator.Server.Client.Models;
|
||||
using EnvelopeGenerator.Server.Client.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
|
||||
public class DocReceiverElementService(HttpClient http, IOptions<ApiOptions> apiOptions)
|
||||
{
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web);
|
||||
|
||||
public async Task<IReadOnlyList<SignatureDto>> GetAsync(string envelopeKey, CancellationToken cancel = default)
|
||||
{
|
||||
var url = $"{apiOptions.Value.BaseUrl}/api/DocReceiverElement/{Uri.EscapeDataString(envelopeKey)}";
|
||||
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 ?? [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using EnvelopeGenerator.Server.Client.Models;
|
||||
using EnvelopeGenerator.Server.Client.Options;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace EnvelopeGenerator.Server.Client.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves <see cref="EnvelopeDto"/>s from the API.
|
||||
/// </summary>
|
||||
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> apiOptions)
|
||||
{
|
||||
_http = http;
|
||||
_apiOptions = apiOptions.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches envelopes from the API with optional filters.
|
||||
/// </summary>
|
||||
/// <exception cref="HttpRequestException">Thrown when the API request fails.</exception>
|
||||
public async Task<IEnumerable<EnvelopeDto>?> 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<string, string?>();
|
||||
|
||||
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<IEnumerable<EnvelopeDto>>(_jsonOptions, cancel);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user