Skip to main content

vynfi/resources/
webhooks.rs

1use reqwest::Method;
2
3use crate::client::{extract_list, Client};
4use crate::error::VynFiError;
5use crate::types::*;
6
7/// Webhooks resource — create, list, update, delete, and test webhooks.
8pub struct Webhooks<'a> {
9    client: &'a Client,
10}
11
12impl<'a> Webhooks<'a> {
13    pub(crate) fn new(client: &'a Client) -> Self {
14        Self { client }
15    }
16
17    /// Create a new webhook. The response includes the signing secret — store
18    /// it securely, as it cannot be retrieved again.
19    pub async fn create(&self, req: &CreateWebhookRequest) -> Result<WebhookCreated, VynFiError> {
20        self.client
21            .request_with_body(Method::POST, "/v1/webhooks", Some(req))
22            .await
23    }
24
25    /// List all webhooks for the current account.
26    pub async fn list(&self) -> Result<Vec<Webhook>, VynFiError> {
27        let value: serde_json::Value = self.client.request(Method::GET, "/v1/webhooks").await?;
28        extract_list(value)
29    }
30
31    /// Get a single webhook by ID, including recent delivery history.
32    pub async fn get(&self, webhook_id: &str) -> Result<WebhookDetail, VynFiError> {
33        self.client
34            .request(Method::GET, &format!("/v1/webhooks/{}", webhook_id))
35            .await
36    }
37
38    /// Update a webhook's URL, events, or status.
39    pub async fn update(
40        &self,
41        webhook_id: &str,
42        req: &UpdateWebhookRequest,
43    ) -> Result<Webhook, VynFiError> {
44        self.client
45            .request_with_body(
46                Method::PATCH,
47                &format!("/v1/webhooks/{}", webhook_id),
48                Some(req),
49            )
50            .await
51    }
52
53    /// Delete a webhook. This action is irreversible.
54    pub async fn delete(&self, webhook_id: &str) -> Result<(), VynFiError> {
55        self.client
56            .request::<()>(Method::DELETE, &format!("/v1/webhooks/{}", webhook_id))
57            .await
58    }
59
60    /// Send a test event to the webhook.
61    pub async fn test(&self, webhook_id: &str) -> Result<serde_json::Value, VynFiError> {
62        self.client
63            .request_with_body::<serde_json::Value, ()>(
64                Method::POST,
65                &format!("/v1/webhooks/{}/test", webhook_id),
66                None,
67            )
68            .await
69    }
70}