Skip to main content

goldrush_sdk/
client.rs

1use crate::{Error, RateLimitConfig, CacheConfig, MetricsCollector, validation::Validator};
2use reqwest::Client as HttpClient;
3use std::sync::Arc;
4use std::time::Duration;
5
6/// Configuration options for the GoldRush client.
7#[derive(Debug, Clone)]
8pub struct ClientConfig {
9    /// Base URL for the GoldRush API.
10    /// TODO: Confirm exact base URL with maintainers - currently using docs example
11    pub base_url: String,
12    
13    /// Request timeout duration.
14    pub timeout: Duration,
15    
16    /// Maximum number of retry attempts for failed requests.
17    pub max_retries: u8,
18    
19    /// User agent string for requests.
20    pub user_agent: String,
21    
22    /// Rate limiting configuration.
23    pub rate_limit: RateLimitConfig,
24    
25    /// Caching configuration.
26    pub cache: CacheConfig,
27    
28    /// Enable request/response logging.
29    pub enable_logging: bool,
30    
31    /// Enable metrics collection.
32    pub enable_metrics: bool,
33    
34    /// Connection pool size.
35    pub connection_pool_size: usize,
36    
37    /// Keep-alive timeout for connections.
38    pub keep_alive_timeout: Duration,
39}
40
41impl Default for ClientConfig {
42    fn default() -> Self {
43        Self {
44            // TODO: Confirm exact base URL from official GoldRush docs
45            base_url: "https://api.goldrush.dev".to_string(),
46            timeout: Duration::from_secs(30),
47            max_retries: 3,
48            user_agent: format!("goldrush-sdk-rs/{}", env!("CARGO_PKG_VERSION")),
49            rate_limit: RateLimitConfig::default(),
50            cache: CacheConfig::default(),
51            enable_logging: true,
52            enable_metrics: true,
53            connection_pool_size: 10,
54            keep_alive_timeout: Duration::from_secs(90),
55        }
56    }
57}
58
59impl ClientConfig {
60    /// Create a new ClientConfig with custom base URL.
61    pub fn new<S: Into<String>>(base_url: S) -> Self {
62        Self {
63            base_url: base_url.into(),
64            ..Default::default()
65        }
66    }
67
68    /// Set the timeout for requests.
69    pub fn with_timeout(mut self, timeout: Duration) -> Self {
70        self.timeout = timeout;
71        self
72    }
73
74    /// Set the maximum number of retry attempts.
75    pub fn with_max_retries(mut self, max_retries: u8) -> Self {
76        self.max_retries = max_retries;
77        self
78    }
79
80    /// Set a custom user agent.
81    pub fn with_user_agent<S: Into<String>>(mut self, user_agent: S) -> Self {
82        self.user_agent = user_agent.into();
83        self
84    }
85}
86
87/// The main GoldRush client for interacting with the API.
88pub struct GoldRushClient {
89    pub(crate) http: HttpClient,
90    pub(crate) api_key: String,
91    pub(crate) config: ClientConfig,
92    pub(crate) metrics: Option<Arc<MetricsCollector>>,
93}
94
95impl GoldRushClient {
96    /// Create a new GoldRush client with the provided API key and configuration.
97    ///
98    /// # Arguments
99    ///
100    /// * `api_key` - Your GoldRush API key
101    /// * `config` - Client configuration options
102    ///
103    /// # Example
104    ///
105    /// ```rust,no_run
106    /// use goldrush_sdk::{GoldRushClient, ClientConfig};
107    ///
108    /// let client = GoldRushClient::new("your-api-key", ClientConfig::default())?;
109    /// # Ok::<(), goldrush_sdk::Error>(())
110    /// ```
111    pub fn new<S: Into<String>>(api_key: S, config: ClientConfig) -> Result<Self, Error> {
112        let api_key = api_key.into();
113        
114        // Validate API key
115        Validator::validate_api_key(&api_key)?;
116        
117        // Validate base URL
118        Validator::validate_url(&config.base_url)?;
119
120        let http = HttpClient::builder()
121            .user_agent(&config.user_agent)
122            .timeout(config.timeout)
123            .pool_max_idle_per_host(config.connection_pool_size)
124            .pool_idle_timeout(config.keep_alive_timeout)
125            .tcp_keepalive(Some(Duration::from_secs(60)))
126            .build()?;
127
128        let metrics = if config.enable_metrics {
129            Some(Arc::new(MetricsCollector::new()))
130        } else {
131            None
132        };
133
134        Ok(Self { 
135            http, 
136            api_key, 
137            config, 
138            metrics 
139        })
140    }
141
142    /// Create a new GoldRush client with default configuration.
143    ///
144    /// # Arguments
145    ///
146    /// * `api_key` - Your GoldRush API key
147    ///
148    /// # Example
149    ///
150    /// ```rust,no_run
151    /// use goldrush_sdk::GoldRushClient;
152    ///
153    /// let client = GoldRushClient::with_key("your-api-key")?;
154    /// # Ok::<(), goldrush_sdk::Error>(())
155    /// ```
156    pub fn with_key<S: Into<String>>(api_key: S) -> Result<Self, Error> {
157        Self::new(api_key, ClientConfig::default())
158    }
159
160    /// Get access to the metrics collector (if enabled).
161    pub fn metrics(&self) -> Option<&Arc<MetricsCollector>> {
162        self.metrics.as_ref()
163    }
164}