noah_sdk/config.rs
1//! Configuration for the Noah SDK
2//!
3//! This module provides configuration types for the Noah SDK client.
4//!
5//! # Examples
6//!
7//! ```no_run
8//! use noah_sdk::{Config, Environment};
9//!
10//! // Use sandbox environment
11//! let config = Config::new(Environment::Sandbox);
12//!
13//! // Use production environment
14//! let config = Config::new(Environment::Production);
15//!
16//! // Use custom URL
17//! let custom_url = url::Url::parse("https://api.example.com/v1").unwrap();
18//! let config = Config::new(Environment::Custom(custom_url));
19//!
20//! // Configure timeout and user agent
21//! let config = Config::new(Environment::Sandbox)
22//! .with_timeout(60)
23//! .with_user_agent("my-app/1.0".to_string())
24//! .with_logging(true);
25//! ```
26
27use url::Url;
28
29/// Environment configuration for the Noah API
30///
31/// Determines which API endpoint the client will connect to.
32///
33/// # Examples
34///
35/// ```no_run
36/// use noah_sdk::Environment;
37///
38/// // Use sandbox for testing
39/// let env = Environment::Sandbox;
40///
41/// // Use production
42/// let env = Environment::Production;
43///
44/// // Use custom endpoint
45/// let custom_url = url::Url::parse("https://api.example.com/v1").unwrap();
46/// let env = Environment::Custom(custom_url);
47/// ```
48#[derive(Debug, Clone, Default)]
49pub enum Environment {
50 /// Sandbox environment
51 #[default]
52 Sandbox,
53 /// Production environment
54 Production,
55 /// Custom base URL
56 Custom(Url),
57}
58
59impl Environment {
60 /// Get the base URL for the environment
61 ///
62 /// Returns the base URL that will be used for API requests.
63 ///
64 /// # Examples
65 ///
66 /// ```no_run
67 /// use noah_sdk::Environment;
68 ///
69 /// let env = Environment::Sandbox;
70 /// let url = env.base_url();
71 /// println!("Base URL: {}", url);
72 /// ```
73 pub fn base_url(&self) -> Url {
74 match self {
75 Environment::Sandbox => {
76 Url::parse("https://api.sandbox.noah.com/v1").expect("Invalid sandbox URL")
77 }
78 Environment::Production => {
79 Url::parse("https://api.noah.com/v1").expect("Invalid production URL")
80 }
81 Environment::Custom(url) => url.clone(),
82 }
83 }
84}
85
86/// Client configuration
87///
88/// Configuration options for the Noah SDK client, including base URL, timeout,
89/// user agent, and logging settings.
90///
91/// # Examples
92///
93/// ```no_run
94/// use noah_sdk::{Config, Environment};
95///
96/// // Default configuration
97/// let config = Config::new(Environment::Sandbox);
98///
99/// // Custom configuration
100/// let config = Config::new(Environment::Production)
101/// .with_timeout(60)
102/// .with_user_agent("my-app/1.0".to_string())
103/// .with_logging(true);
104/// ```
105#[derive(Debug, Clone)]
106pub struct Config {
107 /// Base URL for API requests
108 pub base_url: Url,
109 /// Request timeout in seconds
110 pub timeout_secs: u64,
111 /// User agent string
112 pub user_agent: String,
113 /// Enable request/response logging
114 pub enable_logging: bool,
115}
116
117impl Config {
118 /// Create a new configuration with default values
119 ///
120 /// Creates a configuration with:
121 /// - Base URL from the environment
122 /// - 30 second timeout
123 /// - Default user agent (includes SDK version)
124 /// - Logging disabled
125 ///
126 /// # Arguments
127 ///
128 /// * `environment` - The environment to use (Sandbox, Production, or Custom)
129 ///
130 /// # Examples
131 ///
132 /// ```no_run
133 /// use noah_sdk::{Config, Environment};
134 ///
135 /// let config = Config::new(Environment::Sandbox);
136 /// ```
137 pub fn new(environment: Environment) -> Self {
138 Self {
139 base_url: environment.base_url(),
140 timeout_secs: 30,
141 user_agent: format!("noah-sdk/{}", env!("CARGO_PKG_VERSION")),
142 enable_logging: false,
143 }
144 }
145
146 /// Set the request timeout in seconds
147 ///
148 /// # Arguments
149 ///
150 /// * `timeout_secs` - Timeout in seconds
151 ///
152 /// # Examples
153 ///
154 /// ```no_run
155 /// use noah_sdk::{Config, Environment};
156 ///
157 /// let config = Config::new(Environment::Sandbox)
158 /// .with_timeout(60); // 60 second timeout
159 /// ```
160 pub fn with_timeout(mut self, timeout_secs: u64) -> Self {
161 self.timeout_secs = timeout_secs;
162 self
163 }
164
165 /// Set a custom user agent string
166 ///
167 /// # Arguments
168 ///
169 /// * `user_agent` - Custom user agent string
170 ///
171 /// # Examples
172 ///
173 /// ```no_run
174 /// use noah_sdk::{Config, Environment};
175 ///
176 /// let config = Config::new(Environment::Sandbox)
177 /// .with_user_agent("my-app/1.0".to_string());
178 /// ```
179 pub fn with_user_agent(mut self, user_agent: String) -> Self {
180 self.user_agent = user_agent;
181 self
182 }
183
184 /// Enable or disable request/response logging
185 ///
186 /// # Arguments
187 ///
188 /// * `enable` - Whether to enable logging
189 ///
190 /// # Examples
191 ///
192 /// ```no_run
193 /// use noah_sdk::{Config, Environment};
194 ///
195 /// let config = Config::new(Environment::Sandbox)
196 /// .with_logging(true);
197 /// ```
198 pub fn with_logging(mut self, enable: bool) -> Self {
199 self.enable_logging = enable;
200 self
201 }
202}
203
204impl Default for Config {
205 fn default() -> Self {
206 Self::new(Environment::default())
207 }
208}