From 3f1bd7f5b22f4f4db3d4ce5e8625be61a4ca6a34 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sun, 26 Feb 2023 10:50:19 +0100 Subject: [PATCH] :rotating_light: Stop using string literals for API URLS --- blazor_lab/Services/DataApiService.cs | 21 ++++++++++++--------- blazor_lab/appsettings.json | 7 ++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/blazor_lab/Services/DataApiService.cs b/blazor_lab/Services/DataApiService.cs index 4b90f17..2e5c4ab 100644 --- a/blazor_lab/Services/DataApiService.cs +++ b/blazor_lab/Services/DataApiService.cs @@ -7,11 +7,14 @@ namespace blazor_lab.Services public class DataApiService : IDataService { private readonly HttpClient _http; + private readonly string _apiBaseUrl; public DataApiService( - HttpClient http) + HttpClient http, + IConfiguration config) { _http = http; + _apiBaseUrl = config.GetSection("CraftingApi")["BaseUrl"]; } public async Task Add(ItemModel model) @@ -20,27 +23,27 @@ namespace blazor_lab.Services var item = ItemFactory.Create(model); // Save the data - await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item); + await _http.PostAsJsonAsync($"{_apiBaseUrl}/", item); } public async Task Count() { - return await _http.GetFromJsonAsync("https://localhost:7234/api/Crafting/count"); + return await _http.GetFromJsonAsync($"{_apiBaseUrl}/count"); } public async Task> All() { - return await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/all"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/all"); } public async Task> List(int currentPage, int pageSize) { - return await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/?currentPage={currentPage}&pageSize={pageSize}"); } public async Task GetById(int id) { - return await _http.GetFromJsonAsync($"https://localhost:7234/api/Crafting/{id}"); + return await _http.GetFromJsonAsync($"{_apiBaseUrl}/{id}"); } public async Task Update(int id, ItemModel model) @@ -48,17 +51,17 @@ namespace blazor_lab.Services // Get the item var item = ItemFactory.Create(model); - await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item); + await _http.PutAsJsonAsync($"{_apiBaseUrl}/{id}", item); } public async Task Delete(int id) { - await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}"); + await _http.DeleteAsync($"{_apiBaseUrl}/{id}"); } public async Task> GetRecipes() { - return await _http.GetFromJsonAsync>("https://localhost:7234/api/Crafting/recipe"); + return await _http.GetFromJsonAsync>($"{_apiBaseUrl}/recipe"); } } } diff --git a/blazor_lab/appsettings.json b/blazor_lab/appsettings.json index 2334eaa..b1cc657 100644 --- a/blazor_lab/appsettings.json +++ b/blazor_lab/appsettings.json @@ -7,7 +7,8 @@ }, "AllowedHosts": "*", "CraftingApi": { - "BaseUrl": "https://localhost:7234" - } - + "BaseUrl": "https://localhost:7234/api/Crafting" + }, + "InventoryWidth": 6, + "InventoryHeight": 3, }