dfns_sdk_rs/api/exchanges/
client.rs

1// @dfns-sdk-rs/src/api/exchanges/client.rs
2
3use super::types::*;
4use crate::{
5    error::DfnsError,
6    models::generic::DfnsApiClientOptions,
7    utils::{fetch::simple_fetch, url::build_path_and_query, user_action_fetch::user_action_fetch},
8};
9use std::collections::HashMap;
10
11pub struct ExchangesClient {
12    api_options: DfnsApiClientOptions,
13}
14
15impl ExchangesClient {
16    pub fn new(api_options: DfnsApiClientOptions) -> Self {
17        Self { api_options }
18    }
19
20    pub async fn create_deposit(
21        &self,
22        request: CreateDepositRequest,
23    ) -> Result<CreateDepositResponse, DfnsError> {
24        let path = build_path_and_query(
25            "/exchanges/:exchangeId/accounts/:accountId/deposits",
26            &crate::utils::url::PathAndQueryParams {
27                path: {
28                    let mut map = HashMap::new();
29                    map.insert("exchangeId".to_string(), request.exchange_id);
30                    map.insert("accountId".to_string(), request.account_id);
31                    map
32                },
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(serde_json::to_value(&request.body)?),
43                api_options: self.api_options.clone(),
44            },
45        )
46        .await
47    }
48
49    pub async fn create_exchange(
50        &self,
51        request: CreateExchangeRequest,
52    ) -> Result<CreateExchangeResponse, DfnsError> {
53        let path = build_path_and_query(
54            "/exchanges",
55            &crate::utils::url::PathAndQueryParams {
56                path: HashMap::new(),
57                query: HashMap::new(),
58            },
59        );
60
61        user_action_fetch(
62            &path,
63            crate::utils::fetch::FetchOptions {
64                method: crate::utils::fetch::HttpMethod::POST,
65                headers: None,
66                body: Some(serde_json::to_value(&request.body)?),
67                api_options: self.api_options.clone(),
68            },
69        )
70        .await
71    }
72
73    pub async fn create_withdrawal(
74        &self,
75        request: CreateWithdrawalRequest,
76    ) -> Result<CreateWithdrawalResponse, DfnsError> {
77        let path = build_path_and_query(
78            "/exchanges/:exchangeId/accounts/:accountId/withdrawals",
79            &crate::utils::url::PathAndQueryParams {
80                path: {
81                    let mut map = HashMap::new();
82                    map.insert("exchangeId".to_string(), request.exchange_id);
83                    map.insert("accountId".to_string(), request.account_id);
84                    map
85                },
86                query: HashMap::new(),
87            },
88        );
89
90        user_action_fetch(
91            &path,
92            crate::utils::fetch::FetchOptions {
93                method: crate::utils::fetch::HttpMethod::POST,
94                headers: None,
95                body: Some(serde_json::to_value(&request.body)?),
96                api_options: self.api_options.clone(),
97            },
98        )
99        .await
100    }
101
102    pub async fn delete_exchange(
103        &self,
104        request: DeleteExchangeRequest,
105    ) -> Result<DeleteExchangeResponse, DfnsError> {
106        let path = build_path_and_query(
107            "/exchanges/:exchangeId",
108            &crate::utils::url::PathAndQueryParams {
109                path: {
110                    let mut map = HashMap::new();
111                    map.insert("exchangeId".to_string(), request.exchange_id);
112                    map
113                },
114                query: HashMap::new(),
115            },
116        );
117
118        user_action_fetch(
119            &path,
120            crate::utils::fetch::FetchOptions {
121                method: crate::utils::fetch::HttpMethod::DELETE,
122                headers: None,
123                body: Some(serde_json::json!({})),
124                api_options: self.api_options.clone(),
125            },
126        )
127        .await
128    }
129
130    pub async fn get_exchange(
131        &self,
132        request: GetExchangeRequest,
133    ) -> Result<GetExchangeResponse, DfnsError> {
134        let path = build_path_and_query(
135            "/exchanges/:exchangeId",
136            &crate::utils::url::PathAndQueryParams {
137                path: {
138                    let mut map = HashMap::new();
139                    map.insert("exchangeId".to_string(), request.exchange_id);
140                    map
141                },
142                query: HashMap::new(),
143            },
144        );
145
146        simple_fetch(
147            &path,
148            crate::utils::fetch::FetchOptions {
149                method: crate::utils::fetch::HttpMethod::GET,
150                headers: None,
151                body: None,
152                api_options: self.api_options.base.clone(),
153            },
154        )
155        .await
156    }
157
158    pub async fn list_account_assets(
159        &self,
160        request: ListAccountAssetsRequest,
161    ) -> Result<ListAccountAssetsResponse, DfnsError> {
162        let path = build_path_and_query(
163            "/exchanges/:exchangeId/accounts/:accountId/assets",
164            &crate::utils::url::PathAndQueryParams {
165                path: {
166                    let mut map = HashMap::new();
167                    map.insert("exchangeId".to_string(), request.exchange_id);
168                    map.insert("accountId".to_string(), request.account_id);
169                    map
170                },
171                query: request
172                    .query
173                    .map(|q| {
174                        let mut map = HashMap::new();
175                        if let Some(limit) = q.limit {
176                            map.insert("limit".to_string(), limit.to_string());
177                        }
178                        if let Some(pagination_token) = q.pagination_token {
179                            map.insert("pageToken".to_string(), pagination_token);
180                        }
181                        map
182                    })
183                    .unwrap_or_default(),
184            },
185        );
186
187        simple_fetch(
188            &path,
189            crate::utils::fetch::FetchOptions {
190                method: crate::utils::fetch::HttpMethod::GET,
191                headers: None,
192                body: None,
193                api_options: self.api_options.base.clone(),
194            },
195        )
196        .await
197    }
198
199    pub async fn list_accounts(
200        &self,
201        request: ListAccountsRequest,
202    ) -> Result<ListAccountsResponse, DfnsError> {
203        let path = build_path_and_query(
204            "/exchanges/:exchangeId/accounts",
205            &crate::utils::url::PathAndQueryParams {
206                path: {
207                    let mut map = HashMap::new();
208                    map.insert("exchangeId".to_string(), request.exchange_id);
209                    map
210                },
211                query: request
212                    .query
213                    .map(|q| {
214                        let mut map = HashMap::new();
215                        if let Some(limit) = q.limit {
216                            map.insert("limit".to_string(), limit.to_string());
217                        }
218                        if let Some(pagination_token) = q.pagination_token {
219                            map.insert("pageToken".to_string(), pagination_token);
220                        }
221                        map
222                    })
223                    .unwrap_or_default(),
224            },
225        );
226
227        simple_fetch(
228            &path,
229            crate::utils::fetch::FetchOptions {
230                method: crate::utils::fetch::HttpMethod::GET,
231                headers: None,
232                body: None,
233                api_options: self.api_options.base.clone(),
234            },
235        )
236        .await
237    }
238
239    pub async fn list_asset_withdrawal_networks(
240        &self,
241        request: ListAssetWithdrawalNetworksRequest,
242    ) -> Result<ListAssetWithdrawalNetworksResponse, DfnsError> {
243        let path = build_path_and_query(
244            "/exchanges/:exchangeId/accounts/:accountId/assets/:asset/withdrawal-networks",
245            &crate::utils::url::PathAndQueryParams {
246                path: {
247                    let mut map = HashMap::new();
248                    map.insert("exchangeId".to_string(), request.exchange_id);
249                    map.insert("accountId".to_string(), request.account_id);
250                    map.insert("asset".to_string(), request.asset);
251                    map
252                },
253                query: HashMap::new(),
254            },
255        );
256
257        simple_fetch(
258            &path,
259            crate::utils::fetch::FetchOptions {
260                method: crate::utils::fetch::HttpMethod::GET,
261                headers: None,
262                body: None,
263                api_options: self.api_options.base.clone(),
264            },
265        )
266        .await
267    }
268
269    pub async fn list_exchanges(
270        &self,
271        request: Option<ListExchangesRequest>,
272    ) -> Result<ListExchangesResponse, DfnsError> {
273        let path = build_path_and_query(
274            "/exchanges",
275            &crate::utils::url::PathAndQueryParams {
276                path: HashMap::new(),
277                query: request
278                    .and_then(|r| r.query)
279                    .map(|q| {
280                        let mut map = HashMap::new();
281                        if let Some(limit) = q.limit {
282                            map.insert("limit".to_string(), limit.to_string());
283                        }
284                        if let Some(pagination_token) = q.pagination_token {
285                            map.insert("pageToken".to_string(), pagination_token);
286                        }
287                        map
288                    })
289                    .unwrap_or_default(),
290            },
291        );
292
293        simple_fetch(
294            &path,
295            crate::utils::fetch::FetchOptions {
296                method: crate::utils::fetch::HttpMethod::GET,
297                headers: None,
298                body: None,
299                api_options: self.api_options.base.clone(),
300            },
301        )
302        .await
303    }
304}