🚨 Stop using string literals for API URLS

pull/4/head
Alexis Drai 2 years ago
parent 7210d960b2
commit 3f1bd7f5b2

@ -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<int> Count()
{
return await _http.GetFromJsonAsync<int>("https://localhost:7234/api/Crafting/count");
return await _http.GetFromJsonAsync<int>($"{_apiBaseUrl}/count");
}
public async Task<List<Item>> All()
{
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/all");
return await _http.GetFromJsonAsync<List<Item>>($"{_apiBaseUrl}/all");
}
public async Task<List<Item>> List(int currentPage, int pageSize)
{
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
return await _http.GetFromJsonAsync<List<Item>>($"{_apiBaseUrl}/?currentPage={currentPage}&pageSize={pageSize}");
}
public async Task<Item> GetById(int id)
{
return await _http.GetFromJsonAsync<Item>($"https://localhost:7234/api/Crafting/{id}");
return await _http.GetFromJsonAsync<Item>($"{_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<List<CraftingRecipe>> GetRecipes()
{
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
return await _http.GetFromJsonAsync<List<CraftingRecipe>>($"{_apiBaseUrl}/recipe");
}
}
}

@ -7,7 +7,8 @@
},
"AllowedHosts": "*",
"CraftingApi": {
"BaseUrl": "https://localhost:7234"
}
"BaseUrl": "https://localhost:7234/api/Crafting"
},
"InventoryWidth": 6,
"InventoryHeight": 3,
}

Loading…
Cancel
Save