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
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; }
}
18 changes: 18 additions & 0 deletions POC.CodelessAPIClient.WebApp/HttpClients/AuthClassicClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace POC.CodelessAPIClient.WebApp.HttpClients;

public class AuthClassicClient : IAuthorizedClient
{
private readonly HttpClient _http;
public AuthClassicClient(HttpClient http)
{
_http = http;
}

public async Task<string> GetAuthorization()
{
var result = await _http.GetAsync("SecureStatus");
if (!result.IsSuccessStatusCode)
return "Not Authorized";
return await result.Content.ReadAsStringAsync();
}
}
48 changes: 48 additions & 0 deletions POC.CodelessAPIClient.WebApp/HttpClients/ClassicClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using POC.CodelessAPIClient.Models;

namespace POC.CodelessAPIClient.WebApp.HttpClients;

public class ClassicClient : IClient
{
private readonly HttpClient _http;

public ClassicClient(HttpClient http)
{
_http = http;
}

public async Task<ShoppingList> Get()
{
var result = await _http.GetFromJsonAsync<ShoppingList>("ShoppingList");
if (result is null) throw new InvalidOperationException("Empty response from GET /ShoppingList");
return result;
}

public async Task<ShoppingList> AddItem(ShoppingListItem item)
{
var resp = await _http.PostAsJsonAsync("ShoppingList", item);
resp.EnsureSuccessStatusCode();
var list = await resp.Content.ReadFromJsonAsync<ShoppingList>();
if (list is null) throw new InvalidOperationException("Empty response from POST /ShoppingList");
return list;
}

public async Task<ShoppingList> UpdateItem(ShoppingListItem item)
{
var resp = await _http.PutAsJsonAsync("ShoppingList", item);
resp.EnsureSuccessStatusCode();
var list = await resp.Content.ReadFromJsonAsync<ShoppingList>();
if (list is null) throw new InvalidOperationException("Empty response from PUT /ShoppingList");
return list;
}

public async Task<ShoppingList> RemoveItem(ShoppingListItem item)
{
if (item.Id is null) throw new ArgumentException("Item Id must be set for delete.", nameof(item));
var resp = await _http.DeleteAsync($"ShoppingList/{item.Id}");
resp.EnsureSuccessStatusCode();
var list = await resp.Content.ReadFromJsonAsync<ShoppingList>();
if (list is null) throw new InvalidOperationException("Empty response from DELETE /ShoppingList/{id}");
return list;
}
}
17 changes: 15 additions & 2 deletions POC.CodelessAPIClient.WebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@ 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>();

// Register typed HttpClient for ClassicClient using base URL from configs
builder.Services.AddHttpClient<IClient, ClassicClient>((sp, http) =>
{
http.BaseAddress = new Uri(configs!.ApiUrl.TrimEnd('/') + "/");
});

// Register typed HttpClient for AuthClassicClient using base URL from configs
builder.Services.AddHttpClient<IAuthorizedClient, AuthClassicClient>((sp, http) =>
{
http.BaseAddress = new Uri(configs!.ApiUrl.TrimEnd('/') + "/");
// Add Authorisation header
http.DefaultRequestHeaders.Add("Authorization", $"Bearer {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"
}
}