@@ -17,14 +17,37 @@ internal class HttpClientBuilder : IHttpClientBuilder
1717 private readonly List < X509Certificate2 > _certificates = new List < X509Certificate2 > ( ) ;
1818 private readonly List < IAsyncPolicy < HttpResponseMessage > > _policies = new List < IAsyncPolicy < HttpResponseMessage > > ( ) ;
1919 private readonly List < DelegatingHandler > _middlewareHandlers = new List < DelegatingHandler > ( ) ;
20+ private readonly Dictionary < string , string > _defaultHeaders = new Dictionary < string , string > ( ) ;
2021
22+ public IHttpClientBuilder WithDefaultHeaders ( IReadOnlyDictionary < string , string > headers )
23+ {
24+ foreach ( var kvp in headers )
25+ {
26+ if ( ! _defaultHeaders . ContainsKey ( kvp . Key ) )
27+ _defaultHeaders . Add ( kvp . Key , kvp . Value ) ;
28+ }
29+ return this ;
30+ }
2131
32+ public IHttpClientBuilder WithCertificates ( IEnumerable < X509Certificate2 > certificates )
33+ {
34+ _certificates . AddRange ( certificates ) ;
35+ return this ;
36+ }
37+
2238 public IHttpClientBuilder WithCertificate ( params X509Certificate2 [ ] certificates )
2339 {
2440 _certificates . AddRange ( certificates ) ;
2541 return this ;
2642 }
2743
44+ public IHttpClientBuilder WithPolicies ( IEnumerable < IAsyncPolicy < HttpResponseMessage > > policies )
45+ {
46+ _policies . AddRange ( policies ) ;
47+ return this ;
48+ }
49+
50+
2851 public IHttpClientBuilder WithPolicy ( IAsyncPolicy < HttpResponseMessage > policy )
2952 {
3053 _policies . Add ( policy ) ;
@@ -130,7 +153,7 @@ public HttpClient Build(Action<HttpClientHandler> clientHandlerConfigurator = nu
130153 }
131154
132155 var client = ConstructClientWithMiddleware ( clientHandler , policyHandler ) ;
133-
156+
134157 if ( _timeout . HasValue )
135158 client . Timeout = _timeout . Value ;
136159
@@ -158,6 +181,16 @@ private HttpClient ConstructClientWithMiddleware<TClientHandler>(TClientHandler
158181 }
159182 else
160183 client = new HttpClient ( clientHandler , true ) ;
184+
185+ if ( _defaultHeaders . Count > 0 )
186+ {
187+ foreach ( var header in _defaultHeaders )
188+ {
189+ if ( ! client . DefaultRequestHeaders . Contains ( header . Key ) )
190+ client . DefaultRequestHeaders . Add ( header . Key , header . Value ) ;
191+ }
192+ }
193+
161194 return client ;
162195 }
163196 }
0 commit comments