ant-design-blazor/components/core/Extensions/HttpClientExtensions.cs
James Yeung 47ad157c8b refactor: make the extensions class for HttpClient internal (#549)
* refactor: make httpclient extensions internal

* fix: set property name case-Insensitive
2020-08-31 15:59:47 +08:00

32 lines
976 B
C#

using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace AntDesign.core.Extensions
{
internal static class HttpClientExtensions
{
public static async Task<TValue> GetFromJsonAsync<TValue>(this HttpClient client, string requestUri, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
var res = await client.GetAsync(requestUri, cancellationToken);
if (res.IsSuccessStatusCode)
{
var json = await res.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<TValue>(json, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
}, cancellationToken);
}
return default;
}
}
}