http library

Provides typed HTTP client factory with lifetime management and request/response logging.

This library implements HTTP client abstractions inspired by Microsoft.Extensions.Http, offering centralized configuration, automatic logging, and proper resource management for HTTP clients.

HTTP Client Factory

Create and configure HTTP clients through dependency injection:

final services = ServiceCollection()
  ..addHttpClient()
  ..addHttpClientLogging();

final factory = provider.getRequiredService<HttpClientFactory>();
final client = factory.createClient();

Named Clients

Register named clients with specific configurations:

services.addHttpClient('GitHub')
  .configureHttpClient((client, sp) {
    // Configure client
  })
  .redactLoggedHeaderNames(['Authorization'])
  .setHandlerLifetime(Duration(minutes: 5));

final githubClient = factory.createClient('GitHub');

Request Logging

Automatically log HTTP requests and responses:

services
  ..addLogging((builder) => builder.addSimpleConsole())
  ..addHttpClient()
  ..addHttpClientLogging();  // Enables request/response logging

Message Handlers

Add custom message handlers for cross-cutting concerns:

services.addHttpClient('API')
  .addHttpMessageHandler((sp) => AuthenticationHandler())
  .addHttpMessageHandler((sp) => RetryHandler());

Classes

DelegatingHandler
Base class for composing HTTP handlers into a pipeline.
HttpClientAsyncLogger
An abstraction for asyncronous custom HTTP request logging for a named HttpClient instances returned by HttpClientFactory.
HttpClientBuilder
Fluent builder used to configure named HTTP clients.
HttpClientFactory
A factory abstraction for a component that can create http.BaseClient instances with custom configuration for a given logical name.
HttpClientFactoryOptions
Options used by the default HTTP client factory.
HttpClientLogger
An abstraction for custom HTTP request logging for a named HttpClient instances returned by HttpClientFactory.
HttpClientLoggerHandler
HttpMessageHandler
HttpMessageHandlerBuilder
HttpMessageHandlerBuilderFilter
Used by the DefaultHttpClientFactory to apply additional configuration to the HttpMessageHandlerBuilder immediately before calling build.
HttpMessageHandlerFactory
LifetimeTrackingHttpMessageHandler
A delegating handler that tracks the lifetime of HTTP message handlers and prevents premature disposal.
LoggingHttpMessageHandler
Handles logging of the lifecycle for an HTTP request.
LoggingHttpMessageHandlerBuilderFilter
A filter that adds a LoggingHttpMessageHandler to the HTTP client pipeline for all named clients.
LoggingScopeHttpMessageHandler
A delegating handler that establishes a logging scope for each HTTP request.

Extensions

HttpClientFactoryServiceCollectionExtensions on ServiceCollection
ServiceCollection extensions for configuring HTTP clients.

Typedefs

HttpClientAction = void Function(BaseClient client, ServiceProvider services)
HttpMessageHandlerBuilderAction = void Function(HttpMessageHandlerBuilder builder, ServiceProvider services)