Skip to content
GitHubDiscord

HTTP (lexigram-http)

Outbound HTTP client for the Lexigram Framework.


Async outbound HTTP client for the Lexigram Framework. Provides a first-class async HTTP client backed by aiohttp, with connection pooling, typed request/ response contexts, interceptor chains, base URL clients, streaming support, and DI integration.

This package focuses on making outbound HTTP requests — for inbound web servers, use lexigram-web. Resilience patterns (retry, circuit breaker) are layered in through lexigram-resilience, not built in by default.

Terminal window
uv add lexigram-http
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.http import HTTPModule, HTTPClientConfig
@module(imports=[HTTPModule.configure(HTTPClientConfig())])
class AppModule(Module):
pass
async def main():
async with Application.boot(modules=[AppModule]) as app:
from lexigram.http import HTTPClient
async with HTTPClient.session_context() as client:
response = await client.get("https://api.example.com/users/123")
if response.ok:
user = await response.json()
print(user)
if __name__ == "__main__":
import asyncio
asyncio.run(main())

Zero-config usage: Call HTTPModule.configure() with no arguments to use defaults.

application.yaml
http:
pool:
max_connections: 100
max_keepalive_connections: 50
timeout: 30.0
trust_env: true
cookie_jar: true
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_HTTP__POOL__MAX_CONNECTIONS=100
export LEX_HTTP__POOL__TIMEOUT=30.0
export LEX_HTTP__TRUST_ENV=true
from lexigram.http import HTTPModule, HTTPClientConfig, ConnectionPoolConfig
HTTPModule.configure(
HTTPClientConfig(
pool=ConnectionPoolConfig(
max_connections=100,
timeout=30.0,
),
trust_env=True,
cookie_jar=True,
)
)
FieldDefaultEnv varDescription
pool.max_connections10LEX_HTTP__POOL__MAX_CONNECTIONSTotal concurrent connections across all hosts
pool.max_keepalive_connections5LEX_HTTP__POOL__MAX_KEEPALIVE_CONNECTIONSKeep-alive connections per host
pool.max_connections_per_host10LEX_HTTP__POOL__MAX_CONNECTIONS_PER_HOSTMax connections per individual host
pool.timeout30.0LEX_HTTP__POOL__TIMEOUTRequest timeout (seconds)
pool.ttl_dns_cache300LEX_HTTP__POOL__TTL_DNS_CACHEDNS cache TTL (seconds)
proxynullLEX_HTTP__PROXYHTTP/HTTPS proxy URL
trust_envTrueLEX_HTTP__TRUST_ENVRead proxy settings from environment variables
cookie_jarTrueLEX_HTTP__COOKIE_JAREnable in-memory cookie jar
MethodDescription
HTTPModule.configure(...)Configure with explicit HTTPClientConfig
HTTPModule.stub()No-op HTTPModule for unit testing
  • Connection pooling — per-host limits, keepalive, DNS caching via aiohttp
  • Proxy support — HTTP/HTTPS with environment variable auto-detection
  • Cookie jar — optional, per-session in-memory cookie persistence
  • StreamingStreamContext for large file downloads
  • Interceptor chains — composable auth, logging, metrics hooks
  • Type-safe contextsRequestContext and ResponseContext typed models
from lexigram.contracts.web import HTTPClientProtocol
from lexigram.http.types import ResponseContext
class FakeHTTPClient(HTTPClientProtocol):
async def get(self, url: str, **kwargs) -> ResponseContext:
return ResponseContext(status=200, headers={}, body=b'{"id": 123}')
# Inject into service under test
service = UserService(http_client=FakeHTTPClient())
FileWhat it contains
src/lexigram/http/module.pyHTTPModule with factory methods
src/lexigram/http/config.pyHTTPClientConfig and ConnectionPoolConfig
src/lexigram/http/di/provider.pyHTTPProvider — wires HTTP client into DI container
src/lexigram/http/client/HTTPClient and BaseURLHTTPClient
src/lexigram/http/pool/ConnectionPool (aiohttp connector abstraction)
src/lexigram/http/types.pyRequestContext and ResponseContext typed models