Skip to main content

deepseek_sdk/
balance.rs

1//! User balance API.
2//!
3//! Maps to `GET /user/balance`.
4use crate::DeepSeekClient;
5use crate::DeepSeekError;
6use crate::api_get;
7use serde::Deserialize;
8
9/// Account balance response.
10#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
11pub struct Balance {
12    /// Whether the user's balance is sufficient for API calls.
13    pub is_available: bool,
14    pub balance_infos: Vec<BalanceInfo>,
15}
16
17/// Balance entry by currency.
18#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
19pub struct BalanceInfo {
20    /// Possible values: [CNY, USD]
21    ///
22    /// The currency of the balance.
23    pub currency: String,
24
25    /// The total available balance, including the granted balance and the topped-up balance.
26    pub total_balance: String,
27
28    /// The total not expired granted balance.
29    pub granted_balance: String,
30
31    /// The total topped-up balance.
32    pub topped_up_balance: String,
33}
34
35impl Balance {
36    /// Fetch account balance.
37    pub async fn get(client: DeepSeekClient) -> Result<Self, DeepSeekError> {
38        api_get("/user/balance", client).await
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::DEFAULT_BASE_URL;
46    fn get_client() -> DeepSeekClient {
47        DeepSeekClient::new(
48            std::env::var("DEEPSEEK_API").expect("DEEPSEEK_API is not set"),
49            DEFAULT_BASE_URL.clone(),
50        )
51    }
52
53    #[tokio::test]
54    async fn test_get_balance() {
55        let client = get_client();
56        let balance = Balance::get(client).await.unwrap();
57        println!("{:#?}", balance);
58        assert!(balance.is_available);
59        assert!(!balance.balance_infos.is_empty());
60    }
61}