pub trait HandlerParamwhere
    Self: Sized,
{ type Error: Into<ApiError> + 'static; fn handler_param<'life0, 'async_trait>(
        req: &'life0 Request<()>
    ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send + 'async_trait>>
    where
        Self: 'async_trait,
        'life0: 'async_trait
; }
Expand description

Implement this for anything that you want to be able to pass into a request handler that doesn’t want to consume the body of the request. This is useful for implementing request guards which prevent the handler from being called unless specific conditions are met (for instance a user is logged in).

Example

// This represents a valid user of the API.
pub struct User { pub id: String }

// Make it possible to ask for the current user in a request:
#[seamless::async_trait]
impl HandlerParam for User {
    type Error = ApiError;
    async fn handler_param(req: &Request<()>) -> Result<Self,Self::Error> {
        // We can put things (like DB connections) into requests before they
        // are handed to the API, and then pluck them out here to use:
        let state = req.extensions()
            .get::<State>()
            .map(|s| s.clone())
            .unwrap();
        state.get_user(req).await
    }
}

Required Associated Types§

An error indicating what went wrong in the event that we fail to extract our parameter from the provided request.

To be usable with crate::api::Api, the error should implement Into<ApiError> (the crate::ApiError macro can be used to gelp with this).

It can be simpler just to set this to ApiError directly.

Required Methods§

Given a [http::Request<()>], return a value of type T back, or else return an error of type E describing what went wrong. Any errors here will lead to the route bailing out and the handler not being run.

Implementations on Foreign Types§

Implementors§