From d2e564cec6f4c1c119aecef1263172b8d402494e Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 28 Jul 2023 19:41:21 +0300 Subject: [PATCH 1/5] [cas] use http client factory --- modules/CombotAntiSpam/Combot/CombotClient.cs | 12 +++++------- modules/CombotAntiSpam/CombotAntiSpam.cs | 6 +++++- 2 files changed, 10 insertions(+), 8 deletions(-) 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 From 462f3c983635622a0e12e302242d0a53ab9f938b Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 28 Jul 2023 20:04:05 +0300 Subject: [PATCH 2/5] [store] categorize store services --- modules/ContentStore/Service/BirdService.cs | 7 ------- modules/ContentStore/Service/FoxService.cs | 7 ------- modules/ContentStore/Service/KangarooService.cs | 7 ------- .../Service/{ => Kitties}/AbstractKittiesService.cs | 4 ++-- .../Service/{ => Kitties}/GayKittiesService.cs | 5 +++-- .../Service/{ => Kitties}/StraightKittiesService.cs | 5 ++--- modules/ContentStore/Service/PandaService.cs | 7 ------- modules/ContentStore/Service/RaccoonService.cs | 7 ------- modules/ContentStore/Service/RedPandaService.cs | 7 ------- modules/ContentStore/Service/SomeRandomApi/BirdService.cs | 7 +++++++ modules/ContentStore/Service/SomeRandomApi/FoxService.cs | 7 +++++++ .../ContentStore/Service/SomeRandomApi/KangarooService.cs | 7 +++++++ .../Service/{ => SomeRandomApi}/KoalaService.cs | 4 ++-- modules/ContentStore/Service/SomeRandomApi/PandaService.cs | 7 +++++++ .../ContentStore/Service/SomeRandomApi/RaccoonService.cs | 7 +++++++ .../ContentStore/Service/SomeRandomApi/RedPandaService.cs | 7 +++++++ .../{ => SomeRandomApi}/SomeRandomApiAbstractService.cs | 2 +- 17 files changed, 52 insertions(+), 52 deletions(-) delete mode 100644 modules/ContentStore/Service/BirdService.cs delete mode 100644 modules/ContentStore/Service/FoxService.cs delete mode 100644 modules/ContentStore/Service/KangarooService.cs rename modules/ContentStore/Service/{ => Kitties}/AbstractKittiesService.cs (96%) rename modules/ContentStore/Service/{ => Kitties}/GayKittiesService.cs (82%) rename modules/ContentStore/Service/{ => Kitties}/StraightKittiesService.cs (77%) delete mode 100644 modules/ContentStore/Service/PandaService.cs delete mode 100644 modules/ContentStore/Service/RaccoonService.cs delete mode 100644 modules/ContentStore/Service/RedPandaService.cs create mode 100644 modules/ContentStore/Service/SomeRandomApi/BirdService.cs create mode 100644 modules/ContentStore/Service/SomeRandomApi/FoxService.cs create mode 100644 modules/ContentStore/Service/SomeRandomApi/KangarooService.cs rename modules/ContentStore/Service/{ => SomeRandomApi}/KoalaService.cs (64%) create mode 100644 modules/ContentStore/Service/SomeRandomApi/PandaService.cs create mode 100644 modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs create mode 100644 modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs rename modules/ContentStore/Service/{ => SomeRandomApi}/SomeRandomApiAbstractService.cs (93%) 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/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/AbstractKittiesService.cs b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs similarity index 96% rename from modules/ContentStore/Service/AbstractKittiesService.cs rename to modules/ContentStore/Service/Kitties/AbstractKittiesService.cs index 133f252..dfbb454 100644 --- a/modules/ContentStore/Service/AbstractKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs @@ -6,7 +6,7 @@ using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; -namespace West.TelegramBot.ContentStore.Service; +namespace West.TelegramBot.ContentStore.Service.Kitties; public abstract class AbstractKittiesService : IRandomMediaService { @@ -15,7 +15,7 @@ public abstract class AbstractKittiesService : IRandomMediaService public abstract StoreItem StoreItem { get; } private readonly HttpClient _httpClient; - + protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) { var section = configuration.GetSection("KittiesService"); diff --git a/modules/ContentStore/Service/GayKittiesService.cs b/modules/ContentStore/Service/Kitties/GayKittiesService.cs similarity index 82% rename from modules/ContentStore/Service/GayKittiesService.cs rename to modules/ContentStore/Service/Kitties/GayKittiesService.cs index e01a701..460f5b2 100644 --- a/modules/ContentStore/Service/GayKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/GayKittiesService.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Configuration; 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 { @@ -12,4 +12,5 @@ public class GayKittiesService : AbstractKittiesService } 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 77% rename from modules/ContentStore/Service/StraightKittiesService.cs rename to modules/ContentStore/Service/Kitties/StraightKittiesService.cs index e346906..df88eff 100644 --- a/modules/ContentStore/Service/StraightKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/StraightKittiesService.cs @@ -1,9 +1,8 @@ -using Kruzya.TelegramBot.Core.Data; -using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration; 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 { 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..eeb7d89 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/BirdService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class BirdService : SomeRandomApiAbstractService +{ + public BirdService() : base("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..49b055e --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/FoxService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class FoxService : SomeRandomApiAbstractService +{ + public FoxService() : base("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..ccbf5c0 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/KangarooService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class KangarooService : SomeRandomApiAbstractService +{ + public KangarooService() : base("kangaroo", "Кенгуру", 40, 175) + { } +} diff --git a/modules/ContentStore/Service/KoalaService.cs b/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs similarity index 64% rename from modules/ContentStore/Service/KoalaService.cs rename to modules/ContentStore/Service/SomeRandomApi/KoalaService.cs index 3b5b49e..c6c1908 100644 --- a/modules/ContentStore/Service/KoalaService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/KoalaService.cs @@ -1,7 +1,7 @@ -namespace West.TelegramBot.ContentStore.Service; +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; public class KoalaService : SomeRandomApiAbstractService { public KoalaService() : base("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..22d5db0 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/PandaService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class PandaService : SomeRandomApiAbstractService +{ + public PandaService() : base("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..41b5ab4 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/RaccoonService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class RaccoonService : SomeRandomApiAbstractService +{ + public RaccoonService() : base("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..539fb81 --- /dev/null +++ b/modules/ContentStore/Service/SomeRandomApi/RedPandaService.cs @@ -0,0 +1,7 @@ +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; + +public class RedPandaService : SomeRandomApiAbstractService +{ + public RedPandaService() : base("red_panda", "Красная панда", 40, 150) + { } +} diff --git a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs similarity index 93% rename from modules/ContentStore/Service/SomeRandomApiAbstractService.cs rename to modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs index 9c82aa1..419e179 100644 --- a/modules/ContentStore/Service/SomeRandomApiAbstractService.cs +++ b/modules/ContentStore/Service/SomeRandomApi/SomeRandomApiAbstractService.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; -namespace West.TelegramBot.ContentStore.Service; +namespace West.TelegramBot.ContentStore.Service.SomeRandomApi; public abstract class SomeRandomApiAbstractService : IRandomMediaService { From c19696b360740180f03ed2fdbc4ddd56f59ea6d4 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 28 Jul 2023 22:44:51 +0300 Subject: [PATCH 3/5] [store] kitties now have own http client factory --- modules/ContentStore/API/IKittiesApi.cs | 8 +++++ modules/ContentStore/API/KittiesApi.cs | 28 +++++++++++++++ modules/ContentStore/ContentStore.cs | 15 +++++++- modules/ContentStore/Model/KittiesResponse.cs | 15 ++++++++ .../Service/AbstractNsfwService.cs | 3 +- .../Service/Kitties/AbstractKittiesService.cs | 34 +++++-------------- .../Service/Kitties/GayKittiesService.cs | 3 +- .../Service/Kitties/StraightKittiesService.cs | 3 +- 8 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 modules/ContentStore/API/IKittiesApi.cs create mode 100644 modules/ContentStore/API/KittiesApi.cs create mode 100644 modules/ContentStore/Model/KittiesResponse.cs 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/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/ContentStore.cs b/modules/ContentStore/ContentStore.cs index 62416b9..f1a5559 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,17 @@ 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") + ); + }); + + 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/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/Kitties/AbstractKittiesService.cs b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs index dfbb454..de63ca1 100644 --- a/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/AbstractKittiesService.cs @@ -1,8 +1,6 @@ -using System.Net.Http.Headers; -using System.Text; -using BotFramework.Utils; -using Microsoft.Extensions.Configuration; +using BotFramework.Utils; using Telegram.Bot.Types; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; @@ -10,41 +8,25 @@ 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; } - private readonly HttpClient _httpClient; - protected AbstractKittiesService(IConfiguration configuration, AllowNsfw allowNsfw) + protected AbstractKittiesService(IKittiesApi api, 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) - } - }; + _api = api; _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)); + var response = await _api.GetKittiesImageAsync(VideoType); return new RandomMedia( - new InputFile(await response.Content.ReadAsStreamAsync()), - new HtmlString().Url(videoSource, videoTitle).ToString(), + new InputFile(response.Content), + new HtmlString().Url(response.Source, response.Title).ToString(), true ); } diff --git a/modules/ContentStore/Service/Kitties/GayKittiesService.cs b/modules/ContentStore/Service/Kitties/GayKittiesService.cs index 460f5b2..43f6591 100644 --- a/modules/ContentStore/Service/Kitties/GayKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/GayKittiesService.cs @@ -1,5 +1,6 @@ using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Configuration; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; @@ -7,7 +8,7 @@ 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) { } diff --git a/modules/ContentStore/Service/Kitties/StraightKittiesService.cs b/modules/ContentStore/Service/Kitties/StraightKittiesService.cs index df88eff..3f68b2d 100644 --- a/modules/ContentStore/Service/Kitties/StraightKittiesService.cs +++ b/modules/ContentStore/Service/Kitties/StraightKittiesService.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Configuration; +using West.TelegramBot.ContentStore.API; using West.TelegramBot.ContentStore.Model; using West.TelegramBot.ContentStore.Option; @@ -9,7 +10,7 @@ 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 From 42509dcacd4f08f326660ae7580a8a003d89e0db Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 28 Jul 2023 23:13:56 +0300 Subject: [PATCH 4/5] [core] client factory for SomeRandomApi --- modules/ContentStore/API/ISomeRandomApi.cs | 8 +++++++ modules/ContentStore/API/SomeRandomApi.cs | 21 +++++++++++++++++++ modules/ContentStore/ContentStore.cs | 5 +++++ .../Service/SomeRandomApi/BirdService.cs | 4 +++- .../Service/SomeRandomApi/FoxService.cs | 4 +++- .../Service/SomeRandomApi/KangarooService.cs | 4 +++- .../Service/SomeRandomApi/KoalaService.cs | 4 +++- .../Service/SomeRandomApi/PandaService.cs | 4 +++- .../Service/SomeRandomApi/RaccoonService.cs | 4 +++- .../Service/SomeRandomApi/RedPandaService.cs | 6 ++++-- .../SomeRandomApiAbstractService.cs | 16 ++++++-------- 11 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 modules/ContentStore/API/ISomeRandomApi.cs create mode 100644 modules/ContentStore/API/SomeRandomApi.cs 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); } From 2a37b27bb43910d745dce36a562257115f751f12 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Fri, 28 Jul 2023 23:14:36 +0300 Subject: [PATCH 5/5] [store] other use default client factory --- Core/Startup.cs | 1 + modules/ContentStore/Service/AnimBoobsService.cs | 8 +++----- modules/ContentStore/Service/AnimalAsAService.cs | 11 ++++------- modules/ContentStore/Service/CatApiService.cs | 3 ++- modules/ContentStore/Service/DogApiService.cs | 3 ++- modules/ContentStore/Service/OBoobsService.cs | 8 +++----- 6 files changed, 15 insertions(+), 19 deletions(-) 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/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/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/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()