Abstract for some-random-api.com, more animals

This commit is contained in:
2023-05-17 23:36:20 +03:00
parent d63113f58c
commit 9a554ee295
9 changed files with 76 additions and 26 deletions
@@ -1,28 +1,7 @@
using System.Net.Http.Json;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service;
namespace West.TelegramBot.ContentStore.Service;
public class RedPandaService : IRandomMediaService
public class RedPandaService : SomeRandomApiAbstractService
{
public StoreItem StoreItem { get; } = new("red_panda", "Красная панда", 40, 150);
private readonly HttpClient _httpClient;
public RedPandaService()
{
// TODO: use factory or something, cause we have a lot of http clients in use
_httpClient = new HttpClient();
}
public async Task<RandomMedia> GetRandomMediaAsync()
{
var response = await _httpClient.GetStringAsync("https://some-random-api.com/animal/red_panda");
var image = JsonConvert.DeserializeObject<RedPandaResponse>(response);
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
}
public RedPandaService(): base("red_panda", "Красная панда", 40, 150)
{}
}