First successfull build

This commit is contained in:
OlgunR
2026-03-19 12:35:23 +01:00
parent 7271a92d32
commit 4f3c66b4f7
30 changed files with 767 additions and 105 deletions

View File

@@ -1,6 +1,38 @@
namespace EnvelopeGenerator.ReceiverUI.Client.Services.Base
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 class ApiResponse
{
}
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>
/// 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 };
}

View File

@@ -1,6 +1,110 @@
namespace EnvelopeGenerator.ReceiverUI.Client.Services.Base
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
namespace EnvelopeGenerator.ReceiverUI.Client.Services.Base;
/// <summary>
/// Basisklasse für ALLE API-Services.
///
/// WARUM eine Basisklasse?
/// - Einheitliches Error-Handling: Jeder API-Aufruf wird gleich behandelt
/// - DRY (Don't Repeat Yourself): Logging, Fehlerbehandlung, Serialisierung nur einmal
/// - Einfache Erweiterung: Retry-Logik, Token-Refresh etc. nur hier ändern
/// </summary>
public abstract class ApiServiceBase
{
public class ApiServiceBase
protected readonly HttpClient Http;
protected readonly ILogger Logger;
protected ApiServiceBase(HttpClient http, ILogger logger)
{
Http = http;
Logger = logger;
}
}
/// <summary>
/// GET-Request mit Deserialisierung.
/// Alle API GET-Aufrufe gehen durch diese Methode.
/// </summary>
protected async Task<ApiResponse<T>> GetAsync<T>(string endpoint, CancellationToken ct = default)
{
try
{
var response = await Http.GetAsync(endpoint, ct);
if (!response.IsSuccessStatusCode)
{
var errorBody = await response.Content.ReadAsStringAsync(ct);
Logger.LogWarning("GET {Endpoint} failed: {Status} - {Body}",
endpoint, (int)response.StatusCode, errorBody);
return ApiResponse<T>.Failure((int)response.StatusCode, errorBody);
}
var data = await response.Content.ReadFromJsonAsync<T>(cancellationToken: ct);
return ApiResponse<T>.Success(data!, (int)response.StatusCode);
}
catch (HttpRequestException ex)
{
Logger.LogError(ex, "HTTP error calling GET {Endpoint}", endpoint);
return ApiResponse<T>.Failure(0, "Verbindung zum Server fehlgeschlagen.");
}
catch (TaskCanceledException)
{
Logger.LogWarning("GET {Endpoint} was cancelled", endpoint);
return ApiResponse<T>.Failure(0, "Anfrage abgebrochen.");
}
}
/// <summary>
/// POST-Request mit Body und Response-Deserialisierung.
/// </summary>
protected async Task<ApiResponse<TResponse>> PostAsync<TRequest, TResponse>(
string endpoint, TRequest body, CancellationToken ct = default)
{
try
{
var response = await Http.PostAsJsonAsync(endpoint, body, ct);
if (!response.IsSuccessStatusCode)
{
var errorBody = await response.Content.ReadAsStringAsync(ct);
Logger.LogWarning("POST {Endpoint} failed: {Status} - {Body}",
endpoint, (int)response.StatusCode, errorBody);
return ApiResponse<TResponse>.Failure((int)response.StatusCode, errorBody);
}
var data = await response.Content.ReadFromJsonAsync<TResponse>(cancellationToken: ct);
return ApiResponse<TResponse>.Success(data!, (int)response.StatusCode);
}
catch (HttpRequestException ex)
{
Logger.LogError(ex, "HTTP error calling POST {Endpoint}", endpoint);
return ApiResponse<TResponse>.Failure(0, "Verbindung zum Server fehlgeschlagen.");
}
}
/// <summary>
/// POST-Request ohne Response-Body (z.B. Logout).
/// </summary>
protected async Task<ApiResponse> PostAsync<TRequest>(
string endpoint, TRequest body, CancellationToken ct = default)
{
try
{
var response = await Http.PostAsJsonAsync(endpoint, body, ct);
if (!response.IsSuccessStatusCode)
{
var errorBody = await response.Content.ReadAsStringAsync(ct);
return ApiResponse.Failure((int)response.StatusCode, errorBody);
}
return ApiResponse.Success((int)response.StatusCode);
}
catch (HttpRequestException ex)
{
Logger.LogError(ex, "HTTP error calling POST {Endpoint}", endpoint);
return ApiResponse.Failure(0, "Verbindung zum Server fehlgeschlagen.");
}
}
}