Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend documentation for RequestChain #1442

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion lychee-lib/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,55 @@ pub enum ChainResult<T, R> {

/// Request chain type
///
/// This takes a request and returns a status.
/// Lychee uses a chain of responsibility pattern to handle requests.
/// Each handler in the chain can modify the request and decide if it should be
/// passed to the next handler or not.
///
/// The chain is implemented as a vector of handlers. It is traversed by calling
/// `traverse` on the [`Chain`], which in turn will call [`Handler::handle`] on
/// each handler in the chain consecutively.
///
/// To add external handlers, you can implement the [`Handler`] trait and add your
/// handler to the chain.
///
/// The entire request chain takes a request as input and returns a status.
///
/// # Example
///
/// ```rust
/// use async_trait::async_trait;
/// use lychee_lib::{chain::RequestChain, ChainResult, ClientBuilder, Handler, Result, Status};
/// use reqwest::{Method, Request, Url};
///
/// // Define your own custom handler
/// #[derive(Debug)]
/// struct DummyHandler {}
///
/// #[async_trait]
/// impl Handler<Request, Status> for DummyHandler {
/// async fn handle(&mut self, mut request: Request) -> ChainResult<Request, Status> {
/// // Modify the request here
/// // After that, continue to the next handler
/// ChainResult::Next(request)
/// }
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// // Build a custom request chain with our dummy handler
/// let chain = RequestChain::new(vec![Box::new(DummyHandler {})]);
///
/// let client = ClientBuilder::builder()
/// .plugin_request_chain(chain)
/// .build()
/// .client()?;
///
/// let result = client.check("https://wikipedia.org").await;
/// println!("{:?}", result);
///
/// Ok(())
/// }
/// ```
pub type RequestChain = Chain<reqwest::Request, Status>;

/// Inner chain type.
Expand Down
Loading