Files

24 lines
823 B
C#
Raw Permalink Normal View History

2025-02-09 17:40:03 +02:00
using System.Text.Json;
2023-01-02 19:01:03 +02:00
using Telegram.Bot.Types;
2022-02-16 17:07:30 +02:00
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service;
public abstract class AnimalAsAService : IRandomMediaService
{
private readonly HttpClient _httpClient;
2022-02-16 18:08:02 +02:00
public abstract StoreItem StoreItem { get; }
2022-02-16 17:07:30 +02:00
2023-07-28 23:14:36 +03:00
protected AnimalAsAService(IHttpClientFactory factory, Uri baseAddress)
2022-02-16 17:07:30 +02:00
{
2023-07-28 23:14:36 +03:00
_httpClient = factory.CreateClient();
_httpClient.BaseAddress = baseAddress;
2022-02-16 17:07:30 +02:00
}
2023-07-28 23:14:36 +03:00
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
2022-02-16 17:07:30 +02:00
{
var resp = await _httpClient.GetAsync("images/search");
2025-02-09 17:40:03 +02:00
var catImage = JsonSerializer.Deserialize<List<AnimalImage>>(await resp.Content.ReadAsStringAsync())![0];
2023-01-02 19:01:03 +02:00
return new RandomMedia(new InputFileUrl(catImage.Url));
2022-02-16 17:07:30 +02:00
}
}