1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::sync::Arc;
use std::time::Duration;
use crate::{
auth::Auth,
corporate_actions::CorporateActionsClient,
crypto::CryptoClient,
env,
error::Error,
news::NewsClient,
options::OptionsClient,
stocks::StocksClient,
transport::{
http::HttpClient,
observer::{ObserverHandle, TransportObserver},
rate_limit::RateLimiter,
retry::RetryConfig,
},
};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
/// Root async client for Alpaca Market Data HTTP APIs.
///
/// Build a client once, then obtain resource clients with [`Self::stocks`],
/// [`Self::options`], [`Self::crypto`], [`Self::news`], and
/// [`Self::corporate_actions`].
///
/// # Examples
///
/// ```no_run
/// use alpaca_data::Client;
///
/// let client = Client::builder().build()?;
/// # let _ = client;
/// # Ok::<(), alpaca_data::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct Client {
pub(crate) inner: Arc<Inner>,
}
#[allow(dead_code)]
#[derive(Debug)]
pub(crate) struct Inner {
pub(crate) auth: Auth,
pub(crate) base_url: String,
pub(crate) timeout: Option<Duration>,
pub(crate) retry_config: RetryConfig,
pub(crate) max_in_flight: Option<usize>,
pub(crate) http: HttpClient,
}
#[derive(Clone, Debug)]
pub struct ClientBuilder {
api_key: Option<String>,
secret_key: Option<String>,
base_url: Option<String>,
timeout: Option<Duration>,
reqwest_client: Option<reqwest::Client>,
observer: Option<ObserverHandle>,
retry_config: RetryConfig,
max_in_flight: Option<usize>,
}
impl Client {
/// Builds a client with default runtime settings and no credentials.
///
/// This is useful for the currently implemented public crypto endpoints.
pub fn new() -> Self {
Self::builder()
.build()
.expect("client builder is infallible during bootstrap")
}
/// Starts a [`ClientBuilder`] for explicit runtime configuration.
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
/// Returns the stocks resource client.
pub fn stocks(&self) -> StocksClient {
StocksClient::new(self.inner.clone())
}
/// Returns the options resource client.
pub fn options(&self) -> OptionsClient {
OptionsClient::new(self.inner.clone())
}
/// Returns the crypto resource client.
pub fn crypto(&self) -> CryptoClient {
CryptoClient::new(self.inner.clone())
}
/// Returns the news resource client.
pub fn news(&self) -> NewsClient {
NewsClient::new(self.inner.clone())
}
/// Returns the corporate actions resource client.
pub fn corporate_actions(&self) -> CorporateActionsClient {
CorporateActionsClient::new(self.inner.clone())
}
pub(crate) fn from_parts(
auth: Auth,
base_url: String,
timeout: Option<Duration>,
reqwest_client: Option<reqwest::Client>,
observer: Option<ObserverHandle>,
retry_config: RetryConfig,
max_in_flight: Option<usize>,
) -> Result<Self, Error> {
let http = match reqwest_client {
Some(client) => HttpClient::with_client(
client,
observer,
retry_config.clone(),
RateLimiter::new(max_in_flight),
),
None => HttpClient::from_timeout(
timeout.unwrap_or(DEFAULT_TIMEOUT),
observer,
retry_config.clone(),
RateLimiter::new(max_in_flight),
)?,
};
Ok(Self {
inner: Arc::new(Inner {
auth,
base_url,
timeout,
retry_config,
max_in_flight,
http,
}),
})
}
}
impl Default for ClientBuilder {
fn default() -> Self {
Self {
api_key: None,
secret_key: None,
base_url: None,
timeout: None,
reqwest_client: None,
observer: None,
retry_config: RetryConfig::default(),
max_in_flight: None,
}
}
}
impl ClientBuilder {
/// Sets the Alpaca API key.
pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into());
self
}
/// Sets the Alpaca API secret key.
pub fn secret_key(mut self, secret_key: impl Into<String>) -> Self {
self.secret_key = Some(secret_key.into());
self
}
/// Overrides the default data API base URL.
pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = Some(base_url.into());
self
}
/// Sets the request timeout for the internally constructed `reqwest::Client`.
///
/// Building fails if `reqwest_client(...)` is also used because the injected
/// client owns its own timeout configuration.
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
/// Injects a preconfigured `reqwest::Client` for advanced transport tuning.
///
/// The injected client owns reqwest-level behavior such as connection
/// pooling, proxy behavior, default headers, and timeout settings. Build
/// validation rejects conflicting builder settings such as `timeout(...)`.
pub fn reqwest_client(mut self, reqwest_client: reqwest::Client) -> Self {
self.reqwest_client = Some(reqwest_client);
self
}
/// Registers an immutable observer for successful transport responses.
///
/// Observers receive endpoint metadata only. They cannot change request
/// execution or response shaping.
pub fn observer(mut self, observer: Arc<dyn TransportObserver>) -> Self {
self.observer = Some(ObserverHandle::new(observer));
self
}
/// Sets the maximum number of retry attempts for one request.
///
/// This applies to server-error retries by default. HTTP 429 retries only
/// participate when [`Self::retry_on_429`] is enabled.
pub fn max_retries(mut self, max_retries: u32) -> Self {
self.retry_config.max_retries = max_retries;
self
}
/// Enables or disables automatic retries on HTTP 429 responses.
///
/// This setting is disabled by default and affects only 429 responses. It
/// does not automatically enable honoring `Retry-After`.
pub fn retry_on_429(mut self, retry_on_429: bool) -> Self {
self.retry_config.retry_on_429 = retry_on_429;
self
}
/// Enables or disables honoring the `Retry-After` response header.
///
/// This setting only participates when 429 retries are enabled with
/// [`Self::retry_on_429`]. If a 429 response omits `Retry-After`, the
/// transport falls back to the configured backoff schedule.
pub fn respect_retry_after(mut self, respect_retry_after: bool) -> Self {
self.retry_config.respect_retry_after = respect_retry_after;
self
}
/// Sets the base retry backoff used by the shared HTTP transport.
pub fn base_backoff(mut self, base_backoff: Duration) -> Self {
self.retry_config.base_backoff = base_backoff;
self
}
/// Sets the maximum retry backoff used by the shared HTTP transport.
pub fn max_backoff(mut self, max_backoff: Duration) -> Self {
self.retry_config.max_backoff = max_backoff;
self
}
/// Adds a bounded random delay on top of each computed retry wait.
///
/// Jitter helps concurrent callers avoid retrying in lockstep. The
/// transport keeps the added delay within the configured retry budget and
/// maximum backoff constraints.
pub fn retry_jitter(mut self, retry_jitter: Duration) -> Self {
self.retry_config.jitter = Some(retry_jitter);
self
}
/// Sets an optional elapsed-time budget for one request's retry loop.
///
/// The transport subtracts the request's retry-loop elapsed time from this
/// budget before each retry decision. The remaining budget then caps each
/// scheduled retry wait, including waits derived from `Retry-After` and
/// waits with jitter enabled.
pub fn total_retry_budget(mut self, total_retry_budget: Duration) -> Self {
self.retry_config.total_retry_budget = Some(total_retry_budget);
self
}
/// Loads credentials from `APCA_API_KEY_ID` and `APCA_API_SECRET_KEY`.
///
/// If both variables are unset, the builder is left unchanged. If only one
/// side is set, this returns [`Error::InvalidConfiguration`].
pub fn credentials_from_env(self) -> Result<Self, Error> {
self.credentials_from_env_names(env::DEFAULT_API_KEY_ENV, env::DEFAULT_SECRET_KEY_ENV)
}
/// Loads credentials from the provided environment variable names.
///
/// If both variables are unset, the builder is left unchanged. If only one
/// side is set, this returns [`Error::InvalidConfiguration`].
pub fn credentials_from_env_names(
mut self,
api_key_var: &str,
secret_key_var: &str,
) -> Result<Self, Error> {
if let Some((api_key, secret_key)) =
env::credentials_from_env_names(api_key_var, secret_key_var)?
{
self.api_key = Some(api_key);
self.secret_key = Some(secret_key);
}
Ok(self)
}
/// Sets the maximum number of concurrent in-flight requests.
pub fn max_in_flight(mut self, max_in_flight: usize) -> Self {
self.max_in_flight = Some(max_in_flight);
self
}
/// Validates configuration and builds a [`Client`].
///
/// Credentials must be provided as a pair or omitted as a pair.
pub fn build(self) -> Result<Client, Error> {
if self.retry_config.max_backoff < self.retry_config.base_backoff {
return Err(Error::InvalidConfiguration(
"max_backoff must be greater than or equal to base_backoff".into(),
));
}
if self.reqwest_client.is_some() && self.timeout.is_some() {
return Err(Error::InvalidConfiguration(
"reqwest_client owns timeout configuration; remove timeout(...) or configure timeout on the injected reqwest::Client".into(),
));
}
let auth = Auth::new(self.api_key, self.secret_key)?;
let base_url = self
.base_url
.unwrap_or_else(|| "https://data.alpaca.markets".to_string());
Client::from_parts(
auth,
base_url,
self.timeout,
self.reqwest_client,
self.observer,
self.retry_config,
self.max_in_flight,
)
}
}