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