migrate to IHttpClientFactory in InstallationManager

This commit is contained in:
crobibero 2020-08-31 11:30:05 -06:00
parent 8215f15c45
commit 533b981668

View File

@ -34,7 +34,7 @@ namespace Emby.Server.Implementations.Updates
/// </summary> /// </summary>
private readonly ILogger<InstallationManager> _logger; private readonly ILogger<InstallationManager> _logger;
private readonly IApplicationPaths _appPaths; private readonly IApplicationPaths _appPaths;
private readonly IHttpClient _httpClient; private readonly IHttpClientFactory _httpClientFactory;
private readonly IJsonSerializer _jsonSerializer; private readonly IJsonSerializer _jsonSerializer;
private readonly IServerConfigurationManager _config; private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
@ -63,7 +63,7 @@ namespace Emby.Server.Implementations.Updates
ILogger<InstallationManager> logger, ILogger<InstallationManager> logger,
IApplicationHost appHost, IApplicationHost appHost,
IApplicationPaths appPaths, IApplicationPaths appPaths,
IHttpClient httpClient, IHttpClientFactory httpClientFactory,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
IServerConfigurationManager config, IServerConfigurationManager config,
IFileSystem fileSystem, IFileSystem fileSystem,
@ -80,7 +80,7 @@ namespace Emby.Server.Implementations.Updates
_logger = logger; _logger = logger;
_applicationHost = appHost; _applicationHost = appHost;
_appPaths = appPaths; _appPaths = appPaths;
_httpClient = httpClient; _httpClientFactory = httpClientFactory;
_jsonSerializer = jsonSerializer; _jsonSerializer = jsonSerializer;
_config = config; _config = config;
_fileSystem = fileSystem; _fileSystem = fileSystem;
@ -116,17 +116,10 @@ namespace Emby.Server.Implementations.Updates
{ {
try try
{ {
using (var response = await _httpClient.SendAsync( using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
new HttpRequestOptions .GetAsync(manifest, cancellationToken).ConfigureAwait(false);
{ await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
Url = manifest,
CancellationToken = cancellationToken,
CacheMode = CacheMode.Unconditional,
CacheLength = TimeSpan.FromMinutes(3)
},
HttpMethod.Get).ConfigureAwait(false))
using (Stream stream = response.Content)
{
try try
{ {
return await _jsonSerializer.DeserializeFromStreamAsync<IReadOnlyList<PackageInfo>>(stream).ConfigureAwait(false); return await _jsonSerializer.DeserializeFromStreamAsync<IReadOnlyList<PackageInfo>>(stream).ConfigureAwait(false);
@ -137,7 +130,6 @@ namespace Emby.Server.Implementations.Updates
return Array.Empty<PackageInfo>(); return Array.Empty<PackageInfo>();
} }
} }
}
catch (UriFormatException ex) catch (UriFormatException ex)
{ {
_logger.LogError(ex, "The URL configured for the plugin repository manifest URL is not valid: {Manifest}", manifest); _logger.LogError(ex, "The URL configured for the plugin repository manifest URL is not valid: {Manifest}", manifest);
@ -360,20 +352,13 @@ namespace Emby.Server.Implementations.Updates
// Always override the passed-in target (which is a file) and figure it out again // Always override the passed-in target (which is a file) and figure it out again
string targetDir = Path.Combine(_appPaths.PluginsPath, package.Name); string targetDir = Path.Combine(_appPaths.PluginsPath, package.Name);
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(package.SourceUrl, cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
// CA5351: Do Not Use Broken Cryptographic Algorithms // CA5351: Do Not Use Broken Cryptographic Algorithms
#pragma warning disable CA5351 #pragma warning disable CA5351
using (var res = await _httpClient.SendAsync( using var md5 = MD5.Create();
new HttpRequestOptions
{
Url = package.SourceUrl,
CancellationToken = cancellationToken,
// We need it to be buffered for setting the position
BufferContent = true
},
HttpMethod.Get).ConfigureAwait(false))
using (var stream = res.Content)
using (var md5 = MD5.Create())
{
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var hash = Hex.Encode(md5.ComputeHash(stream)); var hash = Hex.Encode(md5.ComputeHash(stream));
@ -394,7 +379,6 @@ namespace Emby.Server.Implementations.Updates
stream.Position = 0; stream.Position = 0;
_zipClient.ExtractAllFromZip(stream, targetDir, true); _zipClient.ExtractAllFromZip(stream, targetDir, true);
}
#pragma warning restore CA5351 #pragma warning restore CA5351
} }