diff --git a/POC.CodelessAPIClient.WebApp/Configs.cs b/POC.CodelessAPIClient.WebApp/Configs.cs new file mode 100644 index 0000000..ce4420d --- /dev/null +++ b/POC.CodelessAPIClient.WebApp/Configs.cs @@ -0,0 +1,7 @@ +namespace POC.CodelessAPIClient.WebApp; + +public class Configs +{ + public string ApiUrl { get; set; } + public string ApiBearerToken { get; set; } +} \ No newline at end of file diff --git a/POC.CodelessAPIClient.WebApp/HttpClients/AuthClassicClient.cs b/POC.CodelessAPIClient.WebApp/HttpClients/AuthClassicClient.cs new file mode 100644 index 0000000..b70da33 --- /dev/null +++ b/POC.CodelessAPIClient.WebApp/HttpClients/AuthClassicClient.cs @@ -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 GetAuthorization() + { + var result = await _http.GetAsync("SecureStatus"); + if (!result.IsSuccessStatusCode) + return "Not Authorized"; + return await result.Content.ReadAsStringAsync(); + } +} \ No newline at end of file diff --git a/POC.CodelessAPIClient.WebApp/HttpClients/ClassicClient.cs b/POC.CodelessAPIClient.WebApp/HttpClients/ClassicClient.cs new file mode 100644 index 0000000..2aa4e3d --- /dev/null +++ b/POC.CodelessAPIClient.WebApp/HttpClients/ClassicClient.cs @@ -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 Get() + { + var result = await _http.GetFromJsonAsync("ShoppingList"); + if (result is null) throw new InvalidOperationException("Empty response from GET /ShoppingList"); + return result; + } + + public async Task AddItem(ShoppingListItem item) + { + var resp = await _http.PostAsJsonAsync("ShoppingList", item); + resp.EnsureSuccessStatusCode(); + var list = await resp.Content.ReadFromJsonAsync(); + if (list is null) throw new InvalidOperationException("Empty response from POST /ShoppingList"); + return list; + } + + public async Task UpdateItem(ShoppingListItem item) + { + var resp = await _http.PutAsJsonAsync("ShoppingList", item); + resp.EnsureSuccessStatusCode(); + var list = await resp.Content.ReadFromJsonAsync(); + if (list is null) throw new InvalidOperationException("Empty response from PUT /ShoppingList"); + return list; + } + + public async Task 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(); + if (list is null) throw new InvalidOperationException("Empty response from DELETE /ShoppingList/{id}"); + return list; + } +} \ No newline at end of file diff --git a/POC.CodelessAPIClient.WebApp/Program.cs b/POC.CodelessAPIClient.WebApp/Program.cs index dd2bd98..c6099ff 100644 --- a/POC.CodelessAPIClient.WebApp/Program.cs +++ b/POC.CodelessAPIClient.WebApp/Program.cs @@ -13,8 +13,21 @@ public static void Main(string[] args) builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); - builder.Services.AddSingleton(); - builder.Services.AddSingleton(); + var configs = builder.Configuration.GetSection("Configurations").Get(); + + // Register typed HttpClient for ClassicClient using base URL from configs + builder.Services.AddHttpClient((sp, http) => + { + http.BaseAddress = new Uri(configs!.ApiUrl.TrimEnd('/') + "/"); + }); + + // Register typed HttpClient for AuthClassicClient using base URL from configs + builder.Services.AddHttpClient((sp, http) => + { + http.BaseAddress = new Uri(configs!.ApiUrl.TrimEnd('/') + "/"); + // Add Authorisation header + http.DefaultRequestHeaders.Add("Authorization", $"Bearer {configs.ApiBearerToken}"); + }); var app = builder.Build(); diff --git a/POC.CodelessAPIClient.WebApp/appsettings.json b/POC.CodelessAPIClient.WebApp/appsettings.json index 10f68b8..dba5697 100644 --- a/POC.CodelessAPIClient.WebApp/appsettings.json +++ b/POC.CodelessAPIClient.WebApp/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Configurations": { + "ApiUrl": "http://localhost:5052", + "ApiBearerToken": "your-secret-token" + } }