Files
EnvelopeGenerator/EnvelopeGenerator.ReceiverUI/EnvelopeGenerator.ReceiverUI.Client/Services/Base/ApiResponse.cs
OlgunR 93488ba83e Refactor envelope receiver auth flow and state handling
- Introduce IReceiverAuthService and ReceiverAuthService for all envelope receiver authentication and status API calls
- Add ReceiverAuthModel as a client-side DTO for API responses
- Refactor EnvelopeState to store all relevant fields and update via ApplyApiResponse
- Overhaul EnvelopePage.razor to use new service and state, with improved status handling and UI
- Enhance ApiResponse and ApiServiceBase to support structured error deserialization
- Register IReceiverAuthService in DI container
2026-03-23 15:57:20 +01:00

46 lines
1.8 KiB
C#

namespace EnvelopeGenerator.ReceiverUI.Client.Services.Base;
/// <summary>
/// Einheitliches Response-Objekt für ALLE API-Aufrufe.
///
/// WARUM: Jeder API-Aufruf kann fehlschlagen (Netzwerk, 401, 500...).
/// Statt überall try-catch zu haben, kapselt dieses Objekt Erfolg/Fehler einheitlich.
/// So kann jede Blazor-Komponente einheitlich darauf reagieren.
/// </summary>
public record ApiResponse<T>
{
public bool IsSuccess { get; init; }
public T? Data { get; init; }
public int StatusCode { get; init; }
public string? ErrorMessage { get; init; }
public static ApiResponse<T> Success(T data, int statusCode = 200)
=> new() { IsSuccess = true, Data = data, StatusCode = statusCode };
public static ApiResponse<T> Failure(int statusCode, string? error = null)
=> new() { IsSuccess = false, StatusCode = statusCode, ErrorMessage = error };
/// <summary>
/// Failure mit deserialisiertem Body — für Fälle wo die API
/// bei 401/404 trotzdem ein strukturiertes JSON zurückgibt
/// (z.B. ReceiverAuthResponse mit ErrorMessage + Status).
/// </summary>
public static ApiResponse<T> Failure(int statusCode, string? error, T? data)
=> new() { IsSuccess = false, StatusCode = statusCode, ErrorMessage = error, Data = data };
}
/// <summary>
/// Response ohne Daten (für POST/PUT/DELETE die nur Status zurückgeben).
/// </summary>
public record ApiResponse
{
public bool IsSuccess { get; init; }
public int StatusCode { get; init; }
public string? ErrorMessage { get; init; }
public static ApiResponse Success(int statusCode = 200)
=> new() { IsSuccess = true, StatusCode = statusCode };
public static ApiResponse Failure(int statusCode, string? error = null)
=> new() { IsSuccess = false, StatusCode = statusCode, ErrorMessage = error };
}