fraiseql_server/webhooks/
traits.rs1use async_trait::async_trait;
6use serde_json::Value;
7use sqlx::{Postgres, Transaction};
8
9use super::{Result, signature::SignatureError};
10
11#[async_trait]
13pub trait SignatureVerifier: Send + Sync {
14 fn name(&self) -> &'static str;
16
17 fn signature_header(&self) -> &'static str;
19
20 fn verify(
33 &self,
34 payload: &[u8],
35 signature: &str,
36 secret: &str,
37 timestamp: Option<&str>,
38 ) -> std::result::Result<bool, SignatureError>;
39
40 fn extract_timestamp(&self, _signature: &str) -> Option<i64> {
42 None
43 }
44}
45
46#[async_trait]
48pub trait IdempotencyStore: Send + Sync {
49 async fn check(&self, provider: &str, event_id: &str) -> Result<bool>;
51
52 async fn record(
54 &self,
55 provider: &str,
56 event_id: &str,
57 event_type: &str,
58 status: &str,
59 ) -> Result<uuid::Uuid>;
60
61 async fn update_status(
63 &self,
64 provider: &str,
65 event_id: &str,
66 status: &str,
67 error: Option<&str>,
68 ) -> Result<()>;
69}
70
71#[async_trait]
73pub trait SecretProvider: Send + Sync {
74 async fn get_secret(&self, name: &str) -> Result<String>;
76}
77
78#[async_trait]
80pub trait EventHandler: Send + Sync {
81 async fn handle(
83 &self,
84 function_name: &str,
85 params: Value,
86 tx: &mut Transaction<'_, Postgres>,
87 ) -> Result<Value>;
88}
89
90pub trait Clock: Send + Sync {
92 fn now(&self) -> i64;
94}
95
96pub struct SystemClock;
98
99impl Clock for SystemClock {
100 fn now(&self) -> i64 {
101 std::time::SystemTime::now()
102 .duration_since(std::time::UNIX_EPOCH)
103 .unwrap()
104 .as_secs() as i64
105 }
106}