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
Register the factory and its logging filter on a service collection:
final services = ServiceCollection()
// Add logging services
..addLogging((builder) {
builder
..setMinimumLevel(LogLevel.debug)
..addSimpleConsole();
})
// Add HTTP client factory
..addHttpClient()
// Add HTTP client logging filter
..addHttpClientLogging();
Resolve the factory to create clients:
final factory = provider.getRequiredService<HttpClientFactory>();
Named Clients
Each name carries its own handler pipeline, header redaction, and handler lifetime:
services.addHttpClient('GitHub').configureHttpClient((client, sp) {
// Note: BaseClient doesn't have baseAddress, this is conceptual
print('Configuring GitHub client');
}).redactLoggedHeaderNames([
'Authorization',
'X-GitHub-Token'
]).setHandlerLifetime(const Duration(minutes: 5));
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 named HTTP client instances returned by HttpClientFactory.
- HttpClientBuilder
- Fluent builder used to configure named HTTP clients.
- HttpClientFactory
-
A factory abstraction for a component that can create
http.BaseClientinstances 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 named HTTP client instances returned by HttpClientFactory.
- HttpClientLoggerHandler
- HttpMessageHandler
- HttpMessageHandlerBuilder
- HttpMessageHandlerBuilderFilter
-
Used by the
DefaultHttpClientFactoryto apply additional configuration to the HttpMessageHandlerBuilder immediately before callingbuild. - 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)