use super::{Alerter, DriftAlertEvent};
use std::time::Duration;
#[derive(Clone, Debug)]
pub struct WebhookAlerter {
url: String,
client: reqwest::blocking::Client,
}
impl WebhookAlerter {
pub fn new(url: impl Into<String>) -> reqwest::Result<Self> {
Self::with_timeout(url, Duration::from_secs(5))
}
pub fn with_timeout(url: impl Into<String>, timeout: Duration) -> reqwest::Result<Self> {
let client = reqwest::blocking::Client::builder()
.timeout(timeout)
.build()?;
Ok(Self {
url: url.into(),
client,
})
}
pub fn try_send(&self, event: &DriftAlertEvent) -> reqwest::Result<()> {
self.client
.post(&self.url)
.json(event)
.send()?
.error_for_status()?;
Ok(())
}
}
impl Alerter for WebhookAlerter {
fn alert(&self, event: &DriftAlertEvent) {
let _ = self.try_send(event);
}
}