use named http clients

pull/3935/head
crobibero 4 years ago
parent abb79bf810
commit 64a811d783

@ -1,9 +1,11 @@
using System;
using System.ComponentModel;
using System.Net.Http.Headers;
using Jellyfin.Api.TypeConverters;
using Jellyfin.Server.Extensions;
using Jellyfin.Server.Middleware;
using Jellyfin.Server.Models;
using MediaBrowser.Common;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
@ -21,14 +23,17 @@ namespace Jellyfin.Server
public class Startup
{
private readonly IServerConfigurationManager _serverConfigurationManager;
private readonly IApplicationHost _applicationHost;
/// <summary>
/// Initializes a new instance of the <see cref="Startup" /> class.
/// </summary>
/// <param name="serverConfigurationManager">The server configuration manager.</param>
public Startup(IServerConfigurationManager serverConfigurationManager)
/// <param name="applicationHost">The application host.</param>
public Startup(IServerConfigurationManager serverConfigurationManager, IApplicationHost applicationHost)
{
_serverConfigurationManager = serverConfigurationManager;
_applicationHost = applicationHost;
}
/// <summary>
@ -49,10 +54,18 @@ namespace Jellyfin.Server
services.AddJellyfinApiAuthorization();
services
.AddTransient<UserAgentDelegatingHandler>()
.AddHttpClient<DefaultHttpClient>()
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler())
.AddHttpMessageHandler<UserAgentDelegatingHandler>();
.AddHttpClient(NamedClient.Default, c =>
{
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString));
})
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
services.AddHttpClient(NamedClient.MusicBrainz, c =>
{
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString));
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(_applicationHost.ApplicationUserAgentAddress));
})
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
}
/// <summary>

@ -1,77 +0,0 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Default http client.
/// </summary>
public class DefaultHttpClient
{
private readonly HttpClient _httpClient;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultHttpClient" /> class.
/// </summary>
/// <param name="httpClient">Instance of httpclient.</param>
public DefaultHttpClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
/// <summary>
/// Make GET request.
/// </summary>
/// <param name="url">Url to request.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns>
public Task<HttpResponseMessage> GetAsync(Uri url)
{
return _httpClient.GetAsync(url);
}
/// <summary>
/// Make GET request.
/// </summary>
/// <param name="url">Url to request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns>
public Task<HttpResponseMessage> GetAsync(Uri url, CancellationToken cancellationToken)
{
return _httpClient.GetAsync(url, cancellationToken);
}
/// <summary>
/// Get stream.
/// </summary>
/// <param name="url">Url to get stream from.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="Stream"/>.</returns>
public Task<Stream> GetStreamAsync(Uri url)
{
return _httpClient.GetStreamAsync(url);
}
/// <summary>
/// Send request.
/// </summary>
/// <param name="requestMessage">The <see cref="HttpRequestMessage"/>.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage)
{
return _httpClient.SendAsync(requestMessage);
}
/// <summary>
/// Send request.
/// </summary>
/// <param name="requestMessage">The <see cref="HttpRequestMessage"/>.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
{
return _httpClient.SendAsync(requestMessage, cancellationToken);
}
}
}

@ -0,0 +1,18 @@
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Registered http client names.
/// </summary>
public static class NamedClient
{
/// <summary>
/// Gets the value for the default named http client.
/// </summary>
public const string Default = nameof(Default);
/// <summary>
/// Gets the value for the MusicBrainz named http client.
/// </summary>
public const string MusicBrainz = nameof(MusicBrainz);
}
}

@ -1,49 +0,0 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// User agent delegating handler.
/// Adds User-Agent header to all requests.
/// </summary>
public class UserAgentDelegatingHandler : DelegatingHandler
{
private readonly ProductInfoHeaderValue[] _userAgentValues;
/// <summary>
/// Initializes a new instance of the <see cref="UserAgentDelegatingHandler"/> class.
/// </summary>
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
public UserAgentDelegatingHandler(IApplicationHost applicationHost)
{
_userAgentValues = new[]
{
new ProductInfoHeaderValue(applicationHost.Name.Replace(' ', '-'), applicationHost.ApplicationVersionString)
};
}
/// <summary>
/// Send request message.
/// </summary>
/// <param name="request">The request message.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> containing the <see cref="HttpResponseMessage"/>.</returns>
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request.Headers.UserAgent.Count == 0)
{
for (var i = 0; i < _userAgentValues.Length; i++)
{
request.Headers.UserAgent.Add(_userAgentValues[i]);
}
}
return base.SendAsync(request, cancellationToken);
}
}
}
Loading…
Cancel
Save