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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Economics and indicators endpoints
use crate::{
client::FmpClient,
error::Result,
models::economics::{EconomicIndicator, MarketRiskPremium, TreasuryRate},
};
use serde::Serialize;
/// Economics API endpoints
pub struct Economics {
client: FmpClient,
}
impl Economics {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Get treasury rates
///
/// Returns US treasury rates for various maturities (1mo to 30yr).
///
/// # Arguments
/// * `from` - Start date (optional, format: YYYY-MM-DD)
/// * `to` - End date (optional, format: YYYY-MM-DD)
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let rates = client.economics().get_treasury_rates(None, None).await?;
/// for rate in rates.iter().take(5) {
/// println!("{}: 10Y = {:.2}%, 2Y = {:.2}%",
/// rate.date,
/// rate.year_10.unwrap_or(0.0),
/// rate.year_2.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_treasury_rates(
&self,
from: Option<&str>,
to: Option<&str>,
) -> Result<Vec<TreasuryRate>> {
#[derive(Serialize)]
struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
from: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
to: Option<&'a str>,
apikey: &'a str,
}
let url = self.client.build_url("/treasury");
self.client
.get_with_query(
&url,
&Query {
from,
to,
apikey: self.client.api_key(),
},
)
.await
}
/// Get GDP (Gross Domestic Product)
///
/// Returns historical GDP data.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let gdp = client.economics().get_gdp().await?;
/// for record in gdp.iter().take(5) {
/// println!("{}: ${:.2}T", record.date, record.value.unwrap_or(0.0) / 1_000_000_000_000.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_gdp(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get Real GDP
///
/// Returns inflation-adjusted GDP data.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let real_gdp = client.economics().get_real_gdp().await?;
/// println!("Latest Real GDP: ${:.2}T",
/// real_gdp.first().and_then(|r| r.value).unwrap_or(0.0) / 1_000_000_000_000.0);
/// # Ok(())
/// # }
/// ```
pub async fn get_real_gdp(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic_indicator/GDP");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get GDP per capita
///
/// Returns GDP per capita (GDP divided by population).
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let gdp_per_capita = client.economics().get_gdp_per_capita().await?;
/// println!("Latest GDP per capita: ${:.2}",
/// gdp_per_capita.first().and_then(|r| r.value).unwrap_or(0.0));
/// # Ok(())
/// # }
/// ```
pub async fn get_gdp_per_capita(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic_indicator/GDP_PER_CAPITA");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get CPI (Consumer Price Index)
///
/// Returns historical CPI data used to measure inflation.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let cpi = client.economics().get_cpi().await?;
/// for record in cpi.iter().take(12) {
/// println!("{}: CPI = {:.2}", record.date, record.value.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_cpi(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic_indicator/CPI");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get inflation rate
///
/// Returns year-over-year inflation rate (percentage change in CPI).
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let inflation = client.economics().get_inflation_rate().await?;
/// println!("Current inflation: {:.2}%",
/// inflation.first().and_then(|r| r.value).unwrap_or(0.0));
/// # Ok(())
/// # }
/// ```
pub async fn get_inflation_rate(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic_indicator/INFLATION");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get unemployment rate
///
/// Returns historical unemployment rate data.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let unemployment = client.economics().get_unemployment_rate().await?;
/// println!("Current unemployment: {:.1}%",
/// unemployment.first().and_then(|r| r.value).unwrap_or(0.0));
/// # Ok(())
/// # }
/// ```
pub async fn get_unemployment_rate(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/economic_indicator/UNEMPLOYMENT");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get federal funds rate
///
/// Returns the federal funds effective rate set by the Federal Reserve.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let fed_rate = client.economics().get_federal_funds_rate().await?;
/// println!("Current Fed rate: {:.2}%",
/// fed_rate.first().and_then(|r| r.value).unwrap_or(0.0));
/// # Ok(())
/// # }
/// ```
pub async fn get_federal_funds_rate(&self) -> Result<Vec<EconomicIndicator>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url("/economic_indicator/FEDERAL_FUNDS_RATE");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get market risk premium
///
/// Returns equity risk premium data by country.
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let risk_premiums = client.economics().get_market_risk_premium().await?;
/// for premium in risk_premiums.iter().take(10) {
/// println!("{}: Total ERP = {:.2}%",
/// premium.country,
/// premium.total_equity_risk_premium.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_market_risk_premium(&self) -> Result<Vec<MarketRiskPremium>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url("/market_risk_premium");
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
// Golden path tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_treasury_rates() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_treasury_rates(None, None).await;
assert!(result.is_ok());
let rates = result.unwrap();
assert!(!rates.is_empty());
assert!(rates[0].year_10.is_some());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_gdp() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_gdp().await;
assert!(result.is_ok());
let gdp = result.unwrap();
assert!(!gdp.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_real_gdp() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_real_gdp().await;
assert!(result.is_ok());
let real_gdp = result.unwrap();
assert!(!real_gdp.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_gdp_per_capita() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_gdp_per_capita().await;
assert!(result.is_ok());
let gdp_pc = result.unwrap();
assert!(!gdp_pc.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_cpi() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_cpi().await;
assert!(result.is_ok());
let cpi = result.unwrap();
assert!(!cpi.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_inflation_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_inflation_rate().await;
assert!(result.is_ok());
let inflation = result.unwrap();
assert!(!inflation.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_unemployment_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_unemployment_rate().await;
assert!(result.is_ok());
let unemployment = result.unwrap();
assert!(!unemployment.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_federal_funds_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_federal_funds_rate().await;
assert!(result.is_ok());
let fed_rate = result.unwrap();
assert!(!fed_rate.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_market_risk_premium() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_market_risk_premium().await;
assert!(result.is_ok());
let premiums = result.unwrap();
assert!(!premiums.is_empty());
}
// Edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_treasury_rates_with_dates() {
let client = FmpClient::new().unwrap();
let result = client
.economics()
.get_treasury_rates(Some("2024-01-01"), Some("2024-12-31"))
.await;
assert!(result.is_ok());
}
// Error handling tests
#[tokio::test]
async fn test_invalid_api_key() {
let client = FmpClient::builder()
.api_key("invalid_key_12345")
.build()
.unwrap();
let result = client.economics().get_gdp().await;
assert!(result.is_err());
}
// Additional edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_inflation_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_inflation_rate().await;
assert!(result.is_ok());
let inflation = result.unwrap();
assert!(!inflation.is_empty());
// Inflation rate should be a percentage
if let Some(value) = inflation[0].value {
assert!(value >= -10.0 && value <= 20.0); // Reasonable range for inflation
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_unemployment_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_unemployment_rate().await;
assert!(result.is_ok());
let unemployment = result.unwrap();
assert!(!unemployment.is_empty());
// Unemployment rate should be a reasonable percentage
if let Some(value) = unemployment[0].value {
assert!(value >= 0.0 && value <= 50.0); // Reasonable range
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_federal_funds_rate() {
let client = FmpClient::new().unwrap();
let result = client.economics().get_federal_funds_rate().await;
assert!(result.is_ok());
let fed_rate = result.unwrap();
assert!(!fed_rate.is_empty());
// Fed funds rate should be a reasonable percentage
if let Some(value) = fed_rate[0].value {
assert!(value >= 0.0 && value <= 25.0); // Reasonable range
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_treasury_rates_invalid_dates() {
let client = FmpClient::new().unwrap();
// Test with invalid date format
let result = client
.economics()
.get_treasury_rates(Some("invalid-date"), Some("2024-12-31"))
.await;
// Should handle invalid dates gracefully
match result {
Ok(_) => {} // API might handle it gracefully
Err(_) => {} // Or return an error, both are acceptable
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_treasury_rates_future_dates() {
let client = FmpClient::new().unwrap();
// Test with future dates
let result = client
.economics()
.get_treasury_rates(Some("2030-01-01"), Some("2030-12-31"))
.await;
assert!(result.is_ok());
let rates = result.unwrap();
// Future dates should return empty or current data
assert!(rates.is_empty() || !rates.is_empty());
}
}