Skip to content

Commit

Permalink
Adding IConsulClientFactory and BaseConsulClientFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
TMaster committed May 29, 2016
1 parent b0881f0 commit 2b6ede8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Consul/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Net.Http.Headers;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Consul.Interfaces;

namespace Consul
{
Expand Down Expand Up @@ -470,6 +471,17 @@ public ConsulClient(ConsulClientConfiguration config, HttpClient client)
}
}

/// <summary>
/// Initializes a new Consul client using a <see cref="IConsulClientFactory"/> to initialize the HttpClient and the Configuration
/// </summary>
/// <param name="clientFactory">Factoty to initialzie the configuration and the Http client</param>
public ConsulClient(IConsulClientFactory clientFactory)
{
Config = clientFactory.CreateConsulConfiguration();

HttpClient = clientFactory.CreateHttpClient();
}

#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls

Expand Down
2 changes: 2 additions & 0 deletions Consul/Consul.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Event.cs" />
<Compile Include="Health.cs" />
<Compile Include="Client.cs" />
<Compile Include="Interfaces\IConsulClientFactory.cs" />
<Compile Include="Interfaces\ICoordinateEndpoint.cs" />
<Compile Include="Interfaces\IDisposableLock.cs" />
<Compile Include="Interfaces\IDistributedLock.cs" />
Expand All @@ -73,6 +74,7 @@
<Compile Include="Interfaces\ISessionEndpoint.cs" />
<Compile Include="Interfaces\IStatusEndpoint.cs" />
<Compile Include="PreparedQuery.cs" />
<Compile Include="Utilities\BaseConsulClientFactory.cs" />
<Compile Include="Utilities\JsonConverters.cs" />
<Compile Include="KV.cs" />
<Compile Include="Lock.cs" />
Expand Down
22 changes: 22 additions & 0 deletions Consul/Interfaces/IConsulClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Consul.Interfaces
{

/// <summary>
/// Intializes the building blocks to create a dully functioning <see cref="ConsulClient"/>
/// </summary>
public interface IConsulClientFactory
{

ConsulClientConfiguration CreateConsulConfiguration();

HttpClient CreateHttpClient();

}
}
55 changes: 55 additions & 0 deletions Consul/Utilities/BaseConsulClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Consul.Interfaces;

namespace Consul.Utilities
{
/// <summary>
/// Basic implementation of <see cref="IConsulClientFactory"/>
/// </summary>
/// <remarks>
/// This basic structure provide a working <see cref="ConsulClient"/>.
/// If you wish to change the following settings, please inhrit from the base class and override the necessary settings.
/// </remarks>
public class BaseConsulClientFactory : IConsulClientFactory
{
virtual public ConsulClientConfiguration CreateConsulConfiguration()
{
return new ConsulClientConfiguration();
}

/// <summary>
/// Creates new instance of <see cref="HttpClient"/>
/// </summary>
/// <remarks>
/// Please note, the HttpClient must accept the "application/json" content, if you override this method please make sure you call the base method or add the header manually.
/// </remarks>
/// <returns></returns>
virtual public HttpClient CreateHttpClient()
{
var httpClient = new HttpClient(CreateHttpHandler())
{
Timeout = TimeSpan.FromMinutes(15)

};

httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

httpClient.DefaultRequestHeaders.Add("Keep-Alive","true");

return httpClient;

}

//Other entities should not have access to this nethod, the CreateHttpClient only should call this method.
virtual protected WebRequestHandler CreateHttpHandler()
{
return new WebRequestHandler();
}
}
}

0 comments on commit 2b6ede8

Please sign in to comment.