|
| 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 | +{ |
| 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 | + { |
| 35 | + _handlers.Add(handler); |
| 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 | + { |
| 49 | + var options = new HmacAuthenticationOptions |
| 50 | + { |
| 51 | + Client = client, |
| 52 | + Secret = secret, |
| 53 | + SignedHeaders = signedHeaders ?? HmacAuthenticationShared.DefaultSignedHeaders |
| 54 | + }; |
| 55 | + |
| 56 | + var wrapper = new OptionsWrapper<HmacAuthenticationOptions>(options); |
| 57 | + var handler = new HmacAuthenticationHttpHandler(wrapper); |
| 58 | + |
| 59 | + _handlers.Add(handler); |
| 60 | + |
| 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 | + { |
| 71 | + _clientConfigurator = configure; |
| 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 | + { |
| 82 | + HttpMessageHandler pipeline = new HttpClientHandler(); |
| 83 | + |
| 84 | + // Chain handlers in reverse order |
| 85 | + for (int i = _handlers.Count - 1; i >= 0; i--) |
| 86 | + { |
| 87 | + _handlers[i].InnerHandler = pipeline; |
| 88 | + pipeline = _handlers[i]; |
| 89 | + } |
| 90 | + |
| 91 | + var client = new System.Net.Http.HttpClient(pipeline); |
| 92 | + |
| 93 | + _clientConfigurator?.Invoke(client); |
| 94 | + |
| 95 | + return client; |
| 96 | + } |
| 97 | +} |
0 commit comments