Use Refit for APIs, make data/API stack async
This commit is contained in:
parent
9c4f01ab13
commit
b00158daa7
17 changed files with 196 additions and 124 deletions
|
|
@ -1,6 +1,8 @@
|
|||
@using Start.Shared
|
||||
@using System.IO
|
||||
@inject HttpClient Http
|
||||
@using Start.Shared.Api
|
||||
@using Refit
|
||||
|
||||
@inject IBookmarkContainersApi bookmarkContainersApi
|
||||
|
||||
<Dialog Title="Create Container" Active="this.IsOpen" OnClose="this.OnDialogClose">
|
||||
<EditForm Model="this.model" OnValidSubmit="this.OnSubmit">
|
||||
|
|
@ -50,16 +52,10 @@
|
|||
|
||||
protected async void OnSubmit()
|
||||
{
|
||||
HttpResponseMessage response = await Http
|
||||
.PostAsJsonAsync("BookmarkContainers/Create", model.Title);
|
||||
ApiResponse<BookmarkContainerDto?> apiResponse = await bookmarkContainersApi
|
||||
.CreateBookmarkContainer(model.Title);
|
||||
|
||||
Stream stream = response.RequestMessage!.Content!.ReadAsStream();
|
||||
StreamReader reader = new StreamReader(stream);
|
||||
Console.WriteLine(reader.ReadToEnd());
|
||||
|
||||
BookmarkContainerDto? container = await response
|
||||
!.Content
|
||||
!.ReadFromJsonAsync<BookmarkContainerDto>();
|
||||
BookmarkContainerDto? container = apiResponse.Content;
|
||||
|
||||
if (container == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
|
||||
@using Start.Shared.Api
|
||||
|
||||
@inject HttpClient Http
|
||||
@inject IBookmarkContainersApi bookmarkContainersApi
|
||||
|
||||
@{ string title = $"Delete Container \"{this.ContainerTitle}\""; }
|
||||
|
||||
|
|
@ -41,8 +44,8 @@
|
|||
{
|
||||
try
|
||||
{
|
||||
HttpResponseMessage result = await Http
|
||||
.DeleteAsync($"BookmarkContainers/Delete/{this.BookmarkContainerId}");
|
||||
HttpResponseMessage result = await bookmarkContainersApi
|
||||
.DeleteBookmarkContainer(this.BookmarkContainerId);
|
||||
|
||||
if (result.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,11 +1,16 @@
|
|||
@page "/Start"
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using System.Collections.Generic
|
||||
@using System.Linq
|
||||
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
|
||||
@using Start.Client.Components
|
||||
@using Start.Shared
|
||||
@attribute [Authorize]
|
||||
@inject HttpClient Http
|
||||
@using Start.Shared.Api
|
||||
@using Refit
|
||||
|
||||
@* Distiguish from Refit.Authorize *@
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@inject Blazored.LocalStorage.ILocalStorageService localStorage
|
||||
@inject IBookmarkContainersApi bookmarkContainersApi
|
||||
|
||||
@if (bookmarkContainers == null)
|
||||
{
|
||||
|
|
@ -101,9 +106,10 @@ else
|
|||
{
|
||||
try
|
||||
{
|
||||
this.bookmarkContainers = await Http
|
||||
.GetFromJsonAsync<IList<BookmarkContainerDto>>(
|
||||
"BookmarkContainers");
|
||||
ApiResponse<IEnumerable<BookmarkContainerDto>> response = await bookmarkContainersApi
|
||||
.GetAllBookmarkContainers();
|
||||
|
||||
this.bookmarkContainers = response.Content?.ToList();
|
||||
|
||||
if (this.bookmarkContainers == null || !this.bookmarkContainers.Any())
|
||||
{
|
||||
|
|
@ -120,13 +126,10 @@ else
|
|||
|
||||
protected async Task CreateDefaultContainer()
|
||||
{
|
||||
HttpResponseMessage response = await Http
|
||||
.PostAsJsonAsync("BookmarkContainers/Create", "Default");
|
||||
ApiResponse<BookmarkContainerDto?> response = await bookmarkContainersApi
|
||||
.CreateBookmarkContainer("Default");
|
||||
|
||||
BookmarkContainerDto? container = await response
|
||||
.RequestMessage
|
||||
!.Content
|
||||
!.ReadFromJsonAsync<BookmarkContainerDto?>();
|
||||
BookmarkContainerDto? container = response.Content;
|
||||
|
||||
if (container != null)
|
||||
await this.OnContainerSelected(container.BookmarkContainerId);
|
||||
|
|
@ -139,12 +142,13 @@ else
|
|||
if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId == bookmarkContainerId) ?? false)
|
||||
bookmarkContainerId = this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId;
|
||||
|
||||
BookmarkContainerDto? bookmarkContainer = await Http
|
||||
.GetFromJsonAsync<BookmarkContainerDto?>(
|
||||
$"BookmarkContainers/{bookmarkContainerId}");
|
||||
ApiResponse<BookmarkContainerDto?> response = await bookmarkContainersApi
|
||||
.GetBookmarkContainer(bookmarkContainerId);
|
||||
|
||||
BookmarkContainerDto? container = response.Content;
|
||||
|
||||
await this.SetSelectedContainer(bookmarkContainerId);
|
||||
this.selectedBookmarkContainer = bookmarkContainer;
|
||||
this.selectedBookmarkContainer = container;
|
||||
}
|
||||
catch (AccessTokenNotAvailableException e)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
|
|||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Blazored.LocalStorage;
|
||||
using Refit;
|
||||
using Start.Shared.Api;
|
||||
|
||||
namespace Start.Client {
|
||||
public class Program {
|
||||
|
|
@ -16,12 +18,26 @@ namespace Start.Client {
|
|||
client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
|
||||
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
|
||||
|
||||
// Supply HttpClient instances that include access tokens when making requests to the server project
|
||||
// Supply HttpClient instances that include access tokens when making requests to the
|
||||
// server project
|
||||
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
|
||||
.CreateClient("Start.ServerAPI"));
|
||||
.CreateClient("Start.ServerAPI"));
|
||||
|
||||
// Blazor will throw an error if a relative URI is used, so we have to get the base
|
||||
// address for building the API paths
|
||||
Uri baseUri = new(builder.HostEnvironment.BaseAddress);
|
||||
|
||||
builder.Services.AddRefitClient<IBookmarkContainersApi>()
|
||||
.ConfigureHttpClient(c => {
|
||||
c.BaseAddress = new Uri(baseUri, "BookmarkContainers");
|
||||
})
|
||||
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
|
||||
|
||||
builder.Services.AddRefitClient<IBookmarksApi>()
|
||||
.ConfigureHttpClient(c => { c.BaseAddress = new Uri(baseUri, "Bookmarks"); })
|
||||
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
|
||||
|
||||
builder.Services.AddApiAuthorization();
|
||||
|
||||
builder.Services.AddBlazoredLocalStorage();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
|
||||
<PackageReference Include="Blazored.LocalStorage" Version="4.1.5" />
|
||||
<PackageReference Include="Refit" Version="6.1.15" />
|
||||
<PackageReference Include="Refit.HttpClientFactory" Version="6.1.15" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
@ -29,6 +31,8 @@
|
|||
|
||||
<ItemGroup>
|
||||
<None Remove="Blazored.LocalStorage" />
|
||||
<None Remove="Refit" />
|
||||
<None Remove="Refit.HttpClientFactory" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Remove="wwwroot\css\Spectre\" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue