mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
|
|
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)!;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|