using Knot.Shared.Kernel.Configuration; using Knot.Shared.Kernel.Constants; using Microsoft.Extensions.Caching.Memory; using System; using System.Net.Http; using System.Net.Http.Json; using System.Text.Json; using System.Threading.Tasks; namespace Host.Services; public sealed class KlipyService : IKlipyService { private readonly ISettingsService _settings; private readonly IMemoryCache _cache; private readonly IHttpClientFactory _httpClientFactory; public KlipyService(ISettingsService settings, IMemoryCache cache, IHttpClientFactory httpClientFactory) { _settings = settings; _cache = cache; _httpClientFactory = httpClientFactory; } public async Task GetTrendingAsync() { var conf = _settings.Current; if (!conf.EnableKlipy || string.IsNullOrEmpty(conf.KlipyApiKey)) throw new InvalidOperationException(Errors.KlipyNotConfigured); var cacheKeyTrending = $"klipy_trending_{conf.KlipyApiKey}"; if (_cache.TryGetValue(cacheKeyTrending, out JsonElement cachedResult)) { return cachedResult; } var customerId = string.IsNullOrWhiteSpace(conf.KlipyCustomerId) ? Klipy.DefaultCustomerId : conf.KlipyCustomerId; var client = _httpClientFactory.CreateClient(); var urlCo = string.Format(Klipy.ApiUrlCo, conf.KlipyApiKey, Klipy.ResourceTrending, "", customerId); var response = await client.GetAsync(urlCo); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { var urlCom = string.Format(Klipy.ApiUrlCom, conf.KlipyApiKey, Klipy.ResourceTrending, "", customerId); response = await client.GetAsync(urlCom); } if (!response.IsSuccessStatusCode) throw new HttpRequestException(Errors.KlipyApiError); } var result = await response.Content.ReadFromJsonAsync(); _cache.Set(cacheKeyTrending, result, TimeSpan.FromMinutes(Klipy.TrendingCacheMinutes)); return result; } public async Task SearchAsync(string query) { var conf = _settings.Current; if (!conf.EnableKlipy || string.IsNullOrEmpty(conf.KlipyApiKey)) throw new InvalidOperationException(Errors.KlipyNotConfigured); if (string.IsNullOrWhiteSpace(query)) throw new ArgumentException(Errors.InvalidQuery, nameof(query)); var cacheKey = $"klipy_search_{conf.KlipyApiKey}_{query.ToLowerInvariant()}"; if (_cache.TryGetValue(cacheKey, out JsonElement cachedResult)) { return cachedResult; } var customerId = string.IsNullOrWhiteSpace(conf.KlipyCustomerId) ? Klipy.DefaultCustomerId : conf.KlipyCustomerId; var client = _httpClientFactory.CreateClient(); var queryParam = $"q={Uri.EscapeDataString(query)}"; var urlCo = string.Format(Klipy.ApiUrlCo, conf.KlipyApiKey, Klipy.ResourceSearch, queryParam, customerId); var response = await client.GetAsync(urlCo); if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { var urlCom = string.Format(Klipy.ApiUrlCom, conf.KlipyApiKey, Klipy.ResourceSearch, queryParam, customerId); response = await client.GetAsync(urlCom); } if (!response.IsSuccessStatusCode) throw new HttpRequestException(Errors.KlipySearchError); } var result = await response.Content.ReadFromJsonAsync(); _cache.Set(cacheKey, result, TimeSpan.FromMinutes(Klipy.SearchCacheMinutes)); return result; } }