Skip to main content

ic_bn_lib_common/traits/
http.rs

1use std::{
2    fmt::{self, Debug},
3    hash::Hash,
4};
5
6use async_trait::async_trait;
7use http::Request;
8
9use crate::types::http::{CacheError, Error};
10
11/// Generic HTTP client trait that is using Reqwest types
12#[cfg_attr(test, mockall::automock)]
13#[async_trait]
14pub trait Client: Send + Sync + fmt::Debug {
15    async fn execute(&self, req: reqwest::Request) -> Result<reqwest::Response, reqwest::Error>;
16}
17
18/// Generic HTTP client trait that is using `http` crate types
19#[async_trait]
20pub trait ClientHttp<B1, B2 = axum::body::Body>: Send + Sync + fmt::Debug {
21    async fn execute(&self, req: http::Request<B1>) -> Result<http::Response<B2>, Error>;
22}
23
24/// Trait to extract the caching key from the given HTTP request
25pub trait KeyExtractor: Clone + Send + Sync + Debug + 'static {
26    /// The type of the key.
27    type Key: Clone + Send + Sync + Debug + Hash + Eq + 'static;
28
29    /// Extraction method, will return [`Error`] response when the extraction failed
30    fn extract<T>(&self, req: &Request<T>) -> Result<Self::Key, CacheError>;
31}
32
33/// Reason for bypassing caching of the request
34pub trait CustomBypassReason:
35    Debug + Clone + std::fmt::Display + Into<&'static str> + PartialEq + Eq + Send + Sync + 'static
36{
37}
38
39/// Trait to decide if we need to bypass caching of the given request
40pub trait Bypasser: Clone + Send + Sync + Debug + 'static {
41    /// Custom bypass reason
42    type BypassReason: CustomBypassReason;
43
44    /// Checks if we should bypass the given request
45    fn bypass<T>(&self, req: &Request<T>) -> Result<Option<Self::BypassReason>, CacheError>;
46}