Add BaseProvider

This commit is contained in:
xjasonlyu
2022-05-24 21:02:52 +08:00
parent c4b22a357c
commit 1d4aa9f31c

View File

@@ -0,0 +1,70 @@
#if __EMBY__
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using HttpRequestOptions = MediaBrowser.Common.Net.HttpRequestOptions;
#else
using Microsoft.Extensions.Logging;
#endif
namespace Jellyfin.Plugin.JavTube.Providers;
public abstract class BaseProvider
{
private readonly ILogger _logger;
#if __EMBY__
private readonly IHttpClient _httpClient;
protected BaseProvider(IHttpClient httpClient,ILogger logger)
{
_httpClient = httpClient;
Logger = logger;
}
#else
private readonly IHttpClientFactory _httpClientFactory;
protected BaseProvider(IHttpClientFactory httpClientFactory, ILogger logger)
{
_httpClientFactory = httpClientFactory;
_logger = logger;
}
#endif
#if __EMBY__
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
#else
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
#endif
{
LogInfo("GetImageResponse for url: {0}", url);
return GetAsync(url, cancellationToken);
}
#if __EMBY__
private async Task<HttpResponseInfo> GetAsync(string url, CancellationToken cancellationToken)
{
return await _httpClient.GetResponse(new HttpRequestOptions
{
Url = url,
EnableDefaultUserAgent = false,
UserAgent = Constant.UserAgent,
CancellationToken = cancellationToken
}).ConfigureAwait(false);
}
#else
private async Task<HttpResponseMessage> GetAsync(string url, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", Constant.UserAgent);
return await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
}
#endif
protected void LogInfo(string message, params object[] args)
{
#if __EMBY__
_logger.Info(message, args);
#else
// ReSharper disable once TemplateIsNotCompileTimeConstantProblem
_logger.LogInformation(message, args);
#endif
}
}