using Microsoft.AspNetCore.Components.Authorization; namespace OrdersManagement.Services; public class ServiceBase where T : class { private readonly AuthenticationStateProvider _authenticationStateProvider; private readonly HttpClient _httpClient; protected ServiceBase(IHttpClientFactory httpClientFactory, AuthenticationStateProvider authenticationStateProvider) { _authenticationStateProvider = authenticationStateProvider; _httpClient = httpClientFactory.CreateClient("FaKrosnoApi"); _ = Configure(); } protected async Task?> GetListAsync(string request) { var response = await _httpClient.GetAsync(request); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync>(); } protected async Task GetByIdAsync(string request) { var response = await _httpClient.GetAsync(request); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync(); } protected async Task PostAsync(string request) { var response = await _httpClient.PostAsync(request, null); response.EnsureSuccessStatusCode(); return response; } private async Task Configure() { var token = await GetToken(); _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); } private async Task GetToken() { await ((CustomAuthenticationStateProvider)_authenticationStateProvider).InitializeAsync(); return ((CustomAuthenticationStateProvider)_authenticationStateProvider).GetToken(); } }