Merge pull request #32 from Bubuni-Team/sgut_more_animals

Abstract for some-random-api.com, more animals
This commit is contained in:
2023-05-17 23:44:03 +03:00
committed by GitHub
9 changed files with 76 additions and 26 deletions
@@ -1,6 +1,6 @@
namespace West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Model;
public class RedPandaResponse public class SomeRandomApiAnimalResponse
{ {
public string Image { get; set; } = null!; public string Image { get; set; } = null!;
public string Fact { get; set; } = null!; public string Fact { get; set; } = null!;
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class BirdService : SomeRandomApiAbstractService
{
public BirdService(): base("bird", "Птица", 40, 165)
{}
}
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class FoxService : SomeRandomApiAbstractService
{
public FoxService(): base("fox", "Лисица", 40, 170)
{}
}
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class KangarooService : SomeRandomApiAbstractService
{
public KangarooService(): base("kangaroo", "Кенгуру", 40, 175)
{}
}
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class KoalaService : SomeRandomApiAbstractService
{
public KoalaService() : base("koala", "Коала", 40, 180)
{}
}
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class PandaService : SomeRandomApiAbstractService
{
public PandaService(): base("panda", "Панда", 40, 160)
{}
}
@@ -0,0 +1,7 @@
namespace West.TelegramBot.ContentStore.Service;
public class RacoonService : SomeRandomApiAbstractService
{
public RacoonService(): base("racoon", "Енот", 40, 160)
{}
}
@@ -1,28 +1,7 @@
using System.Net.Http.Json; namespace West.TelegramBot.ContentStore.Service;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service; public class RedPandaService : SomeRandomApiAbstractService
public class RedPandaService : IRandomMediaService
{ {
public StoreItem StoreItem { get; } = new("red_panda", "Красная панда", 40, 150); public RedPandaService(): base("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);
}
} }
@@ -0,0 +1,29 @@
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service;
public abstract class SomeRandomApiAbstractService : IRandomMediaService
{
public StoreItem StoreItem { get; }
public string ApiId => StoreItem.Id;
private readonly HttpClient _httpClient;
public SomeRandomApiAbstractService(string id, string name, double price, int order)
{
StoreItem = new(id, name, price, order);
// 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/{ApiId}");
var image = JsonConvert.DeserializeObject<SomeRandomApiAnimalResponse>(response);
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
}
}