forked from DotNetDevs/Synology
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to override HttpClientHandler used
Ideally, would be used to circumvent issues surfaced in DotNetDevs#115
- Loading branch information
Showing
3 changed files
with
94 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |