Skip to main content

axene_mailer/resources/
webhooks.rs

1//! The `webhooks` resource: manage event subscriptions and inspect deliveries.
2
3use reqwest::Method;
4
5use super::{query, urlencode};
6use crate::error::Result;
7use crate::http::HttpTransport;
8use crate::models::{
9    CreateWebhook, Page, UpdateWebhook, Webhook, WebhookDelivery, WebhookDeliveryDetail,
10    WebhookTestResult,
11};
12
13/// Accessed as `client.webhooks()`.
14#[derive(Debug, Clone)]
15pub struct Webhooks {
16    http: HttpTransport,
17}
18
19impl Webhooks {
20    pub(crate) fn new(http: HttpTransport) -> Self {
21        Self { http }
22    }
23
24    /// List your active webhooks.
25    pub async fn list(&self) -> Result<Vec<Webhook>> {
26        self.http.request(Method::GET, "/v1/webhooks/").await
27    }
28
29    /// Create a webhook. The signing `secret` is generated and returned.
30    pub async fn create(&self, params: &CreateWebhook) -> Result<Webhook> {
31        self.http
32            .request_json(Method::POST, "/v1/webhooks/", params)
33            .await
34    }
35
36    /// Update a webhook's url, events, or active state (partial).
37    pub async fn update(&self, id: &str, params: &UpdateWebhook) -> Result<Webhook> {
38        self.http
39            .request_json(
40                Method::PATCH,
41                &format!("/v1/webhooks/{}", urlencode(id)),
42                params,
43            )
44            .await
45    }
46
47    /// Delete a webhook.
48    pub async fn delete(&self, id: &str) -> Result<()> {
49        self.http
50            .request_empty(Method::DELETE, &format!("/v1/webhooks/{}", urlencode(id)))
51            .await
52    }
53
54    /// Queue a sample `email.delivered` delivery to test the endpoint.
55    pub async fn test(&self, id: &str) -> Result<WebhookTestResult> {
56        self.http
57            .request(
58                Method::POST,
59                &format!("/v1/webhooks/{}/test", urlencode(id)),
60            )
61            .await
62    }
63
64    /// List delivery attempts for a webhook (paginated envelope).
65    pub async fn list_deliveries(
66        &self,
67        id: &str,
68        page: Option<u64>,
69        limit: Option<u64>,
70        status: Option<&str>,
71    ) -> Result<Page<WebhookDelivery>> {
72        let q = query(&[
73            ("page", page.map(|p| p.to_string())),
74            ("limit", limit.map(|l| l.to_string())),
75            ("status", status.map(String::from)),
76        ]);
77        self.http
78            .request(
79                Method::GET,
80                &format!("/v1/webhooks/{}/deliveries{q}", urlencode(id)),
81            )
82            .await
83    }
84
85    /// Fetch one delivery with its full payload and the endpoint's response.
86    pub async fn get_delivery(
87        &self,
88        id: &str,
89        delivery_id: &str,
90    ) -> Result<WebhookDeliveryDetail> {
91        self.http
92            .request(
93                Method::GET,
94                &format!(
95                    "/v1/webhooks/{}/deliveries/{}",
96                    urlencode(id),
97                    urlencode(delivery_id)
98                ),
99            )
100            .await
101    }
102}