The goal is to eliminate the need for a "global setup" step for HTTP communication. This can instead be done in the composition root as part of the factory to request FlurlClient objects.pull/151/head
parent
2a79a50d50
commit
d04b10f9d0
@ -1,9 +0,0 @@
|
||||
using Flurl;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
public interface IServerInfo
|
||||
{
|
||||
Url BuildRequest();
|
||||
string SanitizedBaseUrl { get; }
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using Flurl.Http;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
public interface IServiceRequestBuilder
|
||||
{
|
||||
string SanitizedBaseUrl { get; }
|
||||
IFlurlRequest Request(params object[] path);
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
using Flurl;
|
||||
using TrashLib.Extensions;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
public class ServerInfo : IServerInfo
|
||||
{
|
||||
private readonly IServiceConfiguration _config;
|
||||
|
||||
public ServerInfo(IServiceConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public Url BuildRequest()
|
||||
{
|
||||
var apiKey = _config.ApiKey;
|
||||
var baseUrl = _config.BaseUrl;
|
||||
|
||||
return baseUrl
|
||||
.AppendPathSegment("api/v3")
|
||||
.SetQueryParams(new {apikey = apiKey});
|
||||
}
|
||||
|
||||
public string SanitizedBaseUrl => FlurlLogging.SanitizeUrl(_config.BaseUrl);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using Flurl.Http;
|
||||
using TrashLib.Http;
|
||||
|
||||
namespace TrashLib.Config.Services;
|
||||
|
||||
public class ServiceRequestBuilder : IServiceRequestBuilder
|
||||
{
|
||||
private readonly IServiceConfiguration _config;
|
||||
private readonly IFlurlClientFactory _clientFactory;
|
||||
|
||||
public ServiceRequestBuilder(IServiceConfiguration config, IFlurlClientFactory clientFactory)
|
||||
{
|
||||
_config = config;
|
||||
_clientFactory = clientFactory;
|
||||
}
|
||||
|
||||
public IFlurlRequest Request(params object[] path)
|
||||
{
|
||||
var client = _clientFactory.Get(_config.BaseUrl);
|
||||
return client.Request(new[] {"api", "v3"}.Concat(path).ToArray())
|
||||
.SetQueryParams(new {apikey = _config.ApiKey});
|
||||
}
|
||||
|
||||
public string SanitizedBaseUrl => FlurlLogging.SanitizeUrl(_config.BaseUrl);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using Common.Networking;
|
||||
using Flurl.Http;
|
||||
using Flurl.Http.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using TrashLib.Config.Settings;
|
||||
|
||||
namespace TrashLib.Http;
|
||||
|
||||
public class FlurlClientFactory : IFlurlClientFactory
|
||||
{
|
||||
private readonly ILogger _log;
|
||||
private readonly ISettingsProvider _settingsProvider;
|
||||
private readonly PerBaseUrlFlurlClientFactory _factory;
|
||||
|
||||
public FlurlClientFactory(ILogger log, ISettingsProvider settingsProvider)
|
||||
{
|
||||
_log = log;
|
||||
_settingsProvider = settingsProvider;
|
||||
_factory = new PerBaseUrlFlurlClientFactory();
|
||||
}
|
||||
|
||||
public IFlurlClient Get(string baseUrl)
|
||||
{
|
||||
var client = _factory.Get(baseUrl);
|
||||
client.Settings = GetClientSettings();
|
||||
return client;
|
||||
}
|
||||
|
||||
private ClientFlurlHttpSettings GetClientSettings()
|
||||
{
|
||||
var settings = new ClientFlurlHttpSettings
|
||||
{
|
||||
JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings
|
||||
{
|
||||
// This makes sure that null properties, such as maxSize and preferredSize in Radarr
|
||||
// Quality Definitions, do not get written out to JSON request bodies.
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
})
|
||||
};
|
||||
|
||||
FlurlLogging.SetupLogging(settings, _log);
|
||||
|
||||
// ReSharper disable once InvertIf
|
||||
if (!_settingsProvider.Settings.EnableSslCertificateValidation)
|
||||
{
|
||||
_log.Warning(
|
||||
"Security Risk: Certificate validation is being DISABLED because setting " +
|
||||
"`enable_ssl_certificate_validation` is set to `false`");
|
||||
settings.HttpClientFactory = new UntrustedCertClientFactory();
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Flurl.Http;
|
||||
|
||||
namespace TrashLib.Extensions;
|
||||
namespace TrashLib.Http;
|
||||
|
||||
public static class FlurlExtensions
|
||||
{
|
@ -0,0 +1,8 @@
|
||||
using Flurl.Http;
|
||||
|
||||
namespace TrashLib.Http;
|
||||
|
||||
public interface IFlurlClientFactory
|
||||
{
|
||||
IFlurlClient Get(string baseUrl);
|
||||
}
|
Loading…
Reference in new issue