2020-08-14 07:13:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
2020-08-31 15:59:47 +08:00
|
|
|
|
using System.Text.Json;
|
2020-08-14 07:13:28 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign.core.Extensions
|
|
|
|
|
{
|
2020-08-31 15:59:47 +08:00
|
|
|
|
internal static class HttpClientExtensions
|
2020-08-14 07:13:28 +08:00
|
|
|
|
{
|
|
|
|
|
public static async Task<TValue> GetFromJsonAsync<TValue>(this HttpClient client, string requestUri, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(client));
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 15:59:47 +08:00
|
|
|
|
var res = await client.GetAsync(requestUri, cancellationToken);
|
2020-08-14 07:13:28 +08:00
|
|
|
|
if (res.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
var json = await res.Content.ReadAsStreamAsync();
|
2020-08-31 15:59:47 +08:00
|
|
|
|
return await JsonSerializer.DeserializeAsync<TValue>(json, new JsonSerializerOptions()
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
}, cancellationToken);
|
2020-08-14 07:13:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|