use crate::common::model::data::DataEvent;
use crate::common::model::workflow_profile::TaskProfileSnapshot;
use crate::common::model::{Request, Response};
use crate::errors::Result;
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;
pub type DownloadMiddlewareHandle = Arc<Mutex<Box<dyn DownloadMiddleware>>>;
pub type DataMiddlewareHandle = Arc<Mutex<Box<dyn DataMiddleware>>>;
pub type DataStoreMiddlewareHandle = Arc<Mutex<Box<dyn DataStoreMiddleware>>>;
#[async_trait]
pub trait DownloadMiddleware: Send + Sync {
fn name(&self) -> String;
fn weight(&self) -> u32 {
0
}
async fn before_request(
&mut self,
request: Request,
_profile: &TaskProfileSnapshot,
) -> Option<Request> {
Some(request)
}
async fn after_response(
&mut self,
response: Response,
_profile: &TaskProfileSnapshot,
) -> Option<Response> {
Some(response)
}
fn default_arc() -> DownloadMiddlewareHandle
where
Self: Sized;
}
#[async_trait]
pub trait DataMiddleware: Send + Sync {
fn name(&self) -> String;
fn weight(&self) -> u32 {
0
}
async fn handle_data(
&mut self,
data: DataEvent,
_profile: &TaskProfileSnapshot,
) -> Option<DataEvent> {
Some(data)
}
fn default_arc() -> DataMiddlewareHandle
where
Self: Sized;
}
#[async_trait]
pub trait DataStoreMiddleware: DataMiddleware {
async fn before_store(&mut self, _profile: &TaskProfileSnapshot) -> Result<()> {
Ok(())
}
async fn store_data(&mut self, data: DataEvent, profile: &TaskProfileSnapshot) -> Result<()>;
async fn after_store(&mut self, _profile: &TaskProfileSnapshot) -> Result<()> {
Ok(())
}
fn default_arc() -> DataStoreMiddlewareHandle
where
Self: Sized;
}