diff --git a/Core/Startup.cs b/Core/Startup.cs index ca3f127..c52877f 100644 --- a/Core/Startup.cs +++ b/Core/Startup.cs @@ -55,6 +55,7 @@ namespace Kruzya.TelegramBot.Core services.AddHostedService(); services.AddScoped(); + services.AddHttpClient(); // Trigger module handlers. _core.Modules.ConfigureServices(services); diff --git a/modules/CombotAntiSpam/Combot/CombotClient.cs b/modules/CombotAntiSpam/Combot/CombotClient.cs index 1ddd371..ddfe4c1 100644 --- a/modules/CombotAntiSpam/Combot/CombotClient.cs +++ b/modules/CombotAntiSpam/Combot/CombotClient.cs @@ -1,5 +1,4 @@ -using System; -using System.Net.Http; +using System.Net.Http; using System.Threading.Tasks; using Kruzya.TelegramBot.Core.Extensions; using Telegram.Bot.Types; @@ -8,12 +7,11 @@ namespace Kruzya.TelegramBot.CombotAntiSpam.Combot; public class CombotClient : ICombotClient { - public HttpClient _HttpClient; + private readonly HttpClient _httpClient; - public CombotClient() + public CombotClient(HttpClient httpClient) { - _HttpClient = new HttpClient(); - _HttpClient.BaseAddress = new Uri("https://api.cas.chat/check"); + _httpClient = httpClient; } public bool IsSpammer(User user) @@ -23,7 +21,7 @@ public class CombotClient : ICombotClient public async Task IsSpammerAsync(User user) { - var result = await _HttpClient.GetJsonAsync($"?user_id={user.Id}"); + var result = await _httpClient.GetJsonAsync($"?user_id={user.Id}"); return result.IsSpammer; } } \ No newline at end of file diff --git a/modules/CombotAntiSpam/CombotAntiSpam.cs b/modules/CombotAntiSpam/CombotAntiSpam.cs index 9bd9944..0abc135 100644 --- a/modules/CombotAntiSpam/CombotAntiSpam.cs +++ b/modules/CombotAntiSpam/CombotAntiSpam.cs @@ -1,6 +1,7 @@ using Kruzya.TelegramBot.CombotAntiSpam.Combot; using Kruzya.TelegramBot.Core; using Microsoft.Extensions.DependencyInjection; +using System; namespace Kruzya.TelegramBot.CombotAntiSpam; @@ -12,6 +13,9 @@ public class CombotAntiSpam : Module public override void ConfigureServices(IServiceCollection services) { - services.AddSingleton(); + services.AddHttpClient(c => + { + c.BaseAddress = new Uri("https://api.cas.chat/check"); + }); } } \ No newline at end of file diff --git a/modules/ContentStore/API/IKittiesApi.cs b/modules/ContentStore/API/IKittiesApi.cs new file mode 100644 index 0000000..ff1e351 --- /dev/null +++ b/modules/ContentStore/API/IKittiesApi.cs @@ -0,0 +1,8 @@ +using West.TelegramBot.ContentStore.Model; + +namespace West.TelegramBot.ContentStore.API; + +public interface IKittiesApi +{ + public Task GetKittiesImageAsync(string type); +} \ No newline at end of file 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/KittiesApi.cs b/modules/ContentStore/API/KittiesApi.cs new file mode 100644 index 0000000..f77e46b --- /dev/null +++ b/modules/ContentStore/API/KittiesApi.cs @@ -0,0 +1,28 @@ +using System.Text; +using West.TelegramBot.ContentStore.Model; + +namespace West.TelegramBot.ContentStore.API; + +public class KittiesApi : IKittiesApi +{ + private readonly HttpClient _client; + + public KittiesApi(HttpClient client) + { + _client = client; + } + + public async Task GetKittiesImageAsync(string type) + { + var httpResponse = await _client.GetAsync($"v1/{type}"); + var headers = httpResponse.Headers; + var titleBytes = + Convert.FromBase64String(headers.GetValues("X-Video-Title").First()); + + return new KittiesResponse( + await httpResponse.Content.ReadAsStreamAsync(), + headers.GetValues("X-Video-Source").First(), + Encoding.UTF8.GetString(titleBytes) + ); + } +} \ 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 62416b9..2a00e5c 100644 --- a/modules/ContentStore/ContentStore.cs +++ b/modules/ContentStore/ContentStore.cs @@ -1,7 +1,9 @@ using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Extensions; -using Kruzya.TelegramBot.Core.Options; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System.Net.Http.Headers; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Option; using West.TelegramBot.ContentStore.Service; @@ -27,6 +29,22 @@ public class ContentStore : Module { services.AddOption(); + var kittiesConfig = Configuration.GetSection("KittiesService"); + services.AddHttpClient(c => + { + c.BaseAddress = kittiesConfig.GetValue("Uri"); + c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( + "Bearer", + kittiesConfig.GetValue("token") + ); + }); + + services.AddHttpClient(c => + { + c.BaseAddress = new Uri("https://some-random-api.com"); + }); + + foreach (var serviceType in MediaServiceTypes) { services.AddScoped(typeof(IRandomMediaService), serviceType); diff --git a/modules/ContentStore/Model/KittiesResponse.cs b/modules/ContentStore/Model/KittiesResponse.cs new file mode 100644 index 0000000..d5fd0be --- /dev/null +++ b/modules/ContentStore/Model/KittiesResponse.cs @@ -0,0 +1,15 @@ +namespace West.TelegramBot.ContentStore.Model; + +public class KittiesResponse +{ + public Stream Content { get; set; } + public string Source { get; set; } + public string Title { get; set; } + + public KittiesResponse(Stream content, string source, string title) + { + Content = content; + Source = source; + Title = title; + } +} \ No newline at end of file diff --git a/modules/ContentStore/Service/AbstractKittiesService.cs b/modules/ContentStore/Service/AbstractKittiesService.cs deleted file mode 100644 index 133f252..0000000 --- a/modules/ContentStore/Service/AbstractKittiesService.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Net.Http.Headers; -using System.Text; -using BotFramework.Utils; -using Microsoft.Extensions.Configuration; -using Telegram.Bot.Types; -using West.TelegramBot.ContentStore.Model; -using West.TelegramBot.ContentStore.Option; - -namespace West.TelegramBot.ContentStore.Service; - -public abstract class AbstractKittiesService : IRandomMediaService -{ - private readonly AllowNsfw _allowNsfw; - protected abstract string VideoType { get; } - - public abstract StoreItem StoreItem { get; } - private readonly HttpClient _httpClient; - - protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) - { - var section = configuration.GetSection("KittiesService"); - - var uri = section.GetValue("Uri"); - var token = section.GetValue("Token"); - - _httpClient = new HttpClient - { - BaseAddress = uri, - DefaultRequestHeaders = - { - Authorization = new AuthenticationHeaderValue("Bearer", token) - } - }; - _allowNsfw = allowNsfw; - } - - public async Task GetRandomMediaAsync() - { - var response = await _httpClient.GetAsync($"v1/{VideoType}"); - var headers = response.Headers; - var videoSource = headers.GetValues("X-Video-Source").FirstOrDefault(string.Empty); - var videoTitleBase64 = headers.GetValues("X-Video-Title").FirstOrDefault(string.Empty); - var videoTitle = Encoding.UTF8.GetString(Convert.FromBase64String(videoTitleBase64)); - - return new RandomMedia( - new InputFile(await response.Content.ReadAsStreamAsync()), - new HtmlString().Url(videoSource, videoTitle).ToString(), - true - ); - } - - public async Task CanView(Chat chat, User user) - { - return await _allowNsfw.GetValueAsync(chat.Id); - } -} \ No newline at end of file diff --git a/modules/ContentStore/Service/AbstractNsfwService.cs b/modules/ContentStore/Service/AbstractNsfwService.cs index bb96667..8030edb 100644 --- a/modules/ContentStore/Service/AbstractNsfwService.cs +++ b/modules/ContentStore/Service/AbstractNsfwService.cs @@ -15,5 +15,6 @@ public abstract class AbstractNsfwService : IRandomMediaService _allowNsfw = allowNsfw; } - public async Task CanView(Chat chat, User user) => await _allowNsfw.GetValueAsync(chat.Id); + public async Task CanView(Chat chat, User user) + => await _allowNsfw.GetValueAsync(chat.Id); } \ No newline at end of file diff --git a/modules/ContentStore/Service/AnimBoobsService.cs b/modules/ContentStore/Service/AnimBoobsService.cs index b9c00ed..e755332 100644 --- a/modules/ContentStore/Service/AnimBoobsService.cs +++ b/modules/ContentStore/Service/AnimBoobsService.cs @@ -10,12 +10,10 @@ public class AnimBoobsService : AbstractNsfwService private readonly HttpClient _httpClient; public override StoreItem StoreItem { get; } = new("anim_boobs", "Сиськи.gif", 120, 40); - public AnimBoobsService(AllowNsfw allowNsfw) : base(allowNsfw) + public AnimBoobsService(AllowNsfw allowNsfw, IHttpClientFactory factory) : base(allowNsfw) { - _httpClient = new HttpClient - { - BaseAddress = new Uri("https://westdev.me/_boobs/") - }; + _httpClient = factory.CreateClient(); + _httpClient.BaseAddress = new Uri("https://westdev.me/_boobs/"); } public override async Task GetRandomMediaAsync() diff --git a/modules/ContentStore/Service/AnimalAsAService.cs b/modules/ContentStore/Service/AnimalAsAService.cs index 653bcb3..ef97ec1 100644 --- a/modules/ContentStore/Service/AnimalAsAService.cs +++ b/modules/ContentStore/Service/AnimalAsAService.cs @@ -9,15 +9,12 @@ public abstract class AnimalAsAService : IRandomMediaService private readonly HttpClient _httpClient; public abstract StoreItem StoreItem { get; } - protected AnimalAsAService(Uri baseAddress) + protected AnimalAsAService(IHttpClientFactory factory, Uri baseAddress) { - _httpClient = new HttpClient - { - BaseAddress = baseAddress - }; + _httpClient = factory.CreateClient(); + _httpClient.BaseAddress = baseAddress; } - - + public async Task GetRandomMediaAsync() { var resp = await _httpClient.GetAsync("images/search"); diff --git a/modules/ContentStore/Service/BirdService.cs b/modules/ContentStore/Service/BirdService.cs deleted file mode 100644 index 7b25a01..0000000 --- a/modules/ContentStore/Service/BirdService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class BirdService : SomeRandomApiAbstractService -{ - public BirdService(): base("bird", "Птица", 40, 165) - {} -} diff --git a/modules/ContentStore/Service/CatApiService.cs b/modules/ContentStore/Service/CatApiService.cs index b9757cd..51b6fd9 100644 --- a/modules/ContentStore/Service/CatApiService.cs +++ b/modules/ContentStore/Service/CatApiService.cs @@ -5,5 +5,6 @@ namespace West.TelegramBot.ContentStore.Service; public class CatApiService : AnimalAsAService { public override StoreItem StoreItem { get; } = new("cat", "Котэ", 40, 10); - public CatApiService() : base(new Uri("https://api.thecatapi.com/v1/")) {} + public CatApiService(IHttpClientFactory factory) : + base(factory, new Uri("https://api.thecatapi.com/v1/")) {} } \ No newline at end of file diff --git a/modules/ContentStore/Service/DogApiService.cs b/modules/ContentStore/Service/DogApiService.cs index 43bc3e7..1fefa21 100644 --- a/modules/ContentStore/Service/DogApiService.cs +++ b/modules/ContentStore/Service/DogApiService.cs @@ -5,5 +5,6 @@ namespace West.TelegramBot.ContentStore.Service; public class DogApiService : AnimalAsAService { public override StoreItem StoreItem { get; } = new("dog", "Пёсель", 40, 20); - public DogApiService() : base(new Uri("https://api.thedogapi.com/v1/")) {} + public DogApiService(IHttpClientFactory factory) : + base(factory, new Uri("https://api.thedogapi.com/v1/")) {} } \ No newline at end of file diff --git a/modules/ContentStore/Service/FoxService.cs b/modules/ContentStore/Service/FoxService.cs deleted file mode 100644 index 4ca1176..0000000 --- a/modules/ContentStore/Service/FoxService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class FoxService : SomeRandomApiAbstractService -{ - public FoxService(): base("fox", "Лисица", 40, 170) - {} -} diff --git a/modules/ContentStore/Service/KangarooService.cs b/modules/ContentStore/Service/KangarooService.cs deleted file mode 100644 index e4a1caa..0000000 --- a/modules/ContentStore/Service/KangarooService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class KangarooService : SomeRandomApiAbstractService -{ - public KangarooService(): base("kangaroo", "Кенгуру", 40, 175) - {} -} diff --git a/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs new file mode 100644 index 0000000..de63ca1 --- /dev/null +++ b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs @@ -0,0 +1,38 @@ +using BotFramework.Utils; +using Telegram.Bot.Types; +using West.TelegramBot.ContentStore.API; +using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Option; + +namespace West.TelegramBot.ContentStore.Service.Kitties; + +public abstract class AbstractKittiesService : IRandomMediaService +{ + private readonly IKittiesApi _api; + private readonly AllowNsfw _allowNsfw; + protected abstract string VideoType { get; } + + public abstract StoreItem StoreItem { get; } + + protected AbstractKittiesService(IKittiesApi api, AllowNsfw allowNsfw) + { + _api = api; + _allowNsfw = allowNsfw; + } + + public async Task GetRandomMediaAsync() + { + var response = await _api.GetKittiesImageAsync(VideoType); + + return new RandomMedia( + new InputFile(response.Content), + new HtmlString().Url(response.Source, response.Title).ToString(), + true + ); + } + + public async Task CanView(Chat chat, User user) + { + return await _allowNsfw.GetValueAsync(chat.Id); + } +} \ No newline at end of file diff --git a/modules/ContentStore/Service/GayKittiesService.cs b/modules/ContentStore/Service/Kitties/GayKittiesService.cs similarity index 60% rename from modules/ContentStore/Service/GayKittiesService.cs rename to modules/ContentStore/Service/Kitties/GayKittiesService.cs index e01a701..43f6591 100644 --- a/modules/ContentStore/Service/GayKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/GayKittiesService.cs @@ -1,15 +1,17 @@ using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Configuration; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; -namespace West.TelegramBot.ContentStore.Service; +namespace West.TelegramBot.ContentStore.Service.Kitties; public class GayKittiesService : AbstractKittiesService { - public GayKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw) + public GayKittiesService(IKittiesApi api, AllowNsfw allowNsfw) : base(api, allowNsfw) { } protected override string VideoType => "gay"; - public override StoreItem StoreItem { get; } = new("kitties_gay", "Прон (не геи)", 40, 110);} \ No newline at end of file + public override StoreItem StoreItem { get; } = new("kitties_gay", "Прон (не геи)", 40, 110); +} \ No newline at end of file diff --git a/modules/ContentStore/Service/StraightKittiesService.cs b/modules/ContentStore/Service/Kitties/StraightKittiesService.cs similarity index 56% rename from modules/ContentStore/Service/StraightKittiesService.cs rename to modules/ContentStore/Service/Kitties/StraightKittiesService.cs index e346906..3f68b2d 100644 --- a/modules/ContentStore/Service/StraightKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/StraightKittiesService.cs @@ -1,16 +1,16 @@ -using Kruzya.TelegramBot.Core.Data; -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; -namespace West.TelegramBot.ContentStore.Service; +namespace West.TelegramBot.ContentStore.Service.Kitties; public class StraightKittiesService : AbstractKittiesService { protected override string VideoType => "straight"; public override StoreItem StoreItem { get; } = new("kitties_straight", "Прон", 40, 100); - public StraightKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) : base(configuration, allowNsfw) + public StraightKittiesService(IKittiesApi api, AllowNsfw allowNsfw) : base(api, allowNsfw) { } } \ No newline at end of file diff --git a/modules/ContentStore/Service/KoalaService.cs b/modules/ContentStore/Service/KoalaService.cs deleted file mode 100644 index 3b5b49e..0000000 --- a/modules/ContentStore/Service/KoalaService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class KoalaService : SomeRandomApiAbstractService -{ - public KoalaService() : base("koala", "Коала", 40, 180) - {} -} diff --git a/modules/ContentStore/Service/OBoobsService.cs b/modules/ContentStore/Service/OBoobsService.cs index a60b896..0f1e461 100644 --- a/modules/ContentStore/Service/OBoobsService.cs +++ b/modules/ContentStore/Service/OBoobsService.cs @@ -12,12 +12,10 @@ public class OBoobsService : AbstractNsfwService private readonly HttpClient _httpClient; public override StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60, 30); - public OBoobsService(AllowNsfw allowNsfw) : base(allowNsfw) + public OBoobsService(AllowNsfw allowNsfw, IHttpClientFactory factory) : base(allowNsfw) { - _httpClient = new HttpClient - { - BaseAddress = new Uri("http://api.oboobs.ru") - }; + _httpClient = factory.CreateClient(); + _httpClient.BaseAddress = new Uri("http://api.oboobs.ru"); } public override async Task GetRandomMediaAsync() diff --git a/modules/ContentStore/Service/PandaService.cs b/modules/ContentStore/Service/PandaService.cs deleted file mode 100644 index 0198978..0000000 --- a/modules/ContentStore/Service/PandaService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class PandaService : SomeRandomApiAbstractService -{ - public PandaService(): base("panda", "Панда", 40, 160) - {} -} diff --git a/modules/ContentStore/Service/RaccoonService.cs b/modules/ContentStore/Service/RaccoonService.cs deleted file mode 100644 index c1c9280..0000000 --- a/modules/ContentStore/Service/RaccoonService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class RaccoonService : SomeRandomApiAbstractService -{ - public RaccoonService(): base("raccoon", "Енот", 40, 160) - {} -} diff --git a/modules/ContentStore/Service/RedPandaService.cs b/modules/ContentStore/Service/RedPandaService.cs deleted file mode 100644 index fdcbd66..0000000 --- a/modules/ContentStore/Service/RedPandaService.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace West.TelegramBot.ContentStore.Service; - -public class RedPandaService : SomeRandomApiAbstractService -{ - public RedPandaService(): base("red_panda", "Красная панда", 40, 150) - {} -} diff --git a/modules/ContentStore/Service/SomeRandomApi/BirdService.cs b/modules/ContentStore/Service/SomeRandomApi/BirdService.cs new file mode 100644 index 0000000..4309645 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/BirdService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class BirdService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..2269617 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/FoxService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class FoxService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..56bca70 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class KangarooService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..5cb6c08 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class KoalaService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..5dd2d44 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/PandaService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class PandaService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..749325f --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class RaccoonService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..886cd64 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs @@ -0,0 +1,9 @@ +using West.TelegramBot.ContentStore.API; + +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class RedPandaService : SomeRandomApiAbstractService +{ + 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 new file mode 100644 index 0000000..64291a2 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs @@ -0,0 +1,25 @@ +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; } + + protected SomeRandomApiAbstractService(ISomeRandomApi api, string id, string name, double price, int order) + { + StoreItem = new(id, name, price, order); + _api = api; + } + + public async Task GetRandomMediaAsync() + { + var image = await _api.GetAnimalAsync(StoreItem.Id); + + return new RandomMedia(new InputFileUrl(image.Image), image.Fact); + } +} \ No newline at end of file diff --git a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs b/modules/ContentStore/Service/SomeRandomApiAbstractService.cs deleted file mode 100644 index 9c82aa1..0000000 --- a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs +++ /dev/null @@ -1,29 +0,0 @@ -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 GetRandomMediaAsync() - { - var response = await _httpClient.GetStringAsync($"https://some-random-api.com/animal/{ApiId}"); - var image = JsonConvert.DeserializeObject(response)!; - - return new RandomMedia(new InputFileUrl(image.Image), image.Fact); - } -} \ No newline at end of file