28 lines
997 B
C#
28 lines
997 B
C#
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace AnotherReplayReader.Utils
|
|
{
|
|
public static class Network
|
|
{
|
|
public static readonly JsonSerializerOptions CommonJsonOptions = new()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
};
|
|
|
|
public static string UrlEncode(string text) => HttpUtility.UrlEncode(text);
|
|
|
|
public static async Task<T?> HttpGetJson<T>(string url,
|
|
CancellationToken cancelToken = default)
|
|
{
|
|
using var client = new HttpClient();
|
|
using var response = await client.GetAsync(url, cancelToken).ConfigureAwait(false);
|
|
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
|
return await JsonSerializer.DeserializeAsync<T>(stream, CommonJsonOptions, cancelToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|