pub trait RequestContext {
type Method: AsRef<str>;
type Uri;
Show 47 methods
// Required methods
fn request_method(&self) -> &Self::Method;
fn original_uri(&self) -> &Self::Uri;
fn matched_route(&self) -> Cow<'_, str>;
fn request_path(&self) -> &str;
fn get_query_string(&self) -> Option<&str>;
fn get_header(&self, name: &str) -> Option<&str>;
fn client_ip(&self) -> Option<IpAddr>;
fn get_context(&self) -> Option<Context>;
fn get_data<T: Clone + Send + Sync + 'static>(&self) -> Option<T>;
fn set_data<T: Clone + Send + Sync + 'static>(
&mut self,
value: T,
) -> Option<T>;
async fn read_body_bytes(&mut self) -> Result<Vec<u8>, Error>;
// Provided methods
fn path_segments(&self) -> Vec<&str> { ... }
fn new_context(&self) -> Context { ... }
fn get_trace_context(&self) -> Option<TraceContext> { ... }
fn new_trace_context(&self) -> TraceContext { ... }
fn new_cookie(
&self,
name: SharedString,
value: SharedString,
max_age: Option<Duration>,
) -> Cookie<'static> { ... }
fn get_cookie(&self, name: &str) -> Option<Cookie<'_>> { ... }
fn start_time(&self) -> Instant { ... }
fn instance(&self) -> String { ... }
fn request_id(&self) -> Uuid { ... }
fn trace_id(&self) -> Uuid { ... }
fn session_id(&self) -> Option<String> { ... }
fn locale(&self) -> Option<LanguageIdentifier> { ... }
fn data_type(&self) -> Option<&str> { ... }
fn get_param(&self, name: &str) -> Option<&str> { ... }
fn decode_param(&self, name: &str) -> Result<Cow<'_, str>, Rejection> { ... }
fn parse_param<T: FromStr<Err: Into<Error>>>(
&self,
name: &str,
) -> Result<T, Rejection> { ... }
fn get_query(&self, name: &str) -> Option<&str> { ... }
fn decode_query(&self, name: &str) -> Result<Cow<'_, str>, Rejection> { ... }
fn parse_query<T: Default + DeserializeOwned>(&self) -> Result<T, Rejection> { ... }
async fn parse_body<T: DeserializeOwned>(&mut self) -> Result<T, Rejection> { ... }
async fn parse_multipart(&mut self) -> Result<Multipart<'_>, Rejection> { ... }
async fn parse_file(&mut self) -> Result<NamedFile, Rejection> { ... }
async fn parse_files(&mut self) -> Result<Vec<NamedFile>, Rejection> { ... }
async fn parse_form_data<T: DeserializeOwned>(
&mut self,
) -> Result<(T, Vec<NamedFile>), Rejection> { ... }
fn parse_authentication(&self) -> Result<Authentication, Rejection> { ... }
fn parse_access_key_id(&self) -> Result<AccessKeyId, Rejection> { ... }
fn parse_security_token(
&self,
key: &[u8],
) -> Result<SecurityToken, Rejection> { ... }
fn parse_session_id(&self) -> Result<SessionId, Rejection> { ... }
fn parse_jwt_claims<T, K>(&self, key: &K) -> Result<JwtClaims<T>, Rejection>
where T: Default + Serialize + DeserializeOwned,
K: MACLike { ... }
fn query_validation<S>(
&self,
query: &mut Query,
) -> Result<Response<S>, Rejection>
where Self: Sized,
S: ResponseCode { ... }
async fn model_validation<M, S>(
&mut self,
model: &mut M,
) -> Result<Response<S>, Rejection>
where Self: Sized,
M: ModelHooks,
S: ResponseCode { ... }
async fn fetch(
&self,
url: &str,
options: Option<&Map>,
) -> Result<Response, Error> { ... }
async fn fetch_json<T: DeserializeOwned>(
&self,
url: &str,
options: Option<&Map>,
) -> Result<T, Error> { ... }
fn translate(
&self,
message: &str,
args: Option<FluentArgs<'_>>,
) -> Result<SharedString, Error> { ... }
fn subscription(&self) -> Subscription { ... }
fn cloud_event(
&self,
event_type: SharedString,
data: JsonValue,
) -> CloudEvent { ... }
}
Expand description
Request context.
Required Associated Types§
Required Methods§
Sourcefn request_method(&self) -> &Self::Method
fn request_method(&self) -> &Self::Method
Returns the request method.
Sourcefn original_uri(&self) -> &Self::Uri
fn original_uri(&self) -> &Self::Uri
Returns the original request URI regardless of nesting.
Sourcefn matched_route(&self) -> Cow<'_, str>
fn matched_route(&self) -> Cow<'_, str>
Returns the route that matches the request.
Sourcefn request_path(&self) -> &str
fn request_path(&self) -> &str
Returns the request path regardless of nesting.
Sourcefn get_query_string(&self) -> Option<&str>
fn get_query_string(&self) -> Option<&str>
Gets the query string of the request.
Sourcefn get_header(&self, name: &str) -> Option<&str>
fn get_header(&self, name: &str) -> Option<&str>
Gets an HTTP header value with the given name.
Sourcefn get_context(&self) -> Option<Context>
fn get_context(&self) -> Option<Context>
Gets the request context.
Sourcefn get_data<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
fn get_data<T: Clone + Send + Sync + 'static>(&self) -> Option<T>
Gets the request scoped data.
Provided Methods§
Sourcefn path_segments(&self) -> Vec<&str>
fn path_segments(&self) -> Vec<&str>
Returns the request path segments.
Sourcefn new_context(&self) -> Context
fn new_context(&self) -> Context
Creates a new request context.
Sourcefn get_trace_context(&self) -> Option<TraceContext>
fn get_trace_context(&self) -> Option<TraceContext>
Returns the trace context by parsing the traceparent
and tracestate
header values.
Sourcefn new_trace_context(&self) -> TraceContext
fn new_trace_context(&self) -> TraceContext
Creates a new TraceContext
.
Available on crate feature cookie
only.
cookie
only.Creates a new cookie with the given name and value.
Available on crate feature cookie
only.
cookie
only.Gets a cookie with the given name.
Sourcefn start_time(&self) -> Instant
fn start_time(&self) -> Instant
Returns the start time.
Sourcefn request_id(&self) -> Uuid
fn request_id(&self) -> Uuid
Returns the request ID.
Sourcefn session_id(&self) -> Option<String>
fn session_id(&self) -> Option<String>
Returns the session ID.
Sourcefn locale(&self) -> Option<LanguageIdentifier>
Available on crate feature i18n
only.
fn locale(&self) -> Option<LanguageIdentifier>
i18n
only.Returns the locale.
Sourcefn data_type(&self) -> Option<&str>
fn data_type(&self) -> Option<&str>
Gets the data type by parsing the content-type
header.
§Note
Currently, we support the following values: bytes
| csv
| form
| json
| multipart
| ndjson
| text
.
Sourcefn get_param(&self, name: &str) -> Option<&str>
fn get_param(&self, name: &str) -> Option<&str>
Gets the route parameter by name.
The name should not include :
, *
, {
or }
.
§Note
Please note that it does not handle the percent-decoding.
You can use decode_param()
or parse_param()
if you need percent-decoding.
Sourcefn decode_param(&self, name: &str) -> Result<Cow<'_, str>, Rejection>
fn decode_param(&self, name: &str) -> Result<Cow<'_, str>, Rejection>
Decodes the UTF-8 percent-encoded route parameter by name.
Sourcefn parse_param<T: FromStr<Err: Into<Error>>>(
&self,
name: &str,
) -> Result<T, Rejection>
fn parse_param<T: FromStr<Err: Into<Error>>>( &self, name: &str, ) -> Result<T, Rejection>
Parses the route parameter by name as an instance of type T
.
The name should not include :
, *
, {
or }
.
Sourcefn get_query(&self, name: &str) -> Option<&str>
fn get_query(&self, name: &str) -> Option<&str>
Gets the query value of the URI by name.
§Note
Please note that it does not handle the percent-decoding.
You can use decode_query()
or parse_query()
if you need percent-decoding.
Sourcefn decode_query(&self, name: &str) -> Result<Cow<'_, str>, Rejection>
fn decode_query(&self, name: &str) -> Result<Cow<'_, str>, Rejection>
Decodes the UTF-8 percent-encoded query value of the URI by name.
Sourcefn parse_query<T: Default + DeserializeOwned>(&self) -> Result<T, Rejection>
fn parse_query<T: Default + DeserializeOwned>(&self) -> Result<T, Rejection>
Parses the query as an instance of type T
.
Returns a default value of T
when the query is empty.
If the query has a timestamp
parameter, it will be used to prevent replay attacks.
Sourceasync fn parse_body<T: DeserializeOwned>(&mut self) -> Result<T, Rejection>
async fn parse_body<T: DeserializeOwned>(&mut self) -> Result<T, Rejection>
Parses the request body as an instance of type T
.
§Note
Currently, we have built-in support for the following content-type
header values:
application/json
application/problem+json
application/x-www-form-urlencoded
Sourceasync fn parse_multipart(&mut self) -> Result<Multipart<'_>, Rejection>
async fn parse_multipart(&mut self) -> Result<Multipart<'_>, Rejection>
Parses the request body as a multipart, which is commonly used with file uploads.
Sourceasync fn parse_file(&mut self) -> Result<NamedFile, Rejection>
async fn parse_file(&mut self) -> Result<NamedFile, Rejection>
Parses the request body as a file.
Sourceasync fn parse_files(&mut self) -> Result<Vec<NamedFile>, Rejection>
async fn parse_files(&mut self) -> Result<Vec<NamedFile>, Rejection>
Parses the request body as a list of files.
Sourceasync fn parse_form_data<T: DeserializeOwned>(
&mut self,
) -> Result<(T, Vec<NamedFile>), Rejection>
async fn parse_form_data<T: DeserializeOwned>( &mut self, ) -> Result<(T, Vec<NamedFile>), Rejection>
Parses the multipart/form-data
as an instance of type T
and a list of files.
Sourcefn parse_authentication(&self) -> Result<Authentication, Rejection>
Available on crate feature auth
only.
fn parse_authentication(&self) -> Result<Authentication, Rejection>
auth
only.Attempts to construct an instance of Authentication
from an HTTP request.
The value is extracted from the query or the authorization
header.
By default, the Accept
header value is ignored and
the canonicalized resource is set to the request path.
Sourcefn parse_access_key_id(&self) -> Result<AccessKeyId, Rejection>
Available on crate feature auth
only.
fn parse_access_key_id(&self) -> Result<AccessKeyId, Rejection>
auth
only.Attempts to construct an instance of AccessKeyId
from an HTTP request.
The value is extracted from the query parameter access_key_id
or the authorization
header.
Sourcefn parse_security_token(&self, key: &[u8]) -> Result<SecurityToken, Rejection>
Available on crate feature auth
only.
fn parse_security_token(&self, key: &[u8]) -> Result<SecurityToken, Rejection>
auth
only.Attempts to construct an instance of SecurityToken
from an HTTP request.
The value is extracted from the x-security-token
header.
Sourcefn parse_session_id(&self) -> Result<SessionId, Rejection>
Available on crate feature auth
only.
fn parse_session_id(&self) -> Result<SessionId, Rejection>
auth
only.Attempts to construct an instance of SessionId
from an HTTP request.
The value is extracted from the x-session-id
or session-id
header.
Sourcefn parse_jwt_claims<T, K>(&self, key: &K) -> Result<JwtClaims<T>, Rejection>
Available on crate feature jwt
only.
fn parse_jwt_claims<T, K>(&self, key: &K) -> Result<JwtClaims<T>, Rejection>
jwt
only.Attempts to construct an instance of JwtClaims
from an HTTP request.
The value is extracted from the query parameter access_token
or
the authorization
header.
Sourcefn query_validation<S>(
&self,
query: &mut Query,
) -> Result<Response<S>, Rejection>where
Self: Sized,
S: ResponseCode,
fn query_validation<S>(
&self,
query: &mut Query,
) -> Result<Response<S>, Rejection>where
Self: Sized,
S: ResponseCode,
Returns a Response
or Rejection
from a model query validation.
The data is extracted from parse_query()
.
Sourceasync fn model_validation<M, S>(
&mut self,
model: &mut M,
) -> Result<Response<S>, Rejection>
async fn model_validation<M, S>( &mut self, model: &mut M, ) -> Result<Response<S>, Rejection>
Returns a Response
or Rejection
from a model validation.
The data is extracted from parse_body()
.
Sourceasync fn fetch(
&self,
url: &str,
options: Option<&Map>,
) -> Result<Response, Error>
async fn fetch( &self, url: &str, options: Option<&Map>, ) -> Result<Response, Error>
Makes an HTTP request to the provided URL.
Sourceasync fn fetch_json<T: DeserializeOwned>(
&self,
url: &str,
options: Option<&Map>,
) -> Result<T, Error>
async fn fetch_json<T: DeserializeOwned>( &self, url: &str, options: Option<&Map>, ) -> Result<T, Error>
Makes an HTTP request to the provided URL and deserializes the response body via JSON.
Sourcefn translate(
&self,
message: &str,
args: Option<FluentArgs<'_>>,
) -> Result<SharedString, Error>
Available on crate feature i18n
only.
fn translate( &self, message: &str, args: Option<FluentArgs<'_>>, ) -> Result<SharedString, Error>
i18n
only.Translates the localization message.
Sourcefn subscription(&self) -> Subscription
fn subscription(&self) -> Subscription
Constructs a new subscription instance.
Sourcefn cloud_event(&self, event_type: SharedString, data: JsonValue) -> CloudEvent
fn cloud_event(&self, event_type: SharedString, data: JsonValue) -> CloudEvent
Constructs a new cloud event instance.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.