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