use reqwest::Method;
use crate::error::Result;
use crate::http::{Envelope, HttpClient};
use crate::models::{CreateWebhook, Message, UpdateWebhook, Webhook, WebhookCreated};
#[derive(Debug, Clone)]
pub struct Webhooks<'a> {
pub(crate) http: &'a HttpClient,
}
impl Webhooks<'_> {
pub async fn list(&self) -> Result<Vec<Webhook>> {
let body: Envelope<Vec<Webhook>> = self
.http
.send::<_, ()>(Method::GET, "/webhooks", None, None)
.await?;
Ok(body.data)
}
pub async fn create(&self, webhook: &CreateWebhook) -> Result<WebhookCreated> {
let body: Envelope<WebhookCreated> = self
.http
.send(Method::POST, "/webhooks", None, Some(webhook))
.await?;
Ok(body.data)
}
pub async fn update(&self, id: &str, changes: &UpdateWebhook) -> Result<Webhook> {
let body: Envelope<Webhook> = self
.http
.send(Method::PUT, &format!("/webhooks/{id}"), None, Some(changes))
.await?;
Ok(body.data)
}
pub async fn delete(&self, id: &str) -> Result<Message> {
self.http
.send::<Message, ()>(Method::DELETE, &format!("/webhooks/{id}"), None, None)
.await
}
pub async fn test(&self, id: &str) -> Result<Message> {
self.http
.send::<Message, ()>(Method::POST, &format!("/webhooks/{id}/test"), None, None)
.await
}
}