[core] client factory for SomeRandomApi

This commit is contained in:
Andriy
2023-07-28 23:13:56 +03:00
parent c19696b360
commit 42509dcacd
11 changed files with 62 additions and 18 deletions
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class BirdService : SomeRandomApiAbstractService
{
public BirdService() : base("bird", "Птица", 40, 165)
public BirdService(ISomeRandomApi api) : base(api, "bird", "Птица", 40, 165)
{ }
}
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class FoxService : SomeRandomApiAbstractService
{
public FoxService() : base("fox", "Лисица", 40, 170)
public FoxService(ISomeRandomApi api) : base(api, "fox", "Лисица", 40, 170)
{ }
}
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class KangarooService : SomeRandomApiAbstractService
{
public KangarooService() : base("kangaroo", "Кенгуру", 40, 175)
public KangarooService(ISomeRandomApi api) : base(api, "kangaroo", "Кенгуру", 40, 175)
{ }
}
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class KoalaService : SomeRandomApiAbstractService
{
public KoalaService() : base("koala", "Коала", 40, 180)
public KoalaService(ISomeRandomApi api) : base(api, "koala", "Коала", 40, 180)
{ }
}
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class PandaService : SomeRandomApiAbstractService
{
public PandaService() : base("panda", "Панда", 40, 160)
public PandaService(ISomeRandomApi api) : base(api, "panda", "Панда", 40, 160)
{ }
}
@@ -1,7 +1,9 @@
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class RaccoonService : SomeRandomApiAbstractService
{
public RaccoonService() : base("raccoon", "Енот", 40, 160)
public RaccoonService(ISomeRandomApi api) : base(api, "raccoon", "Енот", 40, 160)
{ }
}
@@ -1,7 +1,9 @@
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
using West.TelegramBot.ContentStore.API;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public class RedPandaService : SomeRandomApiAbstractService
{
public RedPandaService() : base("red_panda", "Красная панда", 40, 150)
public RedPandaService(ISomeRandomApi api) : base(api, "red_panda", "Красная панда", 40, 150)
{ }
}
@@ -1,28 +1,24 @@
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service.SomeRandomApi;
public abstract class SomeRandomApiAbstractService : IRandomMediaService
{
private readonly ISomeRandomApi _api;
public StoreItem StoreItem { get; }
public string ApiId => StoreItem.Id;
private readonly HttpClient _httpClient;
public SomeRandomApiAbstractService(string id, string name, double price, int order)
protected SomeRandomApiAbstractService(ISomeRandomApi api, 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();
_api = api;
}
public async Task<RandomMedia> GetRandomMediaAsync()
{
var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}");
var image = JsonConvert.DeserializeObject<SomeRandomApiAnimalResponse>(response)!;
var image = await _api.GetAnimalAsync(StoreItem.Id);
return new RandomMedia(new InputFileUrl(image.Image), image.Fact);
}