dfns_sdk_rs/api/webhooks/
delegated_client.rs

1// @dfns-sdk-rs/src/api/webhooks/delegated_client.rs
2
3use super::types::*;
4use crate::{
5    client::{
6        base_auth_api::{
7            BaseAuthApi, CreateUserActionChallengeRequest, SignUserActionChallengeRequest,
8        },
9        delegated_api_client::DfnsDelegatedApiClientOptions,
10    },
11    error::DfnsError,
12    utils::{
13        fetch::{simple_fetch, FetchOptions, HttpMethod},
14        url::build_path_and_query,
15    },
16};
17use std::collections::HashMap;
18
19pub struct DelegatedWebhooksClient {
20    api_options: DfnsDelegatedApiClientOptions,
21}
22
23impl DelegatedWebhooksClient {
24    pub fn new(api_options: DfnsDelegatedApiClientOptions) -> Self {
25        Self { api_options }
26    }
27
28    pub async fn create_webhook_init(
29        &self,
30        request: CreateWebhookRequest,
31    ) -> Result<crate::signer::UserActionChallenge, DfnsError> {
32        let path = build_path_and_query(
33            "/webhooks",
34            &crate::utils::url::PathAndQueryParams {
35                path: HashMap::new(),
36                query: HashMap::new(),
37            },
38        );
39
40        BaseAuthApi::create_user_action_challenge(
41            CreateUserActionChallengeRequest {
42                user_action_http_method: HttpMethod::POST,
43                user_action_http_path: path,
44                user_action_payload: serde_json::to_string(&request.body)?,
45                user_action_server_kind: "Api".to_string(),
46            },
47            self.api_options.base.clone(),
48        )
49        .await
50    }
51
52    pub async fn create_webhook_complete(
53        &self,
54        request: CreateWebhookRequest,
55        signed_challenge: SignUserActionChallengeRequest,
56    ) -> Result<CreateWebhookResponse, DfnsError> {
57        let path = build_path_and_query(
58            "/webhooks",
59            &crate::utils::url::PathAndQueryParams {
60                path: HashMap::new(),
61                query: HashMap::new(),
62            },
63        );
64
65        let user_action = BaseAuthApi::sign_user_action_challenge(
66            signed_challenge,
67            self.api_options.base.clone(),
68        )
69        .await?
70        .user_action;
71
72        let mut headers = HashMap::new();
73        headers.insert("x-dfns-useraction".to_string(), user_action);
74
75        simple_fetch(
76            &path,
77            FetchOptions {
78                method: HttpMethod::POST,
79                headers: Some(headers),
80                body: Some(serde_json::to_value(&request.body)?),
81                api_options: self.api_options.base.clone(),
82            },
83        )
84        .await
85    }
86
87    pub async fn delete_webhook_init(
88        &self,
89        request: DeleteWebhookRequest,
90    ) -> Result<crate::signer::UserActionChallenge, DfnsError> {
91        let path = build_path_and_query(
92            "/webhooks/:webhookId",
93            &crate::utils::url::PathAndQueryParams {
94                path: {
95                    let mut map = HashMap::new();
96                    map.insert("webhookId".to_string(), request.webhook_id);
97                    map
98                },
99                query: HashMap::new(),
100            },
101        );
102
103        BaseAuthApi::create_user_action_challenge(
104            CreateUserActionChallengeRequest {
105                user_action_http_method: HttpMethod::DELETE,
106                user_action_http_path: path,
107                user_action_payload: serde_json::to_string(&serde_json::json!({}))?,
108                user_action_server_kind: "Api".to_string(),
109            },
110            self.api_options.base.clone(),
111        )
112        .await
113    }
114
115    pub async fn delete_webhook_complete(
116        &self,
117        request: DeleteWebhookRequest,
118        signed_challenge: SignUserActionChallengeRequest,
119    ) -> Result<DeleteWebhookResponse, DfnsError> {
120        let path = build_path_and_query(
121            "/webhooks/:webhookId",
122            &crate::utils::url::PathAndQueryParams {
123                path: {
124                    let mut map = HashMap::new();
125                    map.insert("webhookId".to_string(), request.webhook_id);
126                    map
127                },
128                query: HashMap::new(),
129            },
130        );
131
132        let user_action = BaseAuthApi::sign_user_action_challenge(
133            signed_challenge,
134            self.api_options.base.clone(),
135        )
136        .await?
137        .user_action;
138
139        let mut headers = HashMap::new();
140        headers.insert("x-dfns-useraction".to_string(), user_action);
141
142        simple_fetch(
143            &path,
144            FetchOptions {
145                method: HttpMethod::DELETE,
146                headers: Some(headers),
147                body: Some(serde_json::json!({})),
148                api_options: self.api_options.base.clone(),
149            },
150        )
151        .await
152    }
153
154    pub async fn get_webhook(
155        &self,
156        request: GetWebhookRequest,
157    ) -> Result<GetWebhookResponse, DfnsError> {
158        let path = build_path_and_query(
159            "/webhooks/:webhookId",
160            &crate::utils::url::PathAndQueryParams {
161                path: {
162                    let mut map = HashMap::new();
163                    map.insert("webhookId".to_string(), request.webhook_id);
164                    map
165                },
166                query: HashMap::new(),
167            },
168        );
169
170        simple_fetch(
171            &path,
172            FetchOptions {
173                method: HttpMethod::GET,
174                headers: None,
175                body: None,
176                api_options: self.api_options.base.clone(),
177            },
178        )
179        .await
180    }
181
182    pub async fn get_webhook_event(
183        &self,
184        request: GetWebhookEventRequest,
185    ) -> Result<GetWebhookEventResponse, DfnsError> {
186        let path = build_path_and_query(
187            "/webhooks/:webhookId/events/:webhookEventId",
188            &crate::utils::url::PathAndQueryParams {
189                path: {
190                    let mut map = HashMap::new();
191                    map.insert("webhookId".to_string(), request.webhook_id);
192                    map.insert("webhookEventId".to_string(), request.webhook_event_id);
193                    map
194                },
195                query: HashMap::new(),
196            },
197        );
198
199        simple_fetch(
200            &path,
201            FetchOptions {
202                method: HttpMethod::GET,
203                headers: None,
204                body: None,
205                api_options: self.api_options.base.clone(),
206            },
207        )
208        .await
209    }
210
211    pub async fn list_webhook_events(
212        &self,
213        request: ListWebhookEventsRequest,
214    ) -> Result<ListWebhookEventsResponse, DfnsError> {
215        let path = build_path_and_query(
216            "/webhooks/:webhookId/events",
217            &crate::utils::url::PathAndQueryParams {
218                path: {
219                    let mut map = HashMap::new();
220                    map.insert("webhookId".to_string(), request.webhook_id);
221                    map
222                },
223                query: request
224                    .query
225                    .map(|q| {
226                        let mut map = HashMap::new();
227                        if let Some(delivery_failed) = q.delivery_failed {
228                            map.insert("deliveryFailed".to_string(), delivery_failed.to_string());
229                        }
230                        if let Some(kind) = q.kind {
231                            map.insert("kind".to_string(), kind.to_string());
232                        }
233                        if let Some(limit) = q.limit {
234                            map.insert("limit".to_string(), limit.to_string());
235                        }
236                        if let Some(token) = q.pagination_token {
237                            map.insert("pageToken".to_string(), token);
238                        }
239                        map
240                    })
241                    .unwrap_or_default(),
242            },
243        );
244
245        simple_fetch(
246            &path,
247            FetchOptions {
248                method: HttpMethod::GET,
249                headers: None,
250                body: None,
251                api_options: self.api_options.base.clone(),
252            },
253        )
254        .await
255    }
256
257    pub async fn list_webhooks(
258        &self,
259        request: Option<ListWebhooksRequest>,
260    ) -> Result<ListWebhooksResponse, DfnsError> {
261        let path = build_path_and_query(
262            "/webhooks",
263            &crate::utils::url::PathAndQueryParams {
264                path: HashMap::new(),
265                query: request
266                    .and_then(|r| r.query)
267                    .map(|q| {
268                        let mut map = HashMap::new();
269                        if let Some(limit) = q.limit {
270                            map.insert("limit".to_string(), limit.to_string());
271                        }
272                        if let Some(token) = q.pagination_token {
273                            map.insert("pageToken".to_string(), token);
274                        }
275                        map
276                    })
277                    .unwrap_or_default(),
278            },
279        );
280
281        simple_fetch(
282            &path,
283            FetchOptions {
284                method: HttpMethod::GET,
285                headers: None,
286                body: None,
287                api_options: self.api_options.base.clone(),
288            },
289        )
290        .await
291    }
292
293    pub async fn ping_webhook_init(
294        &self,
295        request: PingWebhookRequest,
296    ) -> Result<crate::signer::UserActionChallenge, DfnsError> {
297        let path = build_path_and_query(
298            "/webhooks/:webhookId/ping",
299            &crate::utils::url::PathAndQueryParams {
300                path: {
301                    let mut map = HashMap::new();
302                    map.insert("webhookId".to_string(), request.webhook_id);
303                    map
304                },
305                query: HashMap::new(),
306            },
307        );
308
309        BaseAuthApi::create_user_action_challenge(
310            CreateUserActionChallengeRequest {
311                user_action_http_method: HttpMethod::POST,
312                user_action_http_path: path,
313                user_action_payload: serde_json::to_string(&serde_json::json!({}))?,
314                user_action_server_kind: "Api".to_string(),
315            },
316            self.api_options.base.clone(),
317        )
318        .await
319    }
320
321    pub async fn ping_webhook_complete(
322        &self,
323        request: PingWebhookRequest,
324        signed_challenge: SignUserActionChallengeRequest,
325    ) -> Result<PingWebhookResponse, DfnsError> {
326        let path = build_path_and_query(
327            "/webhooks/:webhookId/ping",
328            &crate::utils::url::PathAndQueryParams {
329                path: {
330                    let mut map = HashMap::new();
331                    map.insert("webhookId".to_string(), request.webhook_id);
332                    map
333                },
334                query: HashMap::new(),
335            },
336        );
337
338        let user_action = BaseAuthApi::sign_user_action_challenge(
339            signed_challenge,
340            self.api_options.base.clone(),
341        )
342        .await?
343        .user_action;
344
345        let mut headers = HashMap::new();
346        headers.insert("x-dfns-useraction".to_string(), user_action);
347
348        simple_fetch(
349            &path,
350            FetchOptions {
351                method: HttpMethod::POST,
352                headers: Some(headers),
353                body: Some(serde_json::json!({})),
354                api_options: self.api_options.base.clone(),
355            },
356        )
357        .await
358    }
359
360    pub async fn update_webhook_init(
361        &self,
362        request: UpdateWebhookRequest,
363    ) -> Result<crate::signer::UserActionChallenge, DfnsError> {
364        let path = build_path_and_query(
365            "/webhooks/:webhookId",
366            &crate::utils::url::PathAndQueryParams {
367                path: {
368                    let mut map = HashMap::new();
369                    map.insert("webhookId".to_string(), request.webhook_id);
370                    map
371                },
372                query: HashMap::new(),
373            },
374        );
375
376        BaseAuthApi::create_user_action_challenge(
377            CreateUserActionChallengeRequest {
378                user_action_http_method: HttpMethod::PUT,
379                user_action_http_path: path,
380                user_action_payload: serde_json::to_string(&request.body)?,
381                user_action_server_kind: "Api".to_string(),
382            },
383            self.api_options.base.clone(),
384        )
385        .await
386    }
387
388    pub async fn update_webhook_complete(
389        &self,
390        request: UpdateWebhookRequest,
391        signed_challenge: SignUserActionChallengeRequest,
392    ) -> Result<UpdateWebhookResponse, DfnsError> {
393        let path = build_path_and_query(
394            "/webhooks/:webhookId",
395            &crate::utils::url::PathAndQueryParams {
396                path: {
397                    let mut map = HashMap::new();
398                    map.insert("webhookId".to_string(), request.webhook_id);
399                    map
400                },
401                query: HashMap::new(),
402            },
403        );
404
405        let user_action = BaseAuthApi::sign_user_action_challenge(
406            signed_challenge,
407            self.api_options.base.clone(),
408        )
409        .await?
410        .user_action;
411
412        let mut headers = HashMap::new();
413        headers.insert("x-dfns-useraction".to_string(), user_action);
414
415        simple_fetch(
416            &path,
417            FetchOptions {
418                method: HttpMethod::PUT,
419                headers: Some(headers),
420                body: Some(serde_json::to_value(&request.body)?),
421                api_options: self.api_options.base.clone(),
422            },
423        )
424        .await
425    }
426}