Skip to main content

autumn_web/
interceptor.rs

1#[cfg(feature = "oauth2")]
2use std::sync::Arc;
3
4#[cfg(feature = "mail")]
5pub trait MailInterceptor: Send + Sync + 'static {
6    fn intercept<'a>(
7        &'a self,
8        mail: &'a crate::mail::Mail,
9        next: std::pin::Pin<
10            Box<dyn std::future::Future<Output = Result<(), crate::mail::MailError>> + Send + 'a>,
11        >,
12    ) -> std::pin::Pin<
13        Box<dyn std::future::Future<Output = Result<(), crate::mail::MailError>> + Send + 'a>,
14    >;
15}
16
17pub trait JobInterceptor: Send + Sync + 'static {
18    fn intercept_enqueue<'a>(
19        &'a self,
20        name: &'a str,
21        payload: &'a serde_json::Value,
22        next: std::pin::Pin<
23            Box<dyn std::future::Future<Output = crate::AutumnResult<()>> + Send + 'a>,
24        >,
25    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::AutumnResult<()>> + Send + 'a>>;
26
27    fn intercept_execute<'a>(
28        &'a self,
29        name: &'a str,
30        payload: &'a serde_json::Value,
31        next: std::pin::Pin<
32            Box<dyn std::future::Future<Output = crate::AutumnResult<()>> + Send + 'a>,
33        >,
34    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = crate::AutumnResult<()>> + Send + 'a>>;
35}
36
37#[derive(Debug, Clone)]
38pub struct DbCheckoutContext {
39    pub pool_name: String,
40}
41
42#[cfg(feature = "db")]
43pub trait DbConnectionInterceptor: Send + Sync + 'static {
44    fn intercept_checkout<'a>(
45        &'a self,
46        ctx: DbCheckoutContext,
47        next: std::pin::Pin<
48            Box<
49                dyn std::future::Future<
50                        Output = Result<crate::db::PooledConnection, crate::AutumnError>,
51                    > + Send
52                    + 'a,
53            >,
54        >,
55    ) -> std::pin::Pin<
56        Box<
57            dyn std::future::Future<
58                    Output = Result<crate::db::PooledConnection, crate::AutumnError>,
59                > + Send
60                + 'a,
61        >,
62    >;
63
64    /// Returns whether this interceptor enables transactional test isolation mode.
65    fn is_transactional_test(&self) -> bool {
66        false
67    }
68}
69
70#[cfg(feature = "ws")]
71pub trait ChannelsInterceptor: Send + Sync + 'static {
72    /// Intercepts a channel message publication.
73    ///
74    /// # Errors
75    ///
76    /// Returns a [`ChannelPublishError`](crate::channels::ChannelPublishError) if publication fails.
77    fn intercept_publish(
78        &self,
79        topic: &str,
80        msg: &crate::channels::ChannelMessage,
81        next: &dyn Fn(
82            &str,
83            &crate::channels::ChannelMessage,
84        ) -> Result<usize, crate::channels::ChannelPublishError>,
85    ) -> Result<usize, crate::channels::ChannelPublishError>;
86}
87#[cfg(feature = "oauth2")]
88pub type HttpInterceptorFuture<'a> = std::pin::Pin<
89    Box<dyn std::future::Future<Output = Result<reqwest::Response, reqwest::Error>> + Send + 'a>,
90>;
91
92#[cfg(feature = "oauth2")]
93pub trait HttpInterceptor: Send + Sync + 'static {
94    fn intercept<'a>(
95        &'a self,
96        req: reqwest::Request,
97        next: &'a dyn Fn(reqwest::Request) -> HttpInterceptorFuture<'a>,
98    ) -> HttpInterceptorFuture<'a>;
99}
100
101#[cfg(feature = "oauth2")]
102tokio::task_local! {
103    pub static ACTIVE_HTTP_INTERCEPTORS: Vec<Arc<dyn HttpInterceptor>>;
104}