noah_sdk/api/
balances.rs

1//! Balances API
2//!
3//! This module provides functionality to retrieve account balances from the Noah API.
4//!
5//! # Examples
6//!
7//! ## Async Example
8//!
9//! ```no_run
10//! use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
11//!
12//! # #[cfg(feature = "async")]
13//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
14//! let config = Config::new(Environment::Sandbox);
15//! let auth = AuthConfig::with_api_key("your-api-key".to_string());
16//! let client = NoahClient::new(config, auth)?;
17//!
18//! // Get first page of balances
19//! let balances = client.get_balances(None, None).await?;
20//! println!("Balances: {:?}", balances);
21//!
22//! // Get next page if available
23//! if let Some(next_token) = balances.next_page_token {
24//!     let next_page = client.get_balances(Some(20), Some(&next_token)).await?;
25//!     println!("Next page: {:?}", next_page);
26//! }
27//! # Ok(())
28//! # }
29//! ```
30//!
31//! ## Blocking Example
32//!
33//! ```no_run
34//! use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
35//!
36//! # #[cfg(feature = "sync")]
37//! let config = Config::new(Environment::Sandbox);
38//! let auth = AuthConfig::with_api_key("your-api-key".to_string());
39//! let client = NoahClient::new(config, auth)?;
40//!
41//! // Get balances with pagination
42//! let balances = client.get_balances_blocking(Some(50), None)?;
43//! println!("Balances: {:?}", balances);
44//! ```
45
46use crate::client::NoahClient;
47use crate::error::Result;
48use crate::models::balances::GetBalancesResponse;
49
50impl NoahClient {
51    /// Get account balances with optional pagination
52    ///
53    /// Retrieves a list of balances for the authenticated account. Supports pagination
54    /// through `page_size` and `page_token` parameters.
55    ///
56    /// # Arguments
57    ///
58    /// * `page_size` - Optional number of items per page (defaults to API default if None)
59    /// * `page_token` - Optional token for pagination to get the next page of results
60    ///
61    /// # Returns
62    ///
63    /// Returns a [`GetBalancesResponse`] containing the list of balances and pagination information.
64    ///
65    /// # Errors
66    ///
67    /// This function will return an error if:
68    /// - The API request fails
69    /// - Authentication fails
70    /// - The response cannot be deserialized
71    ///
72    /// # Examples
73    ///
74    /// ```no_run
75    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
76    ///
77    /// # #[cfg(feature = "async")]
78    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
79    /// let config = Config::new(Environment::Sandbox);
80    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
81    /// let client = NoahClient::new(config, auth)?;
82    ///
83    /// // Get first page
84    /// let balances = client.get_balances(None, None).await?;
85    ///
86    /// // Get specific page size
87    /// let balances = client.get_balances(Some(100), None).await?;
88    /// # Ok(())
89    /// # }
90    /// ```
91    #[cfg(feature = "async")]
92    pub async fn get_balances(
93        &self,
94        page_size: Option<u32>,
95        page_token: Option<&str>,
96    ) -> Result<GetBalancesResponse> {
97        let mut path = "/balances".to_string();
98        let mut query_params = Vec::new();
99
100        if let Some(size) = page_size {
101            query_params.push(format!("PageSize={size}"));
102        }
103        if let Some(token) = page_token {
104            query_params.push(format!("PageToken={token}"));
105        }
106
107        if !query_params.is_empty() {
108            path.push('?');
109            path.push_str(&query_params.join("&"));
110        }
111
112        self.get(&path).await
113    }
114
115    /// Get account balances with optional pagination (blocking)
116    ///
117    /// Synchronous version of [`get_balances`](Self::get_balances). See that method for
118    /// detailed documentation.
119    ///
120    /// # Examples
121    ///
122    /// ```no_run
123    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
124    ///
125    /// # #[cfg(feature = "sync")]
126    /// let config = Config::new(Environment::Sandbox);
127    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
128    /// let client = NoahClient::new(config, auth)?;
129    ///
130    /// let balances = client.get_balances_blocking(Some(50), None)?;
131    /// println!("Balances: {:?}", balances);
132    /// ```
133    #[cfg(feature = "sync")]
134    pub fn get_balances_blocking(
135        &self,
136        page_size: Option<u32>,
137        page_token: Option<&str>,
138    ) -> Result<GetBalancesResponse> {
139        let mut path = "/balances".to_string();
140        let mut query_params = Vec::new();
141
142        if let Some(size) = page_size {
143            query_params.push(format!("PageSize={size}"));
144        }
145        if let Some(token) = page_token {
146            query_params.push(format!("PageToken={token}"));
147        }
148
149        if !query_params.is_empty() {
150            path.push('?');
151            path.push_str(&query_params.join("&"));
152        }
153
154        self.get_blocking(&path)
155    }
156}