use reqwest::Method;
use super::{query, urlencode};
use crate::error::Result;
use crate::http::HttpTransport;
use crate::models::{
CreateWebhook, Page, UpdateWebhook, Webhook, WebhookDelivery, WebhookDeliveryDetail,
WebhookTestResult,
};
#[derive(Debug, Clone)]
pub struct Webhooks {
http: HttpTransport,
}
impl Webhooks {
pub(crate) fn new(http: HttpTransport) -> Self {
Self { http }
}
pub async fn list(&self) -> Result<Vec<Webhook>> {
self.http.request(Method::GET, "/v1/webhooks/").await
}
pub async fn create(&self, params: &CreateWebhook) -> Result<Webhook> {
self.http
.request_json(Method::POST, "/v1/webhooks/", params)
.await
}
pub async fn update(&self, id: &str, params: &UpdateWebhook) -> Result<Webhook> {
self.http
.request_json(
Method::PATCH,
&format!("/v1/webhooks/{}", urlencode(id)),
params,
)
.await
}
pub async fn delete(&self, id: &str) -> Result<()> {
self.http
.request_empty(Method::DELETE, &format!("/v1/webhooks/{}", urlencode(id)))
.await
}
pub async fn test(&self, id: &str) -> Result<WebhookTestResult> {
self.http
.request(
Method::POST,
&format!("/v1/webhooks/{}/test", urlencode(id)),
)
.await
}
pub async fn list_deliveries(
&self,
id: &str,
page: Option<u64>,
limit: Option<u64>,
status: Option<&str>,
) -> Result<Page<WebhookDelivery>> {
let q = query(&[
("page", page.map(|p| p.to_string())),
("limit", limit.map(|l| l.to_string())),
("status", status.map(String::from)),
]);
self.http
.request(
Method::GET,
&format!("/v1/webhooks/{}/deliveries{q}", urlencode(id)),
)
.await
}
pub async fn get_delivery(
&self,
id: &str,
delivery_id: &str,
) -> Result<WebhookDeliveryDetail> {
self.http
.request(
Method::GET,
&format!(
"/v1/webhooks/{}/deliveries/{}",
urlencode(id),
urlencode(delivery_id)
),
)
.await
}
}