noah_sdk/api/
customers.rs

1//! Customers API
2//!
3//! This module provides functionality to manage customers in the Noah API.
4//!
5//! # Examples
6//!
7//! ```no_run
8//! use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
9//! use noah_sdk::models::customers::CustomerInput;
10//! use noah_sdk::models::common::CustomerID;
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//! // Create or update a customer
19//! let customer_id = CustomerID::from("customer-123");
20//! let customer_input = CustomerInput {
21//!     // ... set customer fields
22//! };
23//! client.create_or_update_customer(&customer_id, &customer_input).await?;
24//!
25//! // Retrieve customer
26//! let customer = client.get_customer(&customer_id).await?;
27//! println!("Customer: {:?}", customer);
28//! # Ok(())
29//! # }
30//! ```
31
32use crate::client::NoahClient;
33use crate::error::Result;
34use crate::models::common::*;
35use crate::models::customers::{Customer, CustomerInput, GetCustomersResponse};
36
37impl NoahClient {
38    /// Retrieve a customer by their ID
39    ///
40    /// # Arguments
41    ///
42    /// * `customer_id` - The unique identifier of the customer to retrieve
43    ///
44    /// # Returns
45    ///
46    /// Returns a [`Customer`] object containing the customer's information.
47    ///
48    /// # Errors
49    ///
50    /// This function will return an error if:
51    /// - The customer ID is invalid
52    /// - The customer does not exist
53    /// - The API request fails
54    ///
55    /// # Examples
56    ///
57    /// ```no_run
58    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
59    /// use noah_sdk::models::common::CustomerID;
60    ///
61    /// # #[cfg(feature = "async")]
62    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
63    /// let config = Config::new(Environment::Sandbox);
64    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
65    /// let client = NoahClient::new(config, auth)?;
66    ///
67    /// let customer_id = CustomerID::from("customer-123");
68    /// let customer = client.get_customer(&customer_id).await?;
69    /// # Ok(())
70    /// # }
71    /// ```
72    #[cfg(feature = "async")]
73    pub async fn get_customer(&self, customer_id: &CustomerID) -> Result<Customer> {
74        let path = format!("/customers/{customer_id}");
75        self.get(&path).await
76    }
77
78    /// Retrieve a customer by their ID (blocking)
79    ///
80    /// Synchronous version of [`get_customer`](Self::get_customer). See that method for
81    /// detailed documentation.
82    #[cfg(feature = "sync")]
83    pub fn get_customer_blocking(&self, customer_id: &CustomerID) -> Result<Customer> {
84        let path = format!("/customers/{customer_id}");
85        self.get_blocking(&path)
86    }
87
88    /// Create a new customer or update an existing one
89    ///
90    /// This method will create a customer if they don't exist, or update an existing customer
91    /// if they do. The customer ID is used as the unique identifier.
92    ///
93    /// # Arguments
94    ///
95    /// * `customer_id` - The unique identifier for the customer
96    /// * `customer` - The customer data to create or update
97    ///
98    /// # Returns
99    ///
100    /// Returns `Ok(())` if the operation succeeds.
101    ///
102    /// # Errors
103    ///
104    /// This function will return an error if:
105    /// - The customer data is invalid
106    /// - The API request fails
107    /// - Authentication fails
108    ///
109    /// # Examples
110    ///
111    /// ```no_run
112    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
113    /// use noah_sdk::models::customers::CustomerInput;
114    /// use noah_sdk::models::common::CustomerID;
115    ///
116    /// # #[cfg(feature = "async")]
117    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
118    /// let config = Config::new(Environment::Sandbox);
119    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
120    /// let client = NoahClient::new(config, auth)?;
121    ///
122    /// let customer_id = CustomerID::from("customer-123");
123    /// let customer = CustomerInput {
124    ///     // ... set customer fields
125    /// };
126    /// client.create_or_update_customer(&customer_id, &customer).await?;
127    /// # Ok(())
128    /// # }
129    /// ```
130    #[cfg(feature = "async")]
131    pub async fn create_or_update_customer(
132        &self,
133        customer_id: &CustomerID,
134        customer: &CustomerInput,
135    ) -> Result<()> {
136        let path = format!("/customers/{customer_id}");
137        let _response: Option<serde_json::Value> = self.put(&path, customer).await?;
138        Ok(())
139    }
140
141    /// Create a new customer or update an existing one (blocking)
142    ///
143    /// Synchronous version of [`create_or_update_customer`](Self::create_or_update_customer).
144    /// See that method for detailed documentation.
145    #[cfg(feature = "sync")]
146    pub fn create_or_update_customer_blocking(
147        &self,
148        customer_id: &CustomerID,
149        customer: &CustomerInput,
150    ) -> Result<()> {
151        let path = format!("/customers/{customer_id}");
152        let _response: Option<serde_json::Value> = self.put_blocking(&path, customer)?;
153        Ok(())
154    }
155
156    /// List customers with optional pagination and sorting
157    ///
158    /// Retrieves a paginated list of customers. Supports pagination and sorting options.
159    ///
160    /// # Arguments
161    ///
162    /// * `page_size` - Optional number of items per page
163    /// * `page_token` - Optional token for pagination to get the next page
164    /// * `sort_direction` - Optional sort direction (ascending or descending)
165    ///
166    /// # Returns
167    ///
168    /// Returns a [`GetCustomersResponse`] containing the list of customers and pagination info.
169    ///
170    /// # Examples
171    ///
172    /// ```no_run
173    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
174    /// use noah_sdk::models::common::SortDirection;
175    ///
176    /// # #[cfg(feature = "async")]
177    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
178    /// let config = Config::new(Environment::Sandbox);
179    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
180    /// let client = NoahClient::new(config, auth)?;
181    ///
182    /// // Get first page, sorted descending
183    /// let customers = client.get_customers(
184    ///     Some(50),
185    ///     None,
186    ///     Some(&SortDirection::Descending)
187    /// ).await?;
188    /// # Ok(())
189    /// # }
190    /// ```
191    #[cfg(feature = "async")]
192    pub async fn get_customers(
193        &self,
194        page_size: Option<u32>,
195        page_token: Option<&str>,
196        sort_direction: Option<&SortDirection>,
197    ) -> Result<GetCustomersResponse> {
198        let mut path = "/customers".to_string();
199        let mut query_params = Vec::new();
200
201        if let Some(size) = page_size {
202            query_params.push(format!("PageSize={size}"));
203        }
204        if let Some(token) = page_token {
205            query_params.push(format!("PageToken={token}"));
206        }
207        if let Some(sort) = sort_direction {
208            query_params.push(format!("SortDirection={sort:?}"));
209        }
210
211        if !query_params.is_empty() {
212            path.push('?');
213            path.push_str(&query_params.join("&"));
214        }
215
216        self.get(&path).await
217    }
218
219    /// List customers with optional pagination and sorting (blocking)
220    ///
221    /// Synchronous version of [`get_customers`](Self::get_customers). See that method for
222    /// detailed documentation.
223    #[cfg(feature = "sync")]
224    pub fn get_customers_blocking(
225        &self,
226        page_size: Option<u32>,
227        page_token: Option<&str>,
228        sort_direction: Option<&SortDirection>,
229    ) -> Result<GetCustomersResponse> {
230        let mut path = "/customers".to_string();
231        let mut query_params = Vec::new();
232
233        if let Some(size) = page_size {
234            query_params.push(format!("PageSize={size}"));
235        }
236        if let Some(token) = page_token {
237            query_params.push(format!("PageToken={token}"));
238        }
239        if let Some(sort) = sort_direction {
240            query_params.push(format!("SortDirection={sort:?}"));
241        }
242
243        if !query_params.is_empty() {
244            path.push('?');
245            path.push_str(&query_params.join("&"));
246        }
247
248        self.get_blocking(&path)
249    }
250}