mirror of
https://github.com/valentincanonical/eShopOnWeb-chisel-demo.git
synced 2026-02-14 09:39:53 +00:00
* udated to .net6 * used the .net6 version RC2 * added editconfig. * App core new Scoped Namespaces style. * BlazorAdmin new Scoped Namespaces style. * Blazor Shared new Scoped Namespaces style. * Infra new Scoped Namespaces style. * public api new Scoped Namespaces style. * web new Scoped Namespaces style. * FunctionalTests new Scoped Namespaces style. * Integrational tests new Scoped Namespaces style. * unit tests new Scoped Namespaces style. * update github action. * update github action. * change the global.
96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using BlazorShared;
|
|
using BlazorShared.Models;
|
|
|
|
namespace BlazorAdmin.Services;
|
|
|
|
public class HttpService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly ToastService _toastService;
|
|
private readonly string _apiUrl;
|
|
|
|
|
|
public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration, ToastService toastService)
|
|
{
|
|
_httpClient = httpClient;
|
|
_toastService = toastService;
|
|
_apiUrl = baseUrlConfiguration.ApiBase;
|
|
}
|
|
|
|
public async Task<T> HttpGet<T>(string uri)
|
|
where T : class
|
|
{
|
|
var result = await _httpClient.GetAsync($"{_apiUrl}{uri}");
|
|
if (!result.IsSuccessStatusCode)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await FromHttpResponseMessage<T>(result);
|
|
}
|
|
|
|
public async Task<T> HttpDelete<T>(string uri, int id)
|
|
where T : class
|
|
{
|
|
var result = await _httpClient.DeleteAsync($"{_apiUrl}{uri}/{id}");
|
|
if (!result.IsSuccessStatusCode)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return await FromHttpResponseMessage<T>(result);
|
|
}
|
|
|
|
public async Task<T> HttpPost<T>(string uri, object dataToSend)
|
|
where T : class
|
|
{
|
|
var content = ToJson(dataToSend);
|
|
|
|
var result = await _httpClient.PostAsync($"{_apiUrl}{uri}", content);
|
|
if (!result.IsSuccessStatusCode)
|
|
{
|
|
var exception = JsonSerializer.Deserialize<ErrorDetails>(await result.Content.ReadAsStringAsync(), new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
});
|
|
_toastService.ShowToast($"Error : {exception.Message}", ToastLevel.Error);
|
|
|
|
return null;
|
|
}
|
|
|
|
return await FromHttpResponseMessage<T>(result);
|
|
}
|
|
|
|
public async Task<T> HttpPut<T>(string uri, object dataToSend)
|
|
where T : class
|
|
{
|
|
var content = ToJson(dataToSend);
|
|
|
|
var result = await _httpClient.PutAsync($"{_apiUrl}{uri}", content);
|
|
if (!result.IsSuccessStatusCode)
|
|
{
|
|
_toastService.ShowToast("Error", ToastLevel.Error);
|
|
return null;
|
|
}
|
|
|
|
return await FromHttpResponseMessage<T>(result);
|
|
}
|
|
|
|
private StringContent ToJson(object obj)
|
|
{
|
|
return new StringContent(JsonSerializer.Serialize(obj), Encoding.UTF8, "application/json");
|
|
}
|
|
|
|
private async Task<T> FromHttpResponseMessage<T>(HttpResponseMessage result)
|
|
{
|
|
return JsonSerializer.Deserialize<T>(await result.Content.ReadAsStringAsync(), new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
});
|
|
}
|
|
}
|