Register and construct IHttpClient correctly

This commit is contained in:
Mark Monteiro 2020-04-04 21:18:09 -04:00
parent adf0e8d3fd
commit e16c16dd51
2 changed files with 10 additions and 17 deletions

View File

@ -123,6 +123,7 @@ namespace Emby.Server.Implementations
private IMediaEncoder _mediaEncoder; private IMediaEncoder _mediaEncoder;
private ISessionManager _sessionManager; private ISessionManager _sessionManager;
private IHttpServer _httpServer; private IHttpServer _httpServer;
private IHttpClient _httpClient;
/// <summary> /// <summary>
/// Gets a value indicating whether this instance can self restart. /// Gets a value indicating whether this instance can self restart.
@ -255,8 +256,6 @@ namespace Emby.Server.Implementations
protected readonly IXmlSerializer XmlSerializer; protected readonly IXmlSerializer XmlSerializer;
public IHttpClient HttpClient { get; private set; }
protected INetworkManager NetworkManager { get; set; } protected INetworkManager NetworkManager { get; set; }
public IJsonSerializer JsonSerializer { get; private set; } public IJsonSerializer JsonSerializer { get; private set; }
@ -363,10 +362,7 @@ namespace Emby.Server.Implementations
} }
} }
/// <summary> /// <inheritdoc/>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
public string Name => ApplicationProductName; public string Name => ApplicationProductName;
/// <summary> /// <summary>
@ -580,12 +576,7 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton(FileSystemManager); serviceCollection.AddSingleton(FileSystemManager);
serviceCollection.AddSingleton<TvdbClientManager>(); serviceCollection.AddSingleton<TvdbClientManager>();
HttpClient = new HttpClientManager.HttpClientManager( serviceCollection.AddSingleton<IHttpClient, HttpClientManager.HttpClientManager>();
ApplicationPaths,
LoggerFactory.CreateLogger<HttpClientManager.HttpClientManager>(),
FileSystemManager,
() => ApplicationUserAgent);
serviceCollection.AddSingleton(HttpClient);
serviceCollection.AddSingleton(NetworkManager); serviceCollection.AddSingleton(NetworkManager);
@ -728,6 +719,7 @@ namespace Emby.Server.Implementations
_mediaEncoder = Resolve<IMediaEncoder>(); _mediaEncoder = Resolve<IMediaEncoder>();
_sessionManager = Resolve<ISessionManager>(); _sessionManager = Resolve<ISessionManager>();
_httpServer = Resolve<IHttpServer>(); _httpServer = Resolve<IHttpServer>();
_httpClient = Resolve<IHttpClient>();
((SqliteDisplayPreferencesRepository)Resolve<IDisplayPreferencesRepository>()).Initialize(); ((SqliteDisplayPreferencesRepository)Resolve<IDisplayPreferencesRepository>()).Initialize();
((AuthenticationRepository)Resolve<IAuthenticationRepository>()).Initialize(); ((AuthenticationRepository)Resolve<IAuthenticationRepository>()).Initialize();
@ -1423,7 +1415,7 @@ namespace Emby.Server.Implementations
try try
{ {
using (var response = await HttpClient.SendAsync( using (var response = await _httpClient.SendAsync(
new HttpRequestOptions new HttpRequestOptions
{ {
Url = apiUrl, Url = apiUrl,

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
@ -24,7 +25,7 @@ namespace Emby.Server.Implementations.HttpClientManager
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IApplicationPaths _appPaths; private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly Func<string> _defaultUserAgentFn; private readonly IApplicationHost _appHost;
/// <summary> /// <summary>
/// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests. /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
@ -40,12 +41,12 @@ namespace Emby.Server.Implementations.HttpClientManager
IApplicationPaths appPaths, IApplicationPaths appPaths,
ILogger<HttpClientManager> logger, ILogger<HttpClientManager> logger,
IFileSystem fileSystem, IFileSystem fileSystem,
Func<string> defaultUserAgentFn) IApplicationHost appHost)
{ {
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
_fileSystem = fileSystem; _fileSystem = fileSystem;
_appPaths = appPaths ?? throw new ArgumentNullException(nameof(appPaths)); _appPaths = appPaths ?? throw new ArgumentNullException(nameof(appPaths));
_defaultUserAgentFn = defaultUserAgentFn; _appHost = appHost;
} }
/// <summary> /// <summary>
@ -91,7 +92,7 @@ namespace Emby.Server.Implementations.HttpClientManager
if (options.EnableDefaultUserAgent if (options.EnableDefaultUserAgent
&& !request.Headers.TryGetValues(HeaderNames.UserAgent, out _)) && !request.Headers.TryGetValues(HeaderNames.UserAgent, out _))
{ {
request.Headers.Add(HeaderNames.UserAgent, _defaultUserAgentFn()); request.Headers.Add(HeaderNames.UserAgent, _appHost.ApplicationUserAgent);
} }
switch (options.DecompressionMethod) switch (options.DecompressionMethod)