use async_trait::async_trait;
use serde_json::Value;
use sqlx::{Postgres, Transaction};
use super::{Result, signature::SignatureError};
#[async_trait]
pub trait SignatureVerifier: Send + Sync {
fn name(&self) -> &'static str;
fn signature_header(&self) -> &'static str;
fn verify(
&self,
payload: &[u8],
signature: &str,
secret: &str,
timestamp: Option<&str>,
) -> std::result::Result<bool, SignatureError>;
fn extract_timestamp(&self, _signature: &str) -> Option<i64> {
None
}
}
#[async_trait]
pub trait IdempotencyStore: Send + Sync {
async fn check(&self, provider: &str, event_id: &str) -> Result<bool>;
async fn record(
&self,
provider: &str,
event_id: &str,
event_type: &str,
status: &str,
) -> Result<uuid::Uuid>;
async fn update_status(
&self,
provider: &str,
event_id: &str,
status: &str,
error: Option<&str>,
) -> Result<()>;
}
#[async_trait]
pub trait SecretProvider: Send + Sync {
async fn get_secret(&self, name: &str) -> Result<String>;
}
#[async_trait]
pub trait EventHandler: Send + Sync {
async fn handle(
&self,
function_name: &str,
params: Value,
tx: &mut Transaction<'_, Postgres>,
) -> Result<Value>;
}
pub trait Clock: Send + Sync {
fn now(&self) -> i64;
}
pub struct SystemClock;
impl Clock for SystemClock {
fn now(&self) -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64
}
}