Class CloudflareHttpClientConfigurator
- Namespace
- Cloudflare.NET.Core
- Assembly
- Cloudflare.NET.dll
Configures HttpClient instances for Cloudflare API communication. Used by both DI registration and dynamic client creation paths to ensure consistent HTTP client configuration.
public static class CloudflareHttpClientConfigurator
- Inheritance
-
CloudflareHttpClientConfigurator
- Inherited Members
Remarks
This class centralizes the HttpClient configuration logic that was previously duplicated between the DI registration path and the dynamic client creation path.
Configuration includes:
- Base Address - Set from ApiBaseUrl.
- Timeout - Set to a long value (5 minutes) to allow the resilience pipeline to handle timeouts. The actual timeout is controlled by the pipeline.
- Authorization Header - Optionally set from ApiToken.
Methods
Configure(HttpClient, CloudflareApiOptions, bool)
Configures an HttpClient for Cloudflare API communication.
public static void Configure(HttpClient client, CloudflareApiOptions options, bool setAuthorizationHeader = true)
Parameters
clientHttpClientThe HttpClient to configure.
optionsCloudflareApiOptionsThe Cloudflare API options containing configuration values.
setAuthorizationHeaderboolIf true, sets the Authorization header from the options. Set to false when authentication is handled separately (e.g., via AuthenticationHandler).
Examples
// For DI registration path (auth handled by AuthenticationHandler)
CloudflareHttpClientConfigurator.Configure(httpClient, options, setAuthorizationHeader: false);
// For named clients or dynamic clients (auth header set directly)
CloudflareHttpClientConfigurator.Configure(httpClient, options, setAuthorizationHeader: true);
Remarks
The HttpClient timeout is set to a long value (5 minutes) to ensure that the resilience pipeline's timeout strategies are the effective timeout controllers. Without this, the HttpClient's default 100-second timeout would interfere with retry attempts.
Exceptions
- ArgumentNullException
Thrown when
clientoroptionsis null.- InvalidOperationException
Thrown when ApiBaseUrl is null or whitespace.
ConfigureBase(HttpClient, CloudflareApiOptions)
Configures only the base properties of an HttpClient (base address and timeout), without setting the Authorization header.
public static void ConfigureBase(HttpClient client, CloudflareApiOptions options)
Parameters
clientHttpClientThe HttpClient to configure.
optionsCloudflareApiOptionsThe Cloudflare API options containing configuration values.
Remarks
This overload is provided for backward compatibility and convenience when authentication is handled by a separate AuthenticationHandler.
Exceptions
- ArgumentNullException
Thrown when
clientoroptionsis null.- InvalidOperationException
Thrown when ApiBaseUrl is null or whitespace.