Skip to main content

agentmail/client/
webhooks.rs

1use crate::client::NoBody;
2use crate::{Client, Error, Page, types::*, util::urlish};
3
4impl Client {
5    /// POST /v0/webhooks, subscribe an HTTPS endpoint to events
6    /// (e.g. `message.received`). The response carries the signing `secret`
7    /// exactly once; store it.
8    pub async fn create_webhook(&self, webhook: CreateWebhook) -> Result<Webhook, Error> {
9        self.request(reqwest::Method::POST, "/v0/webhooks", &[], Some(&webhook))
10            .await
11    }
12
13    /// GET /v0/webhooks (first page; see [`Client::list_webhooks_page`]).
14    pub async fn list_webhooks(&self) -> Result<WebhookList, Error> {
15        self.list_webhooks_page(Page::default()).await
16    }
17
18    /// GET /v0/webhooks with pagination. Feed [`WebhookList::next_page_token`]
19    /// back in as [`Page::page_token`] until it comes back `None`.
20    pub async fn list_webhooks_page(&self, page: Page) -> Result<WebhookList, Error> {
21        self.request(
22            reqwest::Method::GET,
23            "/v0/webhooks",
24            &page.query(),
25            None::<&NoBody>,
26        )
27        .await
28    }
29
30    /// GET /v0/webhooks/{webhook_id}
31    pub async fn get_webhook(&self, webhook_id: &str) -> Result<Webhook, Error> {
32        self.request(
33            reqwest::Method::GET,
34            &format!("/v0/webhooks/{}", urlish(webhook_id)),
35            &[],
36            None::<&NoBody>,
37        )
38        .await
39    }
40
41    /// PATCH /v0/webhooks/{webhook_id}, edit event types and inbox/pod
42    /// targeting. Inbox and pod sets change by delta (see [`UpdateWebhook`]).
43    pub async fn update_webhook(
44        &self,
45        webhook_id: &str,
46        update: UpdateWebhook,
47    ) -> Result<Webhook, Error> {
48        self.request(
49            reqwest::Method::PATCH,
50            &format!("/v0/webhooks/{}", urlish(webhook_id)),
51            &[],
52            Some(&update),
53        )
54        .await
55    }
56
57    /// DELETE /v0/webhooks/{webhook_id}
58    pub async fn delete_webhook(&self, webhook_id: &str) -> Result<(), Error> {
59        self.request(
60            reqwest::Method::DELETE,
61            &format!("/v0/webhooks/{}", urlish(webhook_id)),
62            &[],
63            None::<&NoBody>,
64        )
65        .await
66    }
67}