square_rust/square_client.rs
1//! The SquareApiClient is the main entry point for making requests to the Square API
2
3use crate::api::customers::CustomersApi;
4use crate::api::models::objects::api_error::SquareApiError;
5use crate::config::SquareApiConfig;
6use crate::http::client::http_client::SquareHttpClient;
7
8/// The SquareApiClient is the main entry point for making requests to the Square API
9pub struct SquareApiClient {
10 pub customers: CustomersApi,
11}
12
13impl SquareApiClient {
14 /// Create a new SquareApiClient
15 /// # Example - Create a new SquareApiClient with Sandbox environment and default http client
16 /// ```
17 /// use square_rust::square_client::SquareApiClient;
18 /// use square_rust::config::SquareApiConfig;
19 /// use square_rust::environment::Environment;
20 ///
21 /// let config = SquareApiConfig::builder()
22 /// .environment(Environment::Sandbox)
23 /// .access_token("access_token".to_string())
24 /// .build();
25 /// ```
26 ///
27 /// # Example - Create a new SquareApiClient with Production environment and default http client
28 /// ```no_run
29 /// use square_rust::square_client::SquareApiClient;
30 /// use square_rust::config::SquareApiConfig;
31 /// use square_rust::environment::Environment;
32 ///
33 /// let config = SquareApiConfig::builder()
34 /// .environment(Environment::Production)
35 /// .access_token("access_token".to_string())
36 /// .build();
37 /// ```
38 pub fn try_new(config: Option<SquareApiConfig>) -> Result<Self, SquareApiError> {
39 let config = config.unwrap_or_else(|| SquareApiConfig::builder().build());
40 let http_client = SquareHttpClient::try_new(&config.http_client_config)?;
41 Ok(SquareApiClient {
42 customers: CustomersApi::new(config.clone(), http_client.clone()),
43 })
44 }
45}