2024-03-12 14:05:27 -04:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
using System.Text.Json;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
namespace TwitchChatTTS.Helpers
|
|
|
|
{
|
|
|
|
public class WebClientWrap
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly HttpClient _client;
|
|
|
|
private readonly JsonSerializerOptions _options;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public WebClientWrap(JsonSerializerOptions options)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_client = new HttpClient();
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void AddHeader(string key, string? value)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
if (_client.DefaultRequestHeaders.Contains(key))
|
|
|
|
_client.DefaultRequestHeaders.Remove(key);
|
|
|
|
_client.DefaultRequestHeaders.Add(key, value);
|
|
|
|
}
|
|
|
|
|
2024-07-19 12:56:41 -04:00
|
|
|
public async Task<T?> GetJson<T>(string uri, JsonSerializerOptions? options = null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
var response = await _client.GetAsync(uri);
|
2024-06-24 18:11:36 -04:00
|
|
|
return JsonSerializer.Deserialize<T>(await response.Content.ReadAsStreamAsync(), options ?? _options);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public async Task<HttpResponseMessage> Get(string uri)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
return await _client.GetAsync(uri);
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public async Task<HttpResponseMessage> Post<T>(string uri, T data)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
return await _client.PostAsJsonAsync(uri, data, _options);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public async Task<HttpResponseMessage> Post(string uri)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
return await _client.PostAsJsonAsync(uri, new object(), _options);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
2024-08-06 15:29:29 -04:00
|
|
|
|
|
|
|
public async Task<T?> Delete<T>(string uri)
|
|
|
|
{
|
|
|
|
return await _client.DeleteFromJsonAsync<T>(uri, _options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<HttpResponseMessage> Delete(string uri)
|
|
|
|
{
|
|
|
|
return await _client.DeleteAsync(uri);
|
|
|
|
}
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|