• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / HashGate / 18764422115

23 Oct 2025 09:16PM UTC coverage: 75.69% (-3.8%) from 79.529%
18764422115

push

github

pwelter34
Add HttpClientBuilder for fluent HttpClient configuration

118 of 188 branches covered (62.77%)

Branch coverage included in aggregate %.

0 of 22 new or added lines in 1 file covered. (0.0%)

321 of 392 relevant lines covered (81.89%)

12.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/HashGate.HttpClient/HttpClientBuilder.cs
1
using Microsoft.Extensions.Options;
2

3
namespace HashGate.HttpClient;
4

5
/// <summary>
6
/// Provides a fluent API for building and configuring <see cref="System.Net.Http.HttpClient"/> instances
7
/// with custom message handlers and HMAC authentication.
8
/// </summary>
9
/// <example>
10
/// <code>
11
/// var httpClient = new HttpClientBuilder()
12
///     .AddHmacAuthentication("myClientId", "mySecretKey")
13
///     .Configure(client =>
14
///     {
15
///         client.BaseAddress = new Uri("https://api.example.com");
16
///         client.Timeout = TimeSpan.FromSeconds(30);
17
///     })
18
///     .Build();
19
/// 
20
/// var response = await httpClient.GetAsync("/api/endpoint");
21
/// </code>
22
/// </example>
23
public class HttpClientBuilder
24
{
NEW
25
    private readonly List<DelegatingHandler> _handlers = [];
×
26
    private Action<System.Net.Http.HttpClient>? _clientConfigurator;
27

28
    /// <summary>
29
    /// Adds a custom <see cref="DelegatingHandler"/> to the HTTP client pipeline.
30
    /// </summary>
31
    /// <param name="handler">The delegating handler to add to the pipeline.</param>
32
    /// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
33
    public HttpClientBuilder AddHandler(DelegatingHandler handler)
34
    {
NEW
35
        _handlers.Add(handler);
×
NEW
36
        return this;
×
37
    }
38

39
    /// <summary>
40
    /// Adds HMAC authentication to the HTTP client pipeline using the specified client ID and secret.
41
    /// </summary>
42
    /// <param name="client">The client identifier used for HMAC authentication.</param>
43
    /// <param name="secret">The secret key used for HMAC signature generation.</param>
44
    /// <param name="signedHeaders">Optional list of HTTP headers to include in the HMAC signature. 
45
    /// If not specified, default headers will be used.</param>
46
    /// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
47
    public HttpClientBuilder AddHmacAuthentication(string client, string secret, IReadOnlyList<string>? signedHeaders = null)
48
    {
NEW
49
        var options = new HmacAuthenticationOptions
×
NEW
50
        {
×
NEW
51
            Client = client,
×
NEW
52
            Secret = secret,
×
NEW
53
            SignedHeaders = signedHeaders ?? HmacAuthenticationShared.DefaultSignedHeaders
×
NEW
54
        };
×
55

NEW
56
        var wrapper = new OptionsWrapper<HmacAuthenticationOptions>(options);
×
NEW
57
        var handler = new HmacAuthenticationHttpHandler(wrapper);
×
58

NEW
59
        _handlers.Add(handler);
×
60

NEW
61
        return this;
×
62
    }
63

64
    /// <summary>
65
    /// Configures the <see cref="System.Net.Http.HttpClient"/> instance with additional settings.
66
    /// </summary>
67
    /// <param name="configure">An action to configure the HTTP client (e.g., setting base address, timeout, default headers).</param>
68
    /// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
69
    public HttpClientBuilder Configure(Action<System.Net.Http.HttpClient> configure)
70
    {
NEW
71
        _clientConfigurator = configure;
×
NEW
72
        return this;
×
73
    }
74

75
    /// <summary>
76
    /// Builds and returns a configured <see cref="System.Net.Http.HttpClient"/> instance
77
    /// with all registered handlers and configurations applied.
78
    /// </summary>
79
    /// <returns>A fully configured <see cref="System.Net.Http.HttpClient"/> instance.</returns>
80
    public System.Net.Http.HttpClient Build()
81
    {
NEW
82
        HttpMessageHandler pipeline = new HttpClientHandler();
×
83

84
        // Chain handlers in reverse order
NEW
85
        for (int i = _handlers.Count - 1; i >= 0; i--)
×
86
        {
NEW
87
            _handlers[i].InnerHandler = pipeline;
×
NEW
88
            pipeline = _handlers[i];
×
89
        }
90

NEW
91
        var client = new System.Net.Http.HttpClient(pipeline);
×
92

NEW
93
        _clientConfigurator?.Invoke(client);
×
94

NEW
95
        return client;
×
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc