diff --git a/appveyor.yml b/appveyor.yml index d9e88cd..872f321 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,8 +1,6 @@ version: '{build}' skip_tags: true -image: Visual Studio 2017 -configuration: Release -install: +image: Visual Studio 2019 build_script: - ps: ./Build.ps1 test: off diff --git a/src/Seq.Api/Client/SeqApiClient.cs b/src/Seq.Api/Client/SeqApiClient.cs index f505450..13445df 100644 --- a/src/Seq.Api/Client/SeqApiClient.cs +++ b/src/Seq.Api/Client/SeqApiClient.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Net; using System.Net.Http; @@ -56,7 +57,20 @@ public class SeqApiClient : IDisposable /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) + [Obsolete("Prefer `SeqApiClient(serverUrl, apiKey, handler => handler.UseDefaultCredentials = true)` instead."), EditorBrowsable(EditorBrowsableState.Never)] + public SeqApiClient(string serverUrl, string apiKey, bool useDefaultCredentials) + : this(serverUrl, apiKey, handler => handler.UseDefaultCredentials = useDefaultCredentials) + { + } + + /// + /// Construct a . + /// + /// The base URL of the Seq server. + /// An API key to use when making requests to the server, if required. + /// An optional callback to configure the used when making HTTP requests + /// to the Seq API. + public SeqApiClient(string serverUrl, string apiKey = null, Action configureHttpClientHandler = null) { ServerUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl)); @@ -65,10 +79,11 @@ public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCrede var handler = new HttpClientHandler { - CookieContainer = _cookies, - UseDefaultCredentials = useDefaultCredentials + CookieContainer = _cookies }; + configureHttpClientHandler?.Invoke(handler); + var baseAddress = serverUrl; if (!baseAddress.EndsWith("/")) baseAddress += "/"; diff --git a/src/Seq.Api/SeqConnection.cs b/src/Seq.Api/SeqConnection.cs index 8388519..d7e7ba8 100644 --- a/src/Seq.Api/SeqConnection.cs +++ b/src/Seq.Api/SeqConnection.cs @@ -14,7 +14,9 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Net; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Seq.Api.Client; @@ -27,7 +29,7 @@ namespace Seq.Api /// /// Exposes high-level (typed) interactions with the Seq API through various resource groups. /// - public class SeqConnection : ISeqConnection + public class SeqConnection : ISeqConnection, IDisposable { readonly object _sync = new object(); readonly Dictionary> _resourceGroups = new Dictionary>(); @@ -39,12 +41,34 @@ public class SeqConnection : ISeqConnection /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - public SeqConnection(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) + [Obsolete("Prefer `SeqConnection(serverUrl, apiKey, handler => handler.UseDefaultCredentials = true)` instead."), EditorBrowsable(EditorBrowsableState.Never)] + public SeqConnection(string serverUrl, string apiKey, bool useDefaultCredentials) { if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); Client = new SeqApiClient(serverUrl, apiKey, useDefaultCredentials); } + + /// + /// Construct a . + /// + /// The base URL of the Seq server. + /// An API key to use when making requests to the server, if required. + /// An optional callback to configure the used when making HTTP requests + /// to the Seq API. + public SeqConnection(string serverUrl, string apiKey = null, Action configureHttpClientHandler = null) + { + if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); + Client = new SeqApiClient(serverUrl, apiKey, configureHttpClientHandler); + } + /// + /// + /// + public void Dispose() + { + Client.Dispose(); + } + /// /// Access the lower-level that can be used for resource-oriented navigation through /// the HTTP API. diff --git a/test/Seq.Api.Tests/SeqConnectionTests.cs b/test/Seq.Api.Tests/SeqConnectionTests.cs new file mode 100644 index 0000000..2c30f87 --- /dev/null +++ b/test/Seq.Api.Tests/SeqConnectionTests.cs @@ -0,0 +1,20 @@ +using Xunit; + +namespace Seq.Api.Tests +{ + public class SeqConnectionTests + { + [Fact] + public void WhenConstructedTheHandlerConfigurationCallbackIsCalled() + { + var callCount = 0; + + using var _ = new SeqConnection("https://test.example.com", null, handler => { + Assert.NotNull(handler); + ++callCount; + }); + + Assert.Equal(1, callCount); + } + } +}