Request
HTTP request handling and utilities.
Transport-agnostic request system.
This module provides the complete request handling infrastructure: - BaseRequest: Abstract interface for all request types - HttpRequest: ASGI HTTP request adapter - MsgRequest: Message-based request adapter (WSX over WebSocket, NATS) - RequestRegistry: Factory and tracking for active requests
- Architecture:
- BaseRequest (ABC)
├── HttpRequest # ASGI HTTP scope └── MsgRequest # WSX message (WebSocket, NATS)
Every request: 1. Gets a unique id (correlation ID) 2. Is registered in RequestRegistry 3. Has app_name for per-app metrics 4. Has created_at for age tracking 5. Is unregistered on completion
Example
registry = RequestRegistry() request = await registry.create(scope, receive, send) try:
result = await handler(request)
- finally:
registry.unregister(request.id)
- class genro_asgi.request.BaseRequest[source]
Bases:
ABCAbstract base class for transport-agnostic requests.
All request implementations (HTTP, Message-based) must implement this interface, allowing handlers to work uniformly across transports.
- Properties:
id: Server-generated correlation ID (internal) external_id: Client-provided ID for correlation (optional) method: HTTP method (GET, POST, PUT, DELETE, PATCH) path: Request path (e.g., ‘/users/42’) headers: Request headers as dict cookies: Request cookies as dict query: Query parameters data: Request body/payload transport: Transport type (‘http’, ‘websocket’, ‘nats’) app_name: Name of the app handling this request (for metrics) created_at: Timestamp when request was created (for age tracking) tytx_mode: True if request uses TYTX serialization tytx_transport: TYTX transport type (‘json’, ‘msgpack’) or None
- class genro_asgi.request.HttpRequest[source]
Bases:
BaseRequestHTTP request adapter wrapping ASGI scope.
- property headers_obj: Headers
Request headers as Headers object (case-insensitive).
- async init(scope, receive, send=None, **kwargs)[source]
Async initialization - reads body and parses request data.
- Return type:
- property query_params: QueryParams
Query string parameters as QueryParams object.
- property scope: MutableMapping[str, Any]
Raw ASGI scope dict.
- property state: State
Request-scoped state container.
- property url: URL
Full request URL.
- class genro_asgi.request.MsgRequest[source]
Bases:
BaseRequestMessage-based request adapter (WSX over WebSocket, NATS, etc.).
Parses WSX:// formatted messages into BaseRequest interface. Transport-agnostic: works with any message-based protocol.
- async init(scope, receive, send=None, **kwargs)[source]
Async initialization - parses WSX message.
- Return type:
- property scope: MutableMapping[str, Any]
Access to raw ASGI scope.
- class genro_asgi.request.RequestRegistry(factories=None)[source]
Bases:
objectRegistry for creating and tracking active requests.
Responsibilities: - Creates appropriate request based on scope[“type”] using factories dict - Calls async init() on the created request - Tracks active requests for monitoring and metrics - Provides iteration and lookup by request ID
Example
registry = RequestRegistry() request = await registry.create(scope, receive, send) print(f”Active: {len(registry)}”) registry.unregister()
- async create(scope, receive, send=None, **kwargs)[source]
Create and register a request from ASGI scope.
- Return type:
- property current: BaseRequest | None
Current request from ContextVar.
- factories