forked from PlayFab/consuldotnet
-
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.
Adding IConsulClientFactory and BaseConsulClientFactory
- Loading branch information
TMaster
committed
May 29, 2016
1 parent
b0881f0
commit 2b6ede8
Showing
4 changed files
with
91 additions
and
0 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
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 |
---|---|---|
@@ -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(); | ||
|
||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |