WebRequest class
Represents a unified abstraction over an HTTP request–response interaction.
The WebRequest class serves as a container that binds a ServerHttpRequest and its corresponding ServerHttpResponse, allowing filters, interceptors, and controllers to operate on the same logical web exchange.
Purpose
- Simplifies access to both request and response within the same object.
- Provides lifecycle timing utilities (request start, completion, duration).
- Enables reflective access via the static CLASS constant for runtime type inspection and dependency resolution.
Reflection Support
The static CLASS field exposes the runtime Class metadata used by the
JetLeaf reflection system. This allows the framework to dynamically detect,
resolve, or inject instances of WebRequest in annotated controller methods.
Example
final webRequest = WebRequest(req, res);
log.info('Started at: ${webRequest.getRequestedAt()}');
await controller.handle(webRequest.getRequest(), webRequest.getResponse());
final duration = webRequest.getDuration();
if (duration != null) {
log.info('Request completed in ${duration.inMilliseconds} ms');
}
extension TenantAwareWebRequest on WebRequest {
/// Returns the tenant ID if the underlying [ServerHttpRequest]
/// is a [CustomHttpRequest].
///
/// If the request type does not match, returns `null`.
String? getTenantId() {
final req = getRequest();
if (req is CustomHttpRequest) {
return req.tenantId;
}
return null;
}
/// Convenience method to check whether this request belongs
/// to a particular tenant.
bool isTenant(String tenantId) => getTenantId() == tenantId;
}
Notes
- Subclasses of ServerHttpRequest (e.g., IoRequest) may provide their own timing metadata for performance tracing.
- If either timestamp is unavailable, getDuration will return
null. - Equality checks are based on class identity rather than field values.
Constructors
- WebRequest(ServerHttpRequest _httpRequest, ServerHttpResponse _httpResponse)
- Creates a new WebRequest binding the given ServerHttpRequest and ServerHttpResponse instances into a single exchange context.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
equalizedProperties(
) → List< Object?> - Provides the list of properties used in equality and hash code comparisons.
-
getCompletedAt(
) → DateTime? - Returns the timestamp when the request–response exchange completed.
-
getDuration(
) → Duration? - Computes the total duration of the request lifecycle.
-
getRequest(
) → ServerHttpRequest - Returns the underlying ServerHttpRequest for this web exchange.
-
getRequestedAt(
) → DateTime? - Returns the timestamp at which the request was originally received.
-
getResponse(
) → ServerHttpResponse - Returns the ServerHttpResponse associated with this web exchange.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- CLASS → Class
-
Represents the WebRequest type for reflection purposes.
final