use crate::async_trait;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebhookRequest {
pub method: String,
pub path: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl WebhookRequest {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebhookResponse {
pub status: u16,
pub content_type: &'static str,
pub body: Vec<u8>,
}
impl WebhookResponse {
pub fn json(body: &serde_json::Value) -> Self {
Self {
status: 200,
content_type: "application/json",
body: body.to_string().into_bytes(),
}
}
pub fn status(status: u16) -> Self {
Self {
status,
content_type: "text/plain",
body: Vec::new(),
}
}
}
#[async_trait]
pub trait WebhookHandler: Send + Sync {
fn path(&self) -> String;
async fn handle(&self, request: WebhookRequest) -> WebhookResponse;
}