Files
telegram-bot/modules/CustomChatAddOns/Service/InteropService.cs
T

42 lines
1.3 KiB
C#
Raw Normal View History

2024-05-29 00:59:55 +03:00
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.CustomChatAddOns.Service
{
public class InteropService
{
private readonly JsonSerializerSettings _serializerSettings;
private readonly HttpClient _client;
public InteropService(ISerializationBinder serializationBinder, HttpClient client)
{
_serializerSettings = new()
{
TypeNameHandling = TypeNameHandling.Objects,
SerializationBinder = serializationBinder
};
_client = client;
}
public async Task<TOut> PostMessage<TIn, TOut>(string endpoint, TIn message)
{
var serializedMessage = JsonConvert.SerializeObject(message, _serializerSettings);
var httpContent = new StringContent(serializedMessage, Encoding.UTF8, "application/json");
var response = await (await _client.PostAsync(endpoint, httpContent))
.EnsureSuccessStatusCode()
.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<TOut>(response, _serializerSettings)!;
}
}
}