Skip to content

Commit

Permalink
add convenience methods (adding multiple certificates at once, multip…
Browse files Browse the repository at this point in the history
…le policies at once, etc)
  • Loading branch information
myarichuk committed May 3, 2020
1 parent a12e3b4 commit 34e6254
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Simple.HttpClientFactory/HttpClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,37 @@ internal class HttpClientBuilder : IHttpClientBuilder
private readonly List<X509Certificate2> _certificates = new List<X509Certificate2>();
private readonly List<IAsyncPolicy<HttpResponseMessage>> _policies = new List<IAsyncPolicy<HttpResponseMessage>>();
private readonly List<DelegatingHandler> _middlewareHandlers = new List<DelegatingHandler>();
private readonly Dictionary<string, string> _defaultHeaders = new Dictionary<string, string>();

public IHttpClientBuilder WithDefaultHeaders(IReadOnlyDictionary<string, string> headers)
{
foreach(var kvp in headers)
{
if(!_defaultHeaders.ContainsKey(kvp.Key))
_defaultHeaders.Add(kvp.Key, kvp.Value);
}
return this;
}

public IHttpClientBuilder WithCertificates(IEnumerable<X509Certificate2> certificates)
{
_certificates.AddRange(certificates);
return this;
}

public IHttpClientBuilder WithCertificate(params X509Certificate2[] certificates)
{
_certificates.AddRange(certificates);
return this;
}

public IHttpClientBuilder WithPolicies(IEnumerable<IAsyncPolicy<HttpResponseMessage>> policies)
{
_policies.AddRange(policies);
return this;
}


public IHttpClientBuilder WithPolicy(IAsyncPolicy<HttpResponseMessage> policy)
{
_policies.Add(policy);
Expand Down Expand Up @@ -130,7 +153,7 @@ public HttpClient Build(Action<HttpClientHandler> clientHandlerConfigurator = nu
}

var client = ConstructClientWithMiddleware(clientHandler, policyHandler);

if (_timeout.HasValue)
client.Timeout = _timeout.Value;

Expand Down Expand Up @@ -158,6 +181,16 @@ private HttpClient ConstructClientWithMiddleware<TClientHandler>(TClientHandler
}
else
client = new HttpClient(clientHandler, true);

if(_defaultHeaders.Count > 0)
{
foreach(var header in _defaultHeaders)
{
if(!client.DefaultRequestHeaders.Contains(header.Key))
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
}

return client;
}
}
Expand Down
17 changes: 17 additions & 0 deletions Simple.HttpClientFactory/IHttpClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
using Polly;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;

namespace Simple.HttpClientFactory
{
public interface IHttpClientBuilder
{
/// <summary>
/// Add default headers to be added to teach request
/// </summary>
IHttpClientBuilder WithDefaultHeaders(IReadOnlyDictionary<string, string> headers);

/// <summary>
/// Configure one or more SSL certificates to use
/// </summary>
IHttpClientBuilder WithCertificates(IEnumerable<X509Certificate2> certificates);

/// <summary>
/// Configure one or more SSL certificates to use
/// </summary>
IHttpClientBuilder WithCertificate(params X509Certificate2[] certificates);

/// <summary>
/// Chain multiple Polly error policies
/// </summary>
/// <remarks>Policies will be evaluated in the order of their configuration</remarks>
IHttpClientBuilder WithPolicies(IEnumerable<IAsyncPolicy<HttpResponseMessage>> policies);

/// <summary>
/// Chain Polly error policy
/// </summary>
Expand Down

0 comments on commit 34e6254

Please sign in to comment.