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

loresoft / HashGate / 29697765278

19 Jul 2026 05:55PM UTC coverage: 65.982% (+2.8%) from 63.149%
29697765278

push

github

pwelter34
Add AddHmacClient and runtime options updates

235 of 472 branches covered (49.79%)

Branch coverage included in aggregate %.

146 of 173 new or added lines in 11 files covered. (84.39%)

1 existing line in 1 file now uncovered.

696 of 939 relevant lines covered (74.12%)

26.54 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 System.Net.Http;
2

3
using Microsoft.Extensions.Options;
4

5
namespace HashGate.HttpClient;
6

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

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

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

NEW
58
        var wrapper = new StaticOptionsMonitor<HmacAuthenticationOptions>(options);
×
59
        var handler = new HmacAuthenticationHttpHandler(wrapper);
×
60

61
        _handlers.Add(handler);
×
62

63
        return this;
×
64
    }
65

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

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

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

93
        var client = new System.Net.Http.HttpClient(pipeline);
×
94

95
        _clientConfigurator?.Invoke(client);
×
96

97
        return client;
×
98
    }
99

NEW
100
    private sealed class StaticOptionsMonitor<TOptions>(TOptions currentValue) : IOptionsMonitor<TOptions>
×
101
        where TOptions : class
102
    {
NEW
103
        public TOptions CurrentValue { get; } = currentValue;
×
104

NEW
105
        public TOptions Get(string? name) => CurrentValue;
×
106

NEW
107
        public IDisposable? OnChange(Action<TOptions, string?> listener) => null;
×
108
    }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc