Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions POC.CodelessAPIClient.WebApp/AuthorizedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace POC.CodelessAPIClient.WebApp;

public class AuthorizedHandler : DelegatingHandler
{
private readonly string _apiBearerToken;

public AuthorizedHandler(string apiBearerToken)
{
//_apiBearerToken = "asdasdasd";
_apiBearerToken = apiBearerToken;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (!string.IsNullOrWhiteSpace(_apiBearerToken))
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _apiBearerToken);
}

return await base.SendAsync(request, cancellationToken);
}
}
10 changes: 9 additions & 1 deletion POC.CodelessAPIClient.WebApp/Components/Pages/Home.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components;
using POC.CodelessAPIClient.WebApp.HttpClients;
using Refit;

namespace POC.CodelessAPIClient.WebApp.Components.Pages;

Expand All @@ -20,6 +21,13 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

private async Task GetAuthorized()
{
_authString = await AuthClient.GetAuthorization();
try
{
_authString = await AuthClient.GetAuthorization();
}
catch (ApiException ex)
{
_authString = ex.ReasonPhrase;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private async Task DeleteItem(ShoppingListItem item)
{
_isLoading = true;
StateHasChanged();
await Client.RemoveItem(item);
await Client.RemoveItem(item.Id!.Value);

await RefreshList();

Expand Down
7 changes: 7 additions & 0 deletions POC.CodelessAPIClient.WebApp/Configs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace POC.CodelessAPIClient.WebApp;

public class Configs
{
public string ApiUrl { get; set; }
public string ApiBearerToken { get; set; }
}
6 changes: 0 additions & 6 deletions POC.CodelessAPIClient.WebApp/HttpClients/AuthFakeClient.cs

This file was deleted.

38 changes: 0 additions & 38 deletions POC.CodelessAPIClient.WebApp/HttpClients/FakeClient.cs

This file was deleted.

2 changes: 2 additions & 0 deletions POC.CodelessAPIClient.WebApp/HttpClients/IAuthorizedClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using POC.CodelessAPIClient.Models;
using Refit;

namespace POC.CodelessAPIClient.WebApp.HttpClients;

public interface IAuthorizedClient
{
[Get("/SecureStatus")]
Task<string> GetAuthorization();
}
10 changes: 9 additions & 1 deletion POC.CodelessAPIClient.WebApp/HttpClients/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using POC.CodelessAPIClient.Models;
using Refit;

namespace POC.CodelessAPIClient.WebApp.HttpClients;

public interface IClient
{
[Get("/ShoppingList")]
Task<ShoppingList> Get();

[Post("/ShoppingList")]
Task<ShoppingList> AddItem(ShoppingListItem item);

[Put("/ShoppingList")]
Task<ShoppingList> UpdateItem(ShoppingListItem item);
Task<ShoppingList> RemoveItem(ShoppingListItem item);

[Delete("/ShoppingList/{id}")]
Task<ShoppingList> RemoveItem([AliasAs("id")]Guid id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
<ProjectReference Include="..\POC.CodelessAPIClient.Models\POC.CodelessAPIClient.Models.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="8.0.0" />
<PackageReference Include="Refit.HttpClientFactory" Version="8.0.0" />
</ItemGroup>

</Project>
11 changes: 9 additions & 2 deletions POC.CodelessAPIClient.WebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using POC.CodelessAPIClient.WebApp.Components;
using POC.CodelessAPIClient.WebApp.HttpClients;
using Refit;

namespace POC.CodelessAPIClient.WebApp;

Expand All @@ -13,8 +14,14 @@ public static void Main(string[] args)
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddSingleton<IClient, FakeClient>();
builder.Services.AddSingleton<IAuthorizedClient, AuthFakeClient>();
var configs = builder.Configuration.GetSection("Configurations").Get<Configs>();

builder.Services.AddRefitClient<IClient>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri(configs!.ApiUrl));

builder.Services.AddRefitClient<IAuthorizedClient>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri(configs!.ApiUrl))
.AddHttpMessageHandler(() => new AuthorizedHandler(configs.ApiBearerToken));

var app = builder.Build();

Expand Down
6 changes: 5 additions & 1 deletion POC.CodelessAPIClient.WebApp/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Configurations": {
"ApiUrl": "http://localhost:5052",
"ApiBearerToken": "your-secret-token"
}
}