|
| 1 | +namespace Cloudflare.NET.Core; |
| 2 | + |
| 3 | +using System.Net.Http.Headers; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Configures <see cref="HttpClient" /> instances for Cloudflare API communication. |
| 7 | +/// Used by both DI registration and dynamic client creation paths to ensure |
| 8 | +/// consistent HTTP client configuration. |
| 9 | +/// </summary> |
| 10 | +/// <remarks> |
| 11 | +/// <para> |
| 12 | +/// This class centralizes the HttpClient configuration logic that was previously |
| 13 | +/// duplicated between the DI registration path and the dynamic client creation path. |
| 14 | +/// </para> |
| 15 | +/// <para> |
| 16 | +/// Configuration includes: |
| 17 | +/// </para> |
| 18 | +/// <list type="bullet"> |
| 19 | +/// <item> |
| 20 | +/// <description> |
| 21 | +/// <b>Base Address</b> - Set from <see cref="CloudflareApiOptions.ApiBaseUrl" />. |
| 22 | +/// </description> |
| 23 | +/// </item> |
| 24 | +/// <item> |
| 25 | +/// <description> |
| 26 | +/// <b>Timeout</b> - Set to a long value (5 minutes) to allow the resilience pipeline |
| 27 | +/// to handle timeouts. The actual timeout is controlled by the pipeline. |
| 28 | +/// </description> |
| 29 | +/// </item> |
| 30 | +/// <item> |
| 31 | +/// <description> |
| 32 | +/// <b>Authorization Header</b> - Optionally set from <see cref="CloudflareApiOptions.ApiToken" />. |
| 33 | +/// </description> |
| 34 | +/// </item> |
| 35 | +/// </list> |
| 36 | +/// </remarks> |
| 37 | +public static class CloudflareHttpClientConfigurator |
| 38 | +{ |
| 39 | + #region Constants |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// The HttpClient timeout. This is intentionally long to allow the resilience |
| 43 | + /// pipeline's timeout strategies to be the effective timeout controllers. |
| 44 | + /// </summary> |
| 45 | + /// <remarks> |
| 46 | + /// Ref: https://learn.microsoft.com/en-us/dotnet/core/resilience/http-resilience#httpclient-timeout |
| 47 | + /// </remarks> |
| 48 | + private static readonly TimeSpan HttpClientTimeout = TimeSpan.FromMinutes(5); |
| 49 | + |
| 50 | + #endregion |
| 51 | + |
| 52 | + |
| 53 | + #region Methods - Public |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Configures an <see cref="HttpClient" /> for Cloudflare API communication. |
| 57 | + /// </summary> |
| 58 | + /// <param name="client">The HttpClient to configure.</param> |
| 59 | + /// <param name="options">The Cloudflare API options containing configuration values.</param> |
| 60 | + /// <param name="setAuthorizationHeader"> |
| 61 | + /// If true, sets the Authorization header from the options. Set to false when |
| 62 | + /// authentication is handled separately (e.g., via <see cref="Auth.AuthenticationHandler" />). |
| 63 | + /// </param> |
| 64 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="client" /> or <paramref name="options" /> is null.</exception> |
| 65 | + /// <exception cref="InvalidOperationException">Thrown when <see cref="CloudflareApiOptions.ApiBaseUrl" /> is null or whitespace.</exception> |
| 66 | + /// <remarks> |
| 67 | + /// <para> |
| 68 | + /// The HttpClient timeout is set to a long value (5 minutes) to ensure that the resilience |
| 69 | + /// pipeline's timeout strategies are the effective timeout controllers. Without this, the |
| 70 | + /// HttpClient's default 100-second timeout would interfere with retry attempts. |
| 71 | + /// </para> |
| 72 | + /// </remarks> |
| 73 | + /// <example> |
| 74 | + /// <code> |
| 75 | + /// // For DI registration path (auth handled by AuthenticationHandler) |
| 76 | + /// CloudflareHttpClientConfigurator.Configure(httpClient, options, setAuthorizationHeader: false); |
| 77 | + /// |
| 78 | + /// // For named clients or dynamic clients (auth header set directly) |
| 79 | + /// CloudflareHttpClientConfigurator.Configure(httpClient, options, setAuthorizationHeader: true); |
| 80 | + /// </code> |
| 81 | + /// </example> |
| 82 | + public static void Configure(HttpClient client, |
| 83 | + CloudflareApiOptions options, |
| 84 | + bool setAuthorizationHeader = true) |
| 85 | + { |
| 86 | + ArgumentNullException.ThrowIfNull(client); |
| 87 | + ArgumentNullException.ThrowIfNull(options); |
| 88 | + |
| 89 | + // Validate the API base URL. |
| 90 | + if (string.IsNullOrWhiteSpace(options.ApiBaseUrl)) |
| 91 | + throw new InvalidOperationException( |
| 92 | + "Cloudflare API Base URL is missing. Please configure it in the 'Cloudflare' settings section."); |
| 93 | + |
| 94 | + // Set the base address for all requests. |
| 95 | + client.BaseAddress = new Uri(options.ApiBaseUrl); |
| 96 | + |
| 97 | + // Set a long HttpClient.Timeout so that our resilience pipeline's TotalRequestTimeout is the effective timeout. |
| 98 | + // Ref: https://learn.microsoft.com/en-us/dotnet/core/resilience/http-resilience#httpclient-timeout |
| 99 | + client.Timeout = HttpClientTimeout; |
| 100 | + |
| 101 | + // Optionally set the Authorization header. |
| 102 | + if (setAuthorizationHeader && !string.IsNullOrWhiteSpace(options.ApiToken)) |
| 103 | + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", options.ApiToken); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Configures only the base properties of an <see cref="HttpClient" /> (base address and timeout), |
| 109 | + /// without setting the Authorization header. |
| 110 | + /// </summary> |
| 111 | + /// <param name="client">The HttpClient to configure.</param> |
| 112 | + /// <param name="options">The Cloudflare API options containing configuration values.</param> |
| 113 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="client" /> or <paramref name="options" /> is null.</exception> |
| 114 | + /// <exception cref="InvalidOperationException">Thrown when <see cref="CloudflareApiOptions.ApiBaseUrl" /> is null or whitespace.</exception> |
| 115 | + /// <remarks> |
| 116 | + /// <para> |
| 117 | + /// This overload is provided for backward compatibility and convenience when authentication |
| 118 | + /// is handled by a separate <see cref="Auth.AuthenticationHandler" />. |
| 119 | + /// </para> |
| 120 | + /// </remarks> |
| 121 | + public static void ConfigureBase(HttpClient client, CloudflareApiOptions options) |
| 122 | + { |
| 123 | + Configure(client, options, setAuthorizationHeader: false); |
| 124 | + } |
| 125 | + |
| 126 | + #endregion |
| 127 | +} |
0 commit comments