Skip to content

Commit

Permalink
Allow users to override HttpClientHandler used
Browse files Browse the repository at this point in the history
Ideally, would be used to circumvent issues surfaced in DotNetDevs#115
  • Loading branch information
mitch-b committed Mar 24, 2018
1 parent 21ff2f3 commit a43bed7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 61 deletions.
27 changes: 27 additions & 0 deletions Synology/Classes/SynologyHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net.Http;

namespace Synology.Classes
{
/// <summary>
/// </summary>
public class SynologyHttpClient : HttpClient
{
/// <summary>
/// </summary>
public SynologyHttpClient()
{
}

/// <summary>
/// </summary>
public SynologyHttpClient(HttpMessageHandler handler) : base(handler)
{
}

/// <summary>
/// </summary>
public SynologyHttpClient(HttpMessageHandler handler, bool disposeHandler) : base(handler, disposeHandler)
{
}
}
}
9 changes: 9 additions & 0 deletions Synology/Extensions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISynologyConnectionSettings, SynologyConnectionSettings>();
services.AddSingleton<SidContainer, SidContainer>();
services.AddSingleton<SynologyHttpClient>(defaultHttpClient);
services.AddTransient<ISynologyConnection, SynologyConnection>();

configure(new SynologyBuilder(services));
Expand Down
119 changes: 58 additions & 61 deletions Synology/SynologyConnection.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <inheritdoc />
/// <summary>
///
/// </summary>
internal sealed class SynologyConnection : ISynologyConnection
{
/// <inheritdoc />
/// <summary>
/// </summary>
public ILogger Logger { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public ISynologyConnectionSettings Settings { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public HttpClient Client { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public IServiceProvider ServiceProvider { get; }

/// <summary>
///
/// </summary>
/// <param name="settings"></param>
/// <param name="loggerFactory"></param>
/// <param name="serviceProvider"></param>
public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
{
Settings = settings;
ServiceProvider = serviceProvider;
Logger = loggerFactory.CreateLogger<SynologyConnection>();

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
}
};
}

/// <inheritdoc />
/// <summary>
/// </summary>
public void Dispose()
{
Logger.LogDebug("Closing connection");
Client?.Dispose();
}
}
/// <inheritdoc />
/// <summary>
///
/// </summary>
internal sealed class SynologyConnection : ISynologyConnection
{
/// <inheritdoc />
/// <summary>
/// </summary>
public ILogger Logger { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public ISynologyConnectionSettings Settings { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public HttpClient Client { get; }

/// <inheritdoc />
/// <summary>
/// </summary>
public IServiceProvider ServiceProvider { get; }

/// <summary>
///
/// </summary>
/// <param name="settings"></param>
/// <param name="loggerFactory"></param>
/// <param name="serviceProvider"></param>
/// <param name="httpClient"></param>
public SynologyConnection(ISynologyConnectionSettings settings, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, SynologyHttpClient httpClient)
{
Settings = settings;
ServiceProvider = serviceProvider;
Logger = loggerFactory.CreateLogger<SynologyConnection>();

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;
}

/// <inheritdoc />
/// <summary>
/// </summary>
public void Dispose()
{
Logger.LogDebug("Closing connection");
Client?.Dispose();
}
}
}

0 comments on commit a43bed7

Please sign in to comment.