Files
manga-reader/MangaReader.Core/Search/MangaSearchProviderBase.cs

33 lines
1.0 KiB
C#

using MangaReader.Core.HttpService;
using System.Text.Json;
namespace MangaReader.Core.Search;
public abstract class MangaSearchProviderBase<T>(IHttpService httpService) : IMangaSearchProvider
{
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
public async Task<MangaSearchResult[]> SearchAsync(string keyword, CancellationToken cancellationToken)
{
T? searchResult = await GetSearchResultAsync(keyword);
if (searchResult == null)
return [];
return GetSearchResult(searchResult);
}
private async Task<T?> GetSearchResultAsync(string keyword)
{
string url = GetSearchUrl(keyword);
string response = await httpService.GetStringAsync(url);
return JsonSerializer.Deserialize<T>(response, _jsonSerializerOptions);
}
protected abstract string GetSearchUrl(string keyword);
protected abstract MangaSearchResult[] GetSearchResult(T searchResult);
}