Path: redb/redb.Templates/templates/redb-app/RedbApp.Web/Services/ApiClient.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-app/RedbApp.Web/Services/ApiClient.cs ---- using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using Microsoft.AspNetCore.Components; using RedbApp.Models; namespace RedbApp.Web.Services; /// /// The API calls of the client. Every call carries the token of the ; a 401 answer /// means the token is gone or expired, so the session ends and the user is sent to the sign-in page. /// public sealed class ApiClient(HttpClient http, Session session, NavigationManager navigation) { public async Task LoginAsync(LoginRequest request) { using var response = await http.PostAsJsonAsync("api/auth/login", request); if (!response.IsSuccessStatusCode) return await ErrorOf(response); session.SignIn((await response.Content.ReadFromJsonAsync())!); return null; } public Task ProductsAsync(string? search, bool inStockOnly, string sort, int page) => GetAsync($"api/products?search={Uri.EscapeDataString(search ?? "")}" + $"&inStock={(inStockOnly ? "true" : "false")}&sort={Uri.EscapeDataString(sort)}&page={page}"); public Task ProductAsync(long id) => GetAsync($"api/products/{id}"); public Task SaveProductAsync(ProductDto product) => product.Id == 0 ? SendAsync(HttpMethod.Post, "api/products", product) : SendAsync(HttpMethod.Put, $"api/products/{product.Id}", product); public Task DeleteProductAsync(long id) => SendAsync(HttpMethod.Delete, $"api/products/{id}", null); public Task> CategoriesAsync() => GetAsync>("api/categories"); public Task AddCategoryAsync(NewCategory category) => SendAsync(HttpMethod.Post, "api/categories", category); public Task DeleteCategoryAsync(long id) => SendAsync(HttpMethod.Delete, $"api/categories/{id}", null); private async Task GetAsync(string uri) { using var response = await SendRawAsync(HttpMethod.Get, uri, null); return (await response.Content.ReadFromJsonAsync())!; } private async Task SendAsync(HttpMethod method, string uri, object? body) { using var response = await SendRawAsync(method, uri, body); } private async Task SendRawAsync(HttpMethod method, string uri, object? body) { using var request = new HttpRequestMessage(method, uri); if (session.Token is { } token) request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); if (body is not null) request.Content = JsonContent.Create(body, body.GetType()); var response = await http.SendAsync(request); if (response.StatusCode == HttpStatusCode.Unauthorized) { response.Dispose(); session.SignOut(); navigation.NavigateTo("login"); throw new ApiException("Your session has ended. Please sign in again."); } if (!response.IsSuccessStatusCode) { var message = await ErrorOf(response); response.Dispose(); throw new ApiException(message); } return response; } private static async Task ErrorOf(HttpResponseMessage response) { var error = response.Content.Headers.ContentType?.MediaType == "application/json" ? await response.Content.ReadFromJsonAsync() : null; return error?.Message ?? $"The server answered {(int)response.StatusCode} {response.ReasonPhrase}."; } } /// An API call that did not succeed; the message is meant for the user. public sealed class ApiException(string message) : Exception(message);