mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-07-13 17:13:00 +01:00
Move duplicate concrete IAbstractHttpClientFactorys to Common.HttpClientFactory
This commit is contained in:
@@ -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
-3
@@ -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;
|
||||
|
||||
@@ -12,6 +12,7 @@ using Tgstation.Server.Api.Models;
|
||||
using Tgstation.Server.Api.Models.Request;
|
||||
using Tgstation.Server.Client;
|
||||
using Tgstation.Server.Client.Components;
|
||||
using Tgstation.Server.Common;
|
||||
using Tgstation.Server.Host.Components.Byond;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
using Tgstation.Server.Host.IO;
|
||||
@@ -102,21 +103,22 @@ namespace Tgstation.Server.Tests.Live.Instance
|
||||
var generalConfigOptionsMock = new Mock<IOptions<GeneralConfiguration>>();
|
||||
generalConfigOptionsMock.SetupGet(x => x.Value).Returns(new GeneralConfiguration());
|
||||
|
||||
var byondInstaller = new PlatformIdentifier().IsWindows
|
||||
? (IByondInstaller)new WindowsByondInstaller(
|
||||
var assemblyInformationProvider = new AssemblyInformationProvider();
|
||||
var fileDownloader = new FileDownloader(
|
||||
new HttpClientFactory(assemblyInformationProvider.ProductInfoHeaderValue),
|
||||
Mock.Of<ILogger<FileDownloader>>());
|
||||
|
||||
IByondInstaller byondInstaller = new PlatformIdentifier().IsWindows
|
||||
? new WindowsByondInstaller(
|
||||
Mock.Of<IProcessExecutor>(),
|
||||
Mock.Of<IIOManager>(),
|
||||
new FileDownloader(
|
||||
new ConcreteHttpClientFactory(),
|
||||
Mock.Of<ILogger<FileDownloader>>()),
|
||||
fileDownloader,
|
||||
generalConfigOptionsMock.Object,
|
||||
Mock.Of<ILogger<WindowsByondInstaller>>())
|
||||
: new PosixByondInstaller(
|
||||
Mock.Of<IPostWriteHandler>(),
|
||||
Mock.Of<IIOManager>(),
|
||||
new FileDownloader(
|
||||
new ConcreteHttpClientFactory(),
|
||||
Mock.Of<ILogger<FileDownloader>>()),
|
||||
fileDownloader,
|
||||
Mock.Of<ILogger<PosixByondInstaller>>());
|
||||
|
||||
using var windowsByondInstaller = byondInstaller as WindowsByondInstaller;
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using Tgstation.Server.Common;
|
||||
using Tgstation.Server.Host.Utils;
|
||||
|
||||
namespace Tgstation.Server.Tests.Live.Instance
|
||||
{
|
||||
sealed class ConcreteHttpClientFactory : IAbstractHttpClientFactory
|
||||
{
|
||||
public IHttpClient CreateClient() => new HttpClient();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using Tgstation.Server.Common;
|
||||
using Tgstation.Server.Host.Utils;
|
||||
|
||||
namespace Tgstation.Server.Migrator
|
||||
{
|
||||
sealed class ConcreteHttpClientFactory : IAbstractHttpClientFactory
|
||||
{
|
||||
public IHttpClient CreateClient() => new HttpClient();
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,9 @@ using Octokit;
|
||||
|
||||
using Tgstation.Server.Api;
|
||||
using Tgstation.Server.Client;
|
||||
using Tgstation.Server.Common;
|
||||
using Tgstation.Server.Host.IO;
|
||||
using Tgstation.Server.Host.Setup;
|
||||
using Tgstation.Server.Migrator;
|
||||
|
||||
using FileMode = System.IO.FileMode;
|
||||
|
||||
@@ -264,6 +264,7 @@ try
|
||||
new ProductInfoHeaderValue(
|
||||
assemblyName.Name!,
|
||||
assemblyName.Version!.Semver().ToString());
|
||||
var httpClientFactory = new HttpClientFactory(productInfoHeaderValue);
|
||||
if (!runtimeInstalled)
|
||||
{
|
||||
// RUNTIME DONWLOAD
|
||||
@@ -279,9 +280,9 @@ try
|
||||
|
||||
Console.WriteLine($"Downloading {downloadUri} to {Path.GetFullPath(dotnetDownloadFilePath)}...");
|
||||
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.DefaultRequestHeaders.UserAgent.Add(productInfoHeaderValue);
|
||||
var webRequestTask = httpClient.GetAsync(downloadUri);
|
||||
using var httpClient = httpClientFactory.CreateClient();
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, downloadUri);
|
||||
var webRequestTask = httpClient.SendAsync(request, default);
|
||||
using var response = await webRequestTask;
|
||||
response.EnsureSuccessStatusCode();
|
||||
using (var responseStream = await response.Content.ReadAsStreamAsync())
|
||||
@@ -382,7 +383,6 @@ try
|
||||
// TGS5 DOWNLOAD AND UNZIP
|
||||
Console.WriteLine("Downloading TGS5...");
|
||||
|
||||
var httpClientFactory = new ConcreteHttpClientFactory();
|
||||
using (var loggerFactory = LoggerFactory.Create(builder => { }))
|
||||
{
|
||||
var fileDownloader = new FileDownloader(httpClientFactory, loggerFactory.CreateLogger<FileDownloader>());
|
||||
|
||||
Reference in New Issue
Block a user