pub struct RequestContext { /* private fields */ }Expand description
Request/correlation context attached to request extensions.
RequestContext is available to handlers when the router uses
crate::middleware::validated_request_id_layer plus
crate::middleware::request_context_layer, or when it is wrapped by
crate::middleware::ApiDefaults::production. Extracting it without those
extensions rejects the request with 500 Internal Server Error.
Fields are inferred from request headers and Axum extensions:
request_id: the final validated/generatedx-request-idcorrelation_id:x-correlation-id, falling back to the request IDtrace_id: the trace-id segment fromtraceparentclient_kind:x-api-keymeans API key, otherwiseAuthorizationmeans authenticated, otherwise anonymousroute: Axum’saxum::extract::MatchedPathwhen it is available at the point the context layer runs
use nidus_http::{Json, context::RequestContext};
async fn handler(context: RequestContext) -> Json<serde_json::Value> {
Json(serde_json::json!({
"requestId": context.request_id(),
"correlationId": context.correlation_id(),
}))
}Implementations§
Source§impl RequestContext
impl RequestContext
Sourcepub fn new(
request_id: impl Into<String>,
method: Method,
path: impl Into<String>,
) -> Self
pub fn new( request_id: impl Into<String>, method: Method, path: impl Into<String>, ) -> Self
Creates a context for the current request boundary.
This constructor is useful in tests or custom middleware. It does not
inspect headers, so optional correlation, trace, route, and client fields
remain empty/default until set explicitly or built via Self::from_parts.
Sourcepub fn from_parts(parts: &Parts, request_id: impl Into<String>) -> Self
pub fn from_parts(parts: &Parts, request_id: impl Into<String>) -> Self
Creates a context from request parts.
This reads x-correlation-id, traceparent, x-api-key,
Authorization, and axum::extract::MatchedPath from the request
boundary. The supplied request_id is expected to be the final ID chosen
by request ID middleware.
Sourcepub fn request_id(&self) -> &str
pub fn request_id(&self) -> &str
Returns the final request id.
With crate::middleware::validated_request_id_layer, this is either a
valid inbound UUID v4 or a generated ID.
Sourcepub fn correlation_id(&self) -> Option<&str>
pub fn correlation_id(&self) -> Option<&str>
Returns the correlation id when available.
Self::from_parts prefers x-correlation-id and falls back to the
request ID when no correlation header is present.
Sourcepub fn route(&self) -> Option<&str>
pub fn route(&self) -> Option<&str>
Returns the stable matched route pattern when available.
This depends on Axum’s axum::extract::MatchedPath extension being
present before the context is built. Layer placement can affect whether
this is available for a given router shape.
Sourcepub fn trace_id(&self) -> Option<&str>
pub fn trace_id(&self) -> Option<&str>
Returns the trace id when available.
The value is extracted from the second segment of the W3C traceparent
header. Use [crate::otel::extract_trace_context] when the otel
feature is enabled and you need full trace/span validation.
Sourcepub const fn client_kind(&self) -> ClientKind
pub const fn client_kind(&self) -> ClientKind
Returns the inferred client kind.
x-api-key takes precedence over Authorization; otherwise the request
is classified as anonymous.
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Returns the optional application session id.
Sourcepub fn with_route(self, route: impl Into<String>) -> Self
pub fn with_route(self, route: impl Into<String>) -> Self
Sets the stable matched route pattern.
Sourcepub fn with_user_id(self, user_id: impl Into<String>) -> Self
pub fn with_user_id(self, user_id: impl Into<String>) -> Self
Sets an application user id.
Sourcepub fn with_tenant_id(self, tenant_id: impl Into<String>) -> Self
pub fn with_tenant_id(self, tenant_id: impl Into<String>) -> Self
Sets an application tenant id.
Sourcepub fn with_session_id(self, session_id: impl Into<String>) -> Self
pub fn with_session_id(self, session_id: impl Into<String>) -> Self
Sets an application session id.
Trait Implementations§
Source§impl Clone for RequestContext
impl Clone for RequestContext
Source§fn clone(&self) -> RequestContext
fn clone(&self) -> RequestContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RequestContext
impl Debug for RequestContext
impl Eq for RequestContext
Source§impl<S> FromRequestParts<S> for RequestContext
impl<S> FromRequestParts<S> for RequestContext
Source§type Rejection = StatusCode
type Rejection = StatusCode
Source§impl PartialEq for RequestContext
impl PartialEq for RequestContext
Source§fn eq(&self, other: &RequestContext) -> bool
fn eq(&self, other: &RequestContext) -> bool
self and other values to be equal, and is used by ==.