Skip to main content

spider_lib/
middleware.rs

1use async_trait::async_trait;
2use std::any::Any;
3use std::time::Duration;
4
5use crate::error::SpiderError;
6use crate::request::Request;
7use crate::response::Response;
8
9#[allow(clippy::large_enum_variant)]
10/// Enum returned by middleware methods to control further processing.
11pub enum MiddlewareAction<T> {
12    /// Continue processing with the provided item.
13    Continue(T),
14    /// Retry the Request after the specified duration. (Only valid for Response processing)
15    Retry(Box<Request>, Duration),
16    /// Drop the item, stopping further processing.
17    Drop,
18    /// Return a Response directly, bypassing the downloader. (Only valid for Request processing)
19    ReturnResponse(Response),
20}
21
22/// A trait for processing requests and responses.
23#[async_trait]
24pub trait Middleware<C: Send + Sync>: Any + Send + Sync + 'static {
25    fn name(&self) -> &str;
26
27    async fn process_request(
28        &mut self,
29        _client: &C,
30        request: Request,
31    ) -> Result<MiddlewareAction<Request>, SpiderError> {
32        Ok(MiddlewareAction::Continue(request))
33    }
34    async fn process_response(
35        &mut self,
36        response: Response,
37    ) -> Result<MiddlewareAction<Response>, SpiderError> {
38        Ok(MiddlewareAction::Continue(response))
39    }
40}