diff --git a/modules/ContentStore/API/ISomeRandomApi.cs b/modules/ContentStore/API/ISomeRandomApi.cs new file mode 100644 index 0000000..c26f126 --- /dev/null +++ b/modules/ContentStore/API/ISomeRandomApi.cs @@ -0,0 +1,8 @@ +using West.TelegramBot.ContentStore.Model; + +namespace West.TelegramBot.ContentStore.API; + +public interface ISomeRandomApi +{ + public Task GetAnimalAsync(string animalId); +} \ No newline at end of file diff --git a/modules/ContentStore/API/SomeRandomApi.cs b/modules/ContentStore/API/SomeRandomApi.cs new file mode 100644 index 0000000..1c6f54b --- /dev/null +++ b/modules/ContentStore/API/SomeRandomApi.cs @@ -0,0 +1,21 @@ +using System.Net.Http.Json; +using West.TelegramBot.ContentStore.Model; + +namespace West.TelegramBot.ContentStore.API; + +public class SomeRandomApi : ISomeRandomApi +{ + private readonly HttpClient _client; + + public SomeRandomApi(HttpClient client) + { + _client = client; + } + + public async Task GetAnimalAsync(string animalId) + { + return (await _client.GetFromJsonAsync( + $"animal/{animalId}" + ))!; + } +} \ No newline at end of file diff --git a/modules/ContentStore/ContentStore.cs b/modules/ContentStore/ContentStore.cs index f1a5559..2a00e5c 100644 --- a/modules/ContentStore/ContentStore.cs +++ b/modules/ContentStore/ContentStore.cs @@ -39,6 +39,11 @@ public class ContentStore : Module ); }); + services.AddHttpClient(c => + { + c.BaseAddress = new Uri("https://some-random-api.com"); + }); + foreach (var serviceType in MediaServiceTypes) { diff --git a/modules/ContentStore/Service/SomeRandomApi/BirdService.cs b/modules/ContentStore/Service/SomeRandomApi/BirdService.cs index eeb7d89..4309645 100644 --- a/modules/ContentStore/Service/SomeRandomApi/BirdService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/BirdService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/FoxService.cs b/modules/ContentStore/Service/SomeRandomApi/FoxService.cs index 49b055e..2269617 100644 --- a/modules/ContentStore/Service/SomeRandomApi/FoxService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/FoxService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs b/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs index ccbf5c0..56bca70 100644 --- a/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs b/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs index c6c1908..5cb6c08 100644 --- a/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/PandaService.cs b/modules/ContentStore/Service/SomeRandomApi/PandaService.cs index 22d5db0..5dd2d44 100644 --- a/modules/ContentStore/Service/SomeRandomApi/PandaService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/PandaService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs b/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs index 41b5ab4..749325f 100644 --- a/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs b/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs index 539fb81..886cd64 100644 --- a/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs @@ -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) { } } diff --git a/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs index 419e179..64291a2 100644 --- a/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs @@ -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 GetRandomMediaAsync() { - var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}"); - var image = JsonConvert.DeserializeObject(response)!; + var image = await _api.GetAnimalAsync(StoreItem.Id); return new RandomMedia(new InputFileUrl(image.Image), image.Fact); }