use reqwest::Client;
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct WebhookEvent {
#[serde(rename = "type")]
pub event_type: String,
pub hash: String,
pub at: i64,
pub ip: String,
}
#[derive(Clone)]
pub struct WebhookSender {
client: Client,
}
impl WebhookSender {
pub fn new() -> Self {
Self {
client: Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.unwrap_or_default(),
}
}
pub fn fire(&self, url: String, event: WebhookEvent) {
let client = self.client.clone();
tokio::spawn(async move {
match client.post(&url).json(&event).send().await {
Ok(resp) => {
tracing::debug!("webhook {} → {}", url, resp.status());
}
Err(e) => {
tracing::warn!("webhook {} failed: {}", url, e);
}
}
});
}
}
impl Default for WebhookSender {
fn default() -> Self {
Self::new()
}
}