using Data.Models; using Data.Models.Interfaces; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using System.Net.Http.Json; using System.Text.Json; namespace BlazorWebAssembly.Client; public class BlogApiWebClient : IBlogApi { private readonly IHttpClientFactory _factory; public BlogApiWebClient(IHttpClientFactory factory) { _factory = factory; } public async Task GetBlogPostAsync(string id) { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync($"api/BlogPosts/{id}"); } public async Task GetBlogPostCountAsync() { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync("/api/BlogPostCount"); } public async Task?> GetBlogPostsAsync(int numberofposts, int startindex) { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync>($"/api/BlogPosts?numberofposts={numberofposts}&startindex={startindex}"); } public async Task SaveBlogPostAsync(BlogPost item) { try { var httpclient = _factory.CreateClient("Authenticated"); var response = await httpclient.PutAsJsonAsync ("api/BlogPosts", item); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(json); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } return null; } public async Task DeleteBlogPostAsync(string id) { try { var httpclient = _factory.CreateClient("Authenticated"); await httpclient.DeleteAsync($"api/BlogPosts/{id}"); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } } public async Task?> GetCategoriesAsync() { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync>($"api/Categories"); } public async Task GetCategoryAsync(string id) { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync($"api/Categories/{id}"); } public async Task DeleteCategoryAsync(string id) { try { var httpclient = _factory.CreateClient("Authenticated"); await httpclient.DeleteAsync($"api/Categories/{id}"); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } } public async Task SaveCategoryAsync(Category item) { try { var httpclient = _factory.CreateClient("Authenticated"); var response = await httpclient.PutAsJsonAsync("api/Categories", item); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(json); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } return null; } public async Task GetTagAsync(string id) { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync($"api/Tags/{id}"); } public async Task?> GetTagsAsync() { var httpclient = _factory.CreateClient("Public"); return await httpclient.GetFromJsonAsync>($"api/Tags"); } public async Task DeleteTagAsync(string id) { try { var httpclient = _factory.CreateClient("Authenticated"); await httpclient.DeleteAsync($"api/Tags/{id}"); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } } public async Task SaveTagAsync(Tag item) { try { var httpclient = _factory.CreateClient("Authenticated"); var response = await httpclient.PutAsJsonAsync("api/Tags", item); var json = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(json); } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } return null; } public Task InvalidateCacheAsync() { throw new NotImplementedException(); } }