From a43bed74b5240bc27ba1f17d166fa52cf2a1ce29 Mon Sep 17 00:00:00 2001 From: Mitchell Barry Date: Sat, 24 Mar 2018 11:54:13 -0500 Subject: [PATCH] Allow users to override HttpClientHandler used Ideally, would be used to circumvent issues surfaced in DotNetDevs/Synology#115 --- Synology/Classes/SynologyHttpClient.cs | 27 ++++ .../Extensions/ServiceCollectionExtension.cs | 9 ++ Synology/SynologyConnection.cs | 119 +++++++++--------- 3 files changed, 94 insertions(+), 61 deletions(-) create mode 100644 Synology/Classes/SynologyHttpClient.cs diff --git a/Synology/Classes/SynologyHttpClient.cs b/Synology/Classes/SynologyHttpClient.cs new file mode 100644 index 0000000..c813682 --- /dev/null +++ b/Synology/Classes/SynologyHttpClient.cs @@ -0,0 +1,27 @@ +using System.Net.Http; + +namespace Synology.Classes +{ + /// + /// + public class SynologyHttpClient : HttpClient + { + /// + /// + public SynologyHttpClient() + { + } + + /// + /// + public SynologyHttpClient(HttpMessageHandler handler) : base(handler) + { + } + + /// + /// + public SynologyHttpClient(HttpMessageHandler handler, bool disposeHandler) : base(handler, disposeHandler) + { + } + } +} diff --git a/Synology/Extensions/ServiceCollectionExtension.cs b/Synology/Extensions/ServiceCollectionExtension.cs index c315ccc..9bc736e 100644 --- a/Synology/Extensions/ServiceCollectionExtension.cs +++ b/Synology/Extensions/ServiceCollectionExtension.cs @@ -21,9 +21,18 @@ public static IServiceCollection AddSynology(this IServiceCollection services, A { if (configure == null) throw new ArgumentNullException(nameof(configure)); + var defaultHttpClient = new SynologyHttpClient() + { + DefaultRequestHeaders = + { + ExpectContinue = false + } + }; + services.AddOptions(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(defaultHttpClient); services.AddTransient(); configure(new SynologyBuilder(services)); diff --git a/Synology/SynologyConnection.cs b/Synology/SynologyConnection.cs index 4c62410..357e181 100644 --- a/Synology/SynologyConnection.cs +++ b/Synology/SynologyConnection.cs @@ -1,66 +1,63 @@ -using System; -using System.Net.Http; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; +using Synology.Classes; using Synology.Interfaces; +using System; +using System.Net.Http; namespace Synology { - /// - /// - /// - /// - internal sealed class SynologyConnection : ISynologyConnection - { - /// - /// - /// - public ILogger Logger { get; } - - /// - /// - /// - public ISynologyConnectionSettings Settings { get; } - - /// - /// - /// - public HttpClient Client { get; } - - /// - /// - /// - public IServiceProvider ServiceProvider { get; } - - /// - /// - /// - /// - /// - /// - public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) - { - Settings = settings; - ServiceProvider = serviceProvider; - Logger = loggerFactory.CreateLogger(); - - Logger.LogDebug($"Creating new connection to {Settings.BaseHost} with{(Settings.Ssl ? "" : "out")} SSL to port {Settings.Port}"); - - Client = new HttpClient - { - BaseAddress = new Uri(Settings.WebApiUrl), - DefaultRequestHeaders = { - ExpectContinue = false - } - }; - } - - /// - /// - /// - public void Dispose() - { - Logger.LogDebug("Closing connection"); - Client?.Dispose(); - } - } + /// + /// + /// + /// + internal sealed class SynologyConnection : ISynologyConnection + { + /// + /// + /// + public ILogger Logger { get; } + + /// + /// + /// + public ISynologyConnectionSettings Settings { get; } + + /// + /// + /// + public HttpClient Client { get; } + + /// + /// + /// + public IServiceProvider ServiceProvider { get; } + + /// + /// + /// + /// + /// + /// + /// + public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, SynologyHttpClient httpClient) + { + Settings = settings; + ServiceProvider = serviceProvider; + Logger = loggerFactory.CreateLogger(); + + Logger.LogDebug($"Creating new connection to {Settings.BaseHost} with{(Settings.Ssl ? "" : "out")} SSL to port {Settings.Port}"); + + httpClient.BaseAddress = new Uri(Settings.WebApiUrl); + Client = httpClient; + } + + /// + /// + /// + public void Dispose() + { + Logger.LogDebug("Closing connection"); + Client?.Dispose(); + } + } } \ No newline at end of file