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] [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()