2023-05-07 20:32:18 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2021-12-26 22:32:36 +00:00
|
|
|
using System.Net.Http;
|
2023-05-07 20:32:18 +00:00
|
|
|
using System.Net.Http.Headers;
|
2021-12-26 22:32:36 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Wabbajack.Networking.Http;
|
|
|
|
|
|
|
|
public static class Extensions
|
|
|
|
{
|
|
|
|
public static async Task<T> GetJsonFromSendAsync<T>(this HttpClient client, HttpRequestMessage msg,
|
|
|
|
JsonSerializerOptions opts, CancellationToken? token = null)
|
|
|
|
{
|
|
|
|
token ??= CancellationToken.None;
|
|
|
|
using var result = await client.SendAsync(msg, token.Value);
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
throw new HttpException(result);
|
|
|
|
}
|
|
|
|
return (await JsonSerializer.DeserializeAsync<T>(await result.Content.ReadAsStreamAsync(token.Value)))!;
|
|
|
|
}
|
2023-05-07 20:32:18 +00:00
|
|
|
|
|
|
|
public static WebHeaderCollection ToWebHeaderCollection(this HttpRequestHeaders headers)
|
|
|
|
{
|
|
|
|
var headerCollection = new WebHeaderCollection();
|
|
|
|
|
|
|
|
foreach (var header in headers.Where(header => !WebHeaderCollection.IsRestricted(header.Key)))
|
|
|
|
{
|
|
|
|
header.Value.ToList().ForEach(value => headerCollection.Add(header.Key, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return headerCollection;
|
|
|
|
}
|
2021-12-26 22:32:36 +00:00
|
|
|
}
|