Skip to main content

dna_rs/ops/
account.rs

1use serde_json::Value;
2
3use crate::client::DnaClient;
4use crate::error::{DnaError, DnaResult};
5use crate::models::account::{AccountResponse, Balance, CurrentBalance, ResellerDetails};
6use crate::ops::util::currency_meta;
7
8impl DnaClient {
9    /// Fetch reseller profile including all currency balances.
10    pub async fn get_reseller_details(&self) -> DnaResult<ResellerDetails> {
11        let raw: AccountResponse = self.http.get("deposit/accounts/me", None::<&()>).await?;
12
13        let id = raw
14            .reseller_id
15            .ok_or_else(|| DnaError::UnexpectedResponse("Response missing `resellerId`".into()))?;
16
17        Ok(ResellerDetails {
18            id,
19            name: raw.reseller_name.unwrap_or_default(),
20            active: true, // API does not expose an active flag
21            balances: vec![
22                Balance {
23                    balance: raw.usd_balance.unwrap_or(0.0),
24                    currency: "USD".into(),
25                    symbol: "$".into(),
26                },
27                Balance {
28                    balance: raw.try_balance.unwrap_or(0.0),
29                    currency: "TL".into(),
30                    symbol: "TL".into(),
31                },
32            ],
33        })
34    }
35
36    /// Fetch the balance for a single currency (`"USD"`, `"TRY"`, `"EUR"`, `"GBP"`).
37    pub async fn get_current_balance(&self, currency_id: &str) -> DnaResult<CurrentBalance> {
38        let currency_upper = currency_id.to_uppercase();
39        let balance_key = format!("{}Balance", currency_id.to_lowercase());
40        let query = [("currency", currency_upper.as_str())];
41
42        // Deserialise as Value so we can handle any currency key dynamically.
43        let raw: Value = self.http.get("deposit/accounts/me", Some(&query)).await?;
44        let balance = raw.get(&balance_key).and_then(Value::as_f64).unwrap_or(0.0);
45
46        let (cur_id, cur_name, cur_symbol) = currency_meta(&currency_upper);
47
48        Ok(CurrentBalance {
49            balance,
50            currency_id: cur_id,
51            currency_name: cur_name.into(),
52            currency_symbol: cur_symbol.into(),
53        })
54    }
55}