Move duplicate concrete IAbstractHttpClientFactorys to Common.HttpClientFactory

This commit is contained in:
Dominion
2023-04-23 13:57:57 -04:00
parent 43e1fec1b0
commit 4ecdf8afe1
15 changed files with 68 additions and 42 deletions
@@ -0,0 +1,41 @@
using System;
using System.Net.Http.Headers;
namespace Tgstation.Server.Common
{
/// <summary>
/// <see cref="IAbstractHttpClientFactory"/> that creates <see cref="HttpClient"/>s.
/// </summary>
public sealed class HttpClientFactory : IAbstractHttpClientFactory
{
/// <inheritdoc />
public IHttpClient CreateClient()
{
var client = new HttpClient();
try
{
client.DefaultRequestHeaders.UserAgent.Add(userAgent);
return client;
}
catch
{
client.Dispose();
throw;
}
}
/// <summary>
/// The <see cref="ProductInfoHeaderValue"/> used as created client's User-Agent header on request.
/// </summary>
readonly ProductInfoHeaderValue userAgent;
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientFactory"/> class.
/// </summary>
/// <param name="userAgent">The value of <see cref="userAgent"/>.</param>
public HttpClientFactory(ProductInfoHeaderValue userAgent)
{
this.userAgent = userAgent ?? throw new ArgumentNullException(nameof(userAgent));
}
}
}
@@ -1,6 +1,4 @@
using Tgstation.Server.Common;
namespace Tgstation.Server.Host.Utils
namespace Tgstation.Server.Common
{
/// <summary>
/// Creates <see cref="IHttpClient"/>s.
@@ -5,6 +5,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using Cyberboss.AspNetCore.AsyncInitializer;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
@@ -17,12 +18,15 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Display;
using Tgstation.Server.Api;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Components;
using Tgstation.Server.Host.Components.Byond;
using Tgstation.Server.Host.Components.Chat;
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Tgstation.Server.Host.Utils;
using Tgstation.Server.Common;
namespace Tgstation.Server.Host.IO
{
@@ -3,8 +3,8 @@
using Microsoft.Extensions.Logging;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
namespace Tgstation.Server.Host.Security.OAuth
{
@@ -15,7 +15,6 @@ using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
namespace Tgstation.Server.Host.Security.OAuth
{
@@ -3,8 +3,8 @@
using Microsoft.Extensions.Logging;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
namespace Tgstation.Server.Host.Security.OAuth
{
@@ -3,8 +3,8 @@
using Microsoft.Extensions.Logging;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
namespace Tgstation.Server.Host.Security.OAuth
{
@@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
@@ -3,8 +3,8 @@
using Microsoft.Extensions.Logging;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Utils;
namespace Tgstation.Server.Host.Security.OAuth
{
@@ -15,6 +15,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Tgstation.Server.Api.Models.Response;
using Tgstation.Server.Common;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Core;
using Tgstation.Server.Host.Database;