[store] kitties now have own http client factory

This commit is contained in:
Andriy
2023-07-28 22:44:51 +03:00
parent 462f3c9836
commit c19696b360
8 changed files with 79 additions and 30 deletions
@@ -15,5 +15,6 @@ public abstract class AbstractNsfwService : IRandomMediaService
_allowNsfw = allowNsfw;
}
public async Task<bool> CanView(Chat chat, User user) => await _allowNsfw.GetValueAsync(chat.Id);
public async Task<bool> CanView(Chat chat, User user)
=> await _allowNsfw.GetValueAsync(chat.Id);
}
@@ -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>("Uri");
var token = section.GetValue<string>("Token");
_httpClient = new HttpClient
{
BaseAddress = uri,
DefaultRequestHeaders =
{
Authorization = new AuthenticationHeaderValue("Bearer", token)
}
};
_api = api;
_allowNsfw = allowNsfw;
}
public async Task<RandomMedia> 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
);
}
@@ -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)
{
}
@@ -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)
{
}
}