rain_sdk/api/webhooks.rs
1//! Webhooks API
2//!
3//! This module provides functionality to manage webhooks.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::webhooks::*;
8use uuid::Uuid;
9
10impl RainClient {
11 /// Get all webhooks
12 ///
13 /// # Arguments
14 ///
15 /// * `params` - Query parameters to filter webhooks
16 ///
17 /// # Returns
18 ///
19 /// Returns a [`Vec<Webhook>`] containing the list of webhooks.
20 ///
21 /// # Errors
22 ///
23 /// This method can return the following errors:
24 /// - `401` - Invalid authorization
25 /// - `403` - Forbidden
26 /// - `404` - User not found
27 /// - `500` - Internal server error
28 ///
29 /// # Examples
30 ///
31 /// ```no_run
32 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
33 /// use rain_sdk::models::webhooks::ListWebhooksParams;
34 ///
35 /// # #[cfg(feature = "async")]
36 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
37 /// let config = Config::new(Environment::Dev);
38 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
39 /// let client = RainClient::new(config, auth)?;
40 ///
41 /// let params = ListWebhooksParams {
42 /// resource_id: None,
43 /// resource_type: None,
44 /// resource_action: None,
45 /// request_sent_at_before: None,
46 /// request_sent_at_after: None,
47 /// response_received_at_before: None,
48 /// response_received_at_after: None,
49 /// cursor: None,
50 /// limit: Some(20),
51 /// };
52 /// let webhooks = client.list_webhooks(¶ms).await?;
53 /// # Ok(())
54 /// # }
55 /// ```
56 #[cfg(feature = "async")]
57 pub async fn list_webhooks(&self, params: &ListWebhooksParams) -> Result<Vec<Webhook>> {
58 let path = "/webhooks";
59 let query_string = serde_urlencoded::to_string(params)?;
60 let full_path = if query_string.is_empty() {
61 path.to_string()
62 } else {
63 format!("{path}?{query_string}")
64 };
65 self.get(&full_path).await
66 }
67
68 /// Get a webhook by its id
69 ///
70 /// # Arguments
71 ///
72 /// * `webhook_id` - The unique identifier of the webhook
73 ///
74 /// # Returns
75 ///
76 /// Returns a [`Webhook`] containing the webhook information.
77 ///
78 /// # Errors
79 ///
80 /// This method can return the following errors:
81 /// - `401` - Invalid authorization
82 /// - `403` - Forbidden
83 /// - `404` - Transaction not found
84 /// - `500` - Internal server error
85 ///
86 /// # Examples
87 ///
88 /// ```no_run
89 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
90 /// use uuid::Uuid;
91 ///
92 /// # #[cfg(feature = "async")]
93 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
94 /// let config = Config::new(Environment::Dev);
95 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
96 /// let client = RainClient::new(config, auth)?;
97 ///
98 /// let webhook_id = Uuid::new_v4();
99 /// let webhook = client.get_webhook(&webhook_id).await?;
100 /// # Ok(())
101 /// # }
102 /// ```
103 #[cfg(feature = "async")]
104 pub async fn get_webhook(&self, webhook_id: &Uuid) -> Result<Webhook> {
105 let path = format!("/webhooks/{webhook_id}");
106 self.get(&path).await
107 }
108
109 /// Get all webhooks (blocking)
110 ///
111 /// # Arguments
112 ///
113 /// * `params` - Query parameters to filter webhooks
114 ///
115 /// # Returns
116 ///
117 /// Returns a [`Vec<Webhook>`] containing the list of webhooks.
118 #[cfg(feature = "sync")]
119 pub fn list_webhooks_blocking(&self, params: &ListWebhooksParams) -> Result<Vec<Webhook>> {
120 let path = "/webhooks";
121 let query_string = serde_urlencoded::to_string(params)?;
122 let full_path = if query_string.is_empty() {
123 path.to_string()
124 } else {
125 format!("{path}?{query_string}")
126 };
127 self.get_blocking(&full_path)
128 }
129
130 /// Get a webhook by its id (blocking)
131 ///
132 /// # Arguments
133 ///
134 /// * `webhook_id` - The unique identifier of the webhook
135 ///
136 /// # Returns
137 ///
138 /// Returns a [`Webhook`] containing the webhook information.
139 #[cfg(feature = "sync")]
140 pub fn get_webhook_blocking(&self, webhook_id: &Uuid) -> Result<Webhook> {
141 let path = format!("/webhooks/{webhook_id}");
142 self.get_blocking(&path)
143 }
144}