Skip to main content

agentmail/client/
webhooks.rs

1use crate::client::NoBody;
2use crate::client::scope::{Scoped, Webhooks};
3use crate::{Error, Page, types::*, util::urlish};
4
5impl<S: Webhooks> Scoped<'_, S> {
6    /// POST `{scope}/webhooks`, subscribe an HTTPS endpoint to events (e.g.
7    /// `message.received`). The response carries the signing `secret` exactly
8    /// once; store it. Inbox and pod scopes ignore `inbox_ids`/`pod_ids` (the
9    /// scope already targets the delivery); set those only at [`Client::org`](crate::Client::org).
10    pub async fn create_webhook(&self, webhook: CreateWebhook) -> Result<Webhook, Error> {
11        self.client
12            .request(
13                reqwest::Method::POST,
14                &format!("{}/webhooks", self.base()),
15                &[],
16                Some(&webhook),
17            )
18            .await
19    }
20
21    /// GET `{scope}/webhooks`, one page.
22    pub async fn list_webhooks(&self, page: Page) -> Result<WebhookList, Error> {
23        self.client
24            .request(
25                reqwest::Method::GET,
26                &format!("{}/webhooks", self.base()),
27                &page.query(),
28                None::<&NoBody>,
29            )
30            .await
31    }
32
33    /// Every webhook, draining pagination.
34    pub async fn list_all_webhooks(&self) -> Result<Vec<Webhook>, Error> {
35        let mut out = Vec::new();
36        let mut token = None;
37        loop {
38            let resp = self
39                .list_webhooks(Page {
40                    limit: None,
41                    page_token: token,
42                })
43                .await?;
44            let next = resp.next_page_token;
45            out.extend(resp.webhooks);
46            match next {
47                Some(t) => token = Some(t),
48                None => return Ok(out),
49            }
50        }
51    }
52
53    /// GET `{scope}/webhooks/{webhook_id}`.
54    pub async fn get_webhook(&self, webhook_id: &str) -> Result<Webhook, Error> {
55        self.client
56            .request(
57                reqwest::Method::GET,
58                &format!("{}/webhooks/{}", self.base(), urlish(webhook_id)),
59                &[],
60                None::<&NoBody>,
61            )
62            .await
63    }
64
65    /// PATCH `{scope}/webhooks/{webhook_id}`, edit event types and inbox/pod
66    /// targeting (see [`UpdateWebhook`]).
67    pub async fn update_webhook(
68        &self,
69        webhook_id: &str,
70        update: UpdateWebhook,
71    ) -> Result<Webhook, Error> {
72        self.client
73            .request(
74                reqwest::Method::PATCH,
75                &format!("{}/webhooks/{}", self.base(), urlish(webhook_id)),
76                &[],
77                Some(&update),
78            )
79            .await
80    }
81
82    /// DELETE `{scope}/webhooks/{webhook_id}`.
83    pub async fn delete_webhook(&self, webhook_id: &str) -> Result<(), Error> {
84        self.client
85            .request(
86                reqwest::Method::DELETE,
87                &format!("{}/webhooks/{}", self.base(), urlish(webhook_id)),
88                &[],
89                None::<&NoBody>,
90            )
91            .await
92    }
93}