use reqwest::Client;
use serde_json::Value;
use super::PublishError;
pub struct WebhookTarget {
client: Client,
url: String,
}
impl WebhookTarget {
pub fn new(client: Client, url: String) -> Self {
Self { client, url }
}
pub async fn deliver(&self, envelope: &Value) -> Result<(), PublishError> {
let response = self.client.post(&self.url).json(envelope).send().await?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
tracing::warn!(
status = %status,
url = %self.url,
body = %body,
"webhook delivery received non-2xx response"
);
return Err(PublishError::Serialization(format!("webhook returned {status}")));
}
tracing::debug!(url = %self.url, "webhook delivery succeeded");
Ok(())
}
pub fn url(&self) -> &str {
&self.url
}
}