dfns_sdk_rs/api/webhooks/
client.rs

1// @dfns-sdk-rs/src/api/webhooks/client.rs
2
3use super::types::*;
4use crate::{
5    models::generic::DfnsApiClientOptions,
6    utils::{
7        fetch::simple_fetch,
8        url::{build_path_and_query, PathAndQueryParams},
9        user_action_fetch::user_action_fetch,
10    },
11};
12use serde_json::json;
13use std::collections::HashMap;
14
15#[derive(Debug, Clone, PartialEq)]
16pub struct WebhooksClient {
17    api_options: DfnsApiClientOptions,
18}
19
20impl WebhooksClient {
21    pub fn new(api_options: DfnsApiClientOptions) -> Self {
22        Self { api_options }
23    }
24
25    pub async fn create_webhook(
26        &self,
27        request: CreateWebhookRequest,
28    ) -> Result<CreateWebhookResponse, crate::error::DfnsError> {
29        let path = build_path_and_query(
30            "/webhooks",
31            &PathAndQueryParams {
32                path: HashMap::new(),
33                query: HashMap::new(),
34            },
35        );
36
37        user_action_fetch(
38            &path,
39            crate::utils::fetch::FetchOptions {
40                method: crate::utils::fetch::HttpMethod::POST,
41                headers: None,
42                body: Some(json!(request.body)),
43                api_options: self.api_options.clone(),
44            },
45        )
46        .await
47    }
48
49    pub async fn delete_webhook(
50        &self,
51        request: DeleteWebhookRequest,
52    ) -> Result<DeleteWebhookResponse, crate::error::DfnsError> {
53        let mut path_params = HashMap::new();
54        path_params.insert("webhookId".to_string(), request.webhook_id);
55
56        let path = build_path_and_query(
57            "/webhooks/:webhookId",
58            &PathAndQueryParams {
59                path: path_params,
60                query: HashMap::new(),
61            },
62        );
63
64        user_action_fetch(
65            &path,
66            crate::utils::fetch::FetchOptions {
67                method: crate::utils::fetch::HttpMethod::DELETE,
68                headers: None,
69                body: Some(json!({})),
70                api_options: self.api_options.clone(),
71            },
72        )
73        .await
74    }
75
76    pub async fn get_webhook(
77        &self,
78        request: GetWebhookRequest,
79    ) -> Result<GetWebhookResponse, crate::error::DfnsError> {
80        let mut path_params = HashMap::new();
81        path_params.insert("webhookId".to_string(), request.webhook_id);
82
83        let path = build_path_and_query(
84            "/webhooks/:webhookId",
85            &PathAndQueryParams {
86                path: path_params,
87                query: HashMap::new(),
88            },
89        );
90
91        simple_fetch(
92            &path,
93            crate::utils::fetch::FetchOptions {
94                method: crate::utils::fetch::HttpMethod::GET,
95                headers: None,
96                body: None,
97                api_options: self.api_options.base.clone(),
98            },
99        )
100        .await
101    }
102
103    pub async fn get_webhook_event(
104        &self,
105        request: GetWebhookEventRequest,
106    ) -> Result<GetWebhookEventResponse, crate::error::DfnsError> {
107        let mut path_params = HashMap::new();
108        path_params.insert("webhookId".to_string(), request.webhook_id);
109        path_params.insert("webhookEventId".to_string(), request.webhook_event_id);
110
111        let path = build_path_and_query(
112            "/webhooks/:webhookId/events/:webhookEventId",
113            &PathAndQueryParams {
114                path: path_params,
115                query: HashMap::new(),
116            },
117        );
118
119        simple_fetch(
120            &path,
121            crate::utils::fetch::FetchOptions {
122                method: crate::utils::fetch::HttpMethod::GET,
123                headers: None,
124                body: None,
125                api_options: self.api_options.base.clone(),
126            },
127        )
128        .await
129    }
130
131    pub async fn list_webhook_events(
132        &self,
133        request: ListWebhookEventsRequest,
134    ) -> Result<ListWebhookEventsResponse, crate::error::DfnsError> {
135        let mut path_params = HashMap::new();
136        path_params.insert("webhookId".to_string(), request.webhook_id);
137
138        let mut query_params = HashMap::new();
139        if let Some(query) = request.query {
140            if let Some(delivery_failed) = query.delivery_failed {
141                query_params.insert("deliveryFailed".to_string(), delivery_failed.to_string());
142            }
143            if let Some(kind) = query.kind {
144                query_params.insert("kind".to_string(), kind.to_string());
145            }
146            if let Some(limit) = query.limit {
147                query_params.insert("limit".to_string(), limit.to_string());
148            }
149            if let Some(token) = query.pagination_token {
150                query_params.insert("paginationToken".to_string(), token);
151            }
152        }
153
154        let path = build_path_and_query(
155            "/webhooks/:webhookId/events",
156            &PathAndQueryParams {
157                path: path_params,
158                query: query_params,
159            },
160        );
161
162        simple_fetch(
163            &path,
164            crate::utils::fetch::FetchOptions {
165                method: crate::utils::fetch::HttpMethod::GET,
166                headers: None,
167                body: None,
168                api_options: self.api_options.base.clone(),
169            },
170        )
171        .await
172    }
173
174    pub async fn list_webhooks(
175        &self,
176        request: Option<ListWebhooksRequest>,
177    ) -> Result<ListWebhooksResponse, crate::error::DfnsError> {
178        let mut query_params = HashMap::new();
179        if let Some(req) = request {
180            if let Some(query) = req.query {
181                if let Some(limit) = query.limit {
182                    query_params.insert("limit".to_string(), limit.to_string());
183                }
184                if let Some(token) = query.pagination_token {
185                    query_params.insert("paginationToken".to_string(), token);
186                }
187            }
188        }
189
190        let path = build_path_and_query(
191            "/webhooks",
192            &PathAndQueryParams {
193                path: HashMap::new(),
194                query: query_params,
195            },
196        );
197
198        simple_fetch(
199            &path,
200            crate::utils::fetch::FetchOptions {
201                method: crate::utils::fetch::HttpMethod::GET,
202                headers: None,
203                body: None,
204                api_options: self.api_options.base.clone(),
205            },
206        )
207        .await
208    }
209
210    pub async fn ping_webhook(
211        &self,
212        request: PingWebhookRequest,
213    ) -> Result<PingWebhookResponse, crate::error::DfnsError> {
214        let mut path_params = HashMap::new();
215        path_params.insert("webhookId".to_string(), request.webhook_id);
216
217        let path = build_path_and_query(
218            "/webhooks/:webhookId/ping",
219            &PathAndQueryParams {
220                path: path_params,
221                query: HashMap::new(),
222            },
223        );
224
225        user_action_fetch(
226            &path,
227            crate::utils::fetch::FetchOptions {
228                method: crate::utils::fetch::HttpMethod::POST,
229                headers: None,
230                body: Some(json!({})),
231                api_options: self.api_options.clone(),
232            },
233        )
234        .await
235    }
236
237    pub async fn update_webhook(
238        &self,
239        request: UpdateWebhookRequest,
240    ) -> Result<UpdateWebhookResponse, crate::error::DfnsError> {
241        let mut path_params = HashMap::new();
242        path_params.insert("webhookId".to_string(), request.webhook_id);
243
244        let path = build_path_and_query(
245            "/webhooks/:webhookId",
246            &PathAndQueryParams {
247                path: path_params,
248                query: HashMap::new(),
249            },
250        );
251
252        user_action_fetch(
253            &path,
254            crate::utils::fetch::FetchOptions {
255                method: crate::utils::fetch::HttpMethod::PUT,
256                headers: None,
257                body: Some(json!(request.body)),
258                api_options: self.api_options.clone(),
259            },
260        )
261        .await
262    }
263}