zino_http::request

Trait RequestContext

Source
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§

Source

type Method: AsRef<str>

The method type.

Source

type Uri

The uri type.

Required Methods§

Source

fn request_method(&self) -> &Self::Method

Returns the request method.

Source

fn original_uri(&self) -> &Self::Uri

Returns the original request URI regardless of nesting.

Source

fn matched_route(&self) -> Cow<'_, str>

Returns the route that matches the request.

Source

fn request_path(&self) -> &str

Returns the request path regardless of nesting.

Source

fn get_query_string(&self) -> Option<&str>

Gets the query string of the request.

Source

fn get_header(&self, name: &str) -> Option<&str>

Gets an HTTP header value with the given name.

Source

fn client_ip(&self) -> Option<IpAddr>

Returns the client’s remote IP.

Source

fn get_context(&self) -> Option<Context>

Gets the request context.

Source

fn get_data<T: Clone + Send + Sync + 'static>(&self) -> Option<T>

Gets the request scoped data.

Source

fn set_data<T: Clone + Send + Sync + 'static>(&mut self, value: T) -> Option<T>

Sets the request scoped data and returns the old value if an item of this type was already stored.

Source

async fn read_body_bytes(&mut self) -> Result<Vec<u8>, Error>

Reads the entire request body into a byte buffer.

Provided Methods§

Source

fn path_segments(&self) -> Vec<&str>

Returns the request path segments.

Source

fn new_context(&self) -> Context

Creates a new request context.

Source

fn get_trace_context(&self) -> Option<TraceContext>

Returns the trace context by parsing the traceparent and tracestate header values.

Source

fn new_trace_context(&self) -> TraceContext

Creates a new TraceContext.

Available on crate feature cookie only.

Creates a new cookie with the given name and value.

Available on crate feature cookie only.

Gets a cookie with the given name.

Source

fn start_time(&self) -> Instant

Returns the start time.

Source

fn instance(&self) -> String

Returns the instance.

Source

fn request_id(&self) -> Uuid

Returns the request ID.

Source

fn trace_id(&self) -> Uuid

Returns the trace ID.

Source

fn session_id(&self) -> Option<String>

Returns the session ID.

Source

fn locale(&self) -> Option<LanguageIdentifier>

Available on crate feature i18n only.

Returns the locale.

Source

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.

Source

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.

Source

fn decode_param(&self, name: &str) -> Result<Cow<'_, str>, Rejection>

Decodes the UTF-8 percent-encoded route parameter by name.

Source

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 }.

Source

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.

Source

fn decode_query(&self, name: &str) -> Result<Cow<'_, str>, Rejection>

Decodes the UTF-8 percent-encoded query value of the URI by name.

Source

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.

Source

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
Source

async fn parse_multipart(&mut self) -> Result<Multipart<'_>, Rejection>

Parses the request body as a multipart, which is commonly used with file uploads.

Source

async fn parse_file(&mut self) -> Result<NamedFile, Rejection>

Parses the request body as a file.

Source

async fn parse_files(&mut self) -> Result<Vec<NamedFile>, Rejection>

Parses the request body as a list of files.

Source

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.

Source

fn parse_authentication(&self) -> Result<Authentication, Rejection>

Available on crate feature 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.

Source

fn parse_access_key_id(&self) -> Result<AccessKeyId, Rejection>

Available on crate feature 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.

Source

fn parse_security_token(&self, key: &[u8]) -> Result<SecurityToken, Rejection>

Available on crate feature auth only.

Attempts to construct an instance of SecurityToken from an HTTP request. The value is extracted from the x-security-token header.

Source

fn parse_session_id(&self) -> Result<SessionId, Rejection>

Available on crate feature 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.

Source

fn parse_jwt_claims<T, K>(&self, key: &K) -> Result<JwtClaims<T>, Rejection>

Available on crate feature 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.

Source

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().

Source

async fn model_validation<M, S>( &mut self, model: &mut M, ) -> Result<Response<S>, Rejection>
where Self: Sized, M: ModelHooks, S: ResponseCode,

Returns a Response or Rejection from a model validation. The data is extracted from parse_body().

Source

async fn fetch( &self, url: &str, options: Option<&Map>, ) -> Result<Response, Error>

Makes an HTTP request to the provided URL.

Source

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.

Source

fn translate( &self, message: &str, args: Option<FluentArgs<'_>>, ) -> Result<SharedString, Error>

Available on crate feature i18n only.

Translates the localization message.

Source

fn subscription(&self) -> Subscription

Constructs a new subscription instance.

Source

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.

Implementors§