Handler

Trait Handler 

Source
pub trait Handler<T, R>: Debug {
    // Required method
    fn handle<'life0, 'async_trait>(
        &'life0 mut self,
        input: T,
    ) -> Pin<Box<dyn Future<Output = ChainResult<T, R>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Handler trait for implementing request handlers

This trait needs to be implemented by all chainable handlers. It is the only requirement to handle requests in lychee.

It takes an input request and returns a ChainResult, which can be either ChainResult::Next to continue to the next handler or ChainResult::Done to stop the chain.

The request can be modified by the handler before it is passed to the next handler. This allows for modifying the request, such as adding headers or changing the URL (e.g. for remapping or filtering).

Required Methods§

Source

fn handle<'life0, 'async_trait>( &'life0 mut self, input: T, ) -> Pin<Box<dyn Future<Output = ChainResult<T, R>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Given an input request, return a ChainResult to continue or stop the chain.

The input request can be modified by the handler before it is passed to the next handler.

§Example
use lychee_lib::{Handler, ChainResult, Status};
use reqwest::Request;
use async_trait::async_trait;

#[derive(Debug)]
struct AddHeader;

#[async_trait]
impl Handler<Request, Status> for AddHeader {
   async fn handle(&mut self, mut request: Request) -> ChainResult<Request, Status> {
     // You can modify the request however you like here
     request.headers_mut().append("X-Header", "value".parse().unwrap());

     // Pass the request to the next handler
     ChainResult::Next(request)
  }
}

Implementations on Foreign Types§

Source§

impl<Response> Handler<Request, Response> for Option<BasicAuthCredentials>

Source§

fn handle<'life0, 'async_trait>( &'life0 mut self, request: Request, ) -> Pin<Box<dyn Future<Output = ChainResult<Request, Response>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§