Skip to main content

voltaria_sdk/api/resources/webhooks/
webhooks.rs

1use crate::api::*;
2use crate::{ApiError, ClientConfig, HttpClient, QueryBuilder, RequestOptions};
3use reqwest::Method;
4use std::collections::HashMap;
5
6pub struct WebhooksClient {
7    pub http_client: HttpClient,
8}
9
10impl WebhooksClient {
11    pub fn new(config: ClientConfig) -> Result<Self, ApiError> {
12        Ok(Self {
13            http_client: HttpClient::new(config.clone())?,
14        })
15    }
16
17    /// List all webhook subscriptions for your partner account.
18    ///
19    /// # Arguments
20    ///
21    /// * `options` - Additional request options such as headers, timeout, etc.
22    ///
23    /// # Returns
24    ///
25    /// JSON response from the API
26    pub async fn list_webhook_subscriptions(
27        &self,
28        request: &ListWebhookSubscriptionsQueryRequest,
29        options: Option<RequestOptions>,
30    ) -> Result<PaginatedResponseWebhookSubscriptionResponse, ApiError> {
31        self.http_client
32            .execute_request(
33                Method::GET,
34                "v2/webhooks/subscriptions",
35                None,
36                QueryBuilder::new()
37                    .int("page", request.page.clone())
38                    .serialize("page_size", request.page_size.clone())
39                    .serialize("event_type", request.event_type.clone())
40                    .build(),
41                options,
42            )
43            .await
44    }
45
46    /// Create a new webhook subscription for a specific event type.
47    ///
48    /// # Arguments
49    ///
50    /// * `options` - Additional request options such as headers, timeout, etc.
51    ///
52    /// # Returns
53    ///
54    /// JSON response from the API
55    pub async fn create_webhook_subscription(
56        &self,
57        request: &WebhookCreatePayload,
58        options: Option<RequestOptions>,
59    ) -> Result<WebhookSubscriptionResponse, ApiError> {
60        self.http_client
61            .execute_request(
62                Method::POST,
63                "v2/webhooks/subscriptions",
64                Some(serde_json::to_value(request).map_err(ApiError::Serialization)?),
65                None,
66                options,
67            )
68            .await
69    }
70
71    /// Retrieve details for a specific webhook subscription with its webhook ID.
72    ///
73    /// # Arguments
74    ///
75    /// * `options` - Additional request options such as headers, timeout, etc.
76    ///
77    /// # Returns
78    ///
79    /// JSON response from the API
80    pub async fn get_webhook_subscription(
81        &self,
82        webhook_id: &str,
83        options: Option<RequestOptions>,
84    ) -> Result<WebhookSubscriptionResponse, ApiError> {
85        self.http_client
86            .execute_request(
87                Method::GET,
88                &format!("v2/webhooks/subscriptions/{}", webhook_id),
89                None,
90                None,
91                options,
92            )
93            .await
94    }
95
96    /// Update a webhook subscription with its specific webhook ID.
97    ///
98    /// # Arguments
99    ///
100    /// * `options` - Additional request options such as headers, timeout, etc.
101    ///
102    /// # Returns
103    ///
104    /// JSON response from the API
105    pub async fn update_webhook_subscription(
106        &self,
107        webhook_id: &str,
108        request: &WebhookUpdatePayload,
109        options: Option<RequestOptions>,
110    ) -> Result<WebhookSubscriptionResponse, ApiError> {
111        self.http_client
112            .execute_request(
113                Method::PUT,
114                &format!("v2/webhooks/subscriptions/{}", webhook_id),
115                Some(serde_json::to_value(request).map_err(ApiError::Serialization)?),
116                None,
117                options,
118            )
119            .await
120    }
121
122    /// Delete a specific webhook subscription.
123    ///
124    /// # Arguments
125    ///
126    /// * `options` - Additional request options such as headers, timeout, etc.
127    ///
128    /// # Returns
129    ///
130    /// JSON response from the API
131    pub async fn delete_webhook_subscription(
132        &self,
133        webhook_id: &str,
134        options: Option<RequestOptions>,
135    ) -> Result<HashMap<String, serde_json::Value>, ApiError> {
136        self.http_client
137            .execute_request(
138                Method::DELETE,
139                &format!("v2/webhooks/subscriptions/{}", webhook_id),
140                None,
141                None,
142                options,
143            )
144            .await
145    }
146
147    /// Retrieve all webhook logs linked to your partner account.
148    ///
149    /// # Arguments
150    ///
151    /// * `options` - Additional request options such as headers, timeout, etc.
152    ///
153    /// # Returns
154    ///
155    /// JSON response from the API
156    pub async fn list_webhook_logs(
157        &self,
158        request: &ListWebhookLogsQueryRequest,
159        options: Option<RequestOptions>,
160    ) -> Result<PaginatedResponseWebhookLogResponse, ApiError> {
161        self.http_client
162            .execute_request(
163                Method::GET,
164                "v2/webhooks/logs",
165                None,
166                QueryBuilder::new()
167                    .serialize("webhook_id", request.webhook_id.clone())
168                    .int("page", request.page.clone())
169                    .serialize("page_size", request.page_size.clone())
170                    .build(),
171                options,
172            )
173            .await
174    }
175}