fmp_rs/endpoints/
economics.rs1use crate::{
4 client::FmpClient,
5 error::Result,
6 models::economics::{EconomicIndicator, MarketRiskPremium, TreasuryRate},
7};
8use serde::Serialize;
9
10pub struct Economics {
12 client: FmpClient,
13}
14
15impl Economics {
16 pub(crate) fn new(client: FmpClient) -> Self {
17 Self { client }
18 }
19
20 pub async fn get_treasury_rates(
45 &self,
46 from: Option<&str>,
47 to: Option<&str>,
48 ) -> Result<Vec<TreasuryRate>> {
49 #[derive(Serialize)]
50 struct Query<'a> {
51 #[serde(skip_serializing_if = "Option::is_none")]
52 from: Option<&'a str>,
53 #[serde(skip_serializing_if = "Option::is_none")]
54 to: Option<&'a str>,
55 apikey: &'a str,
56 }
57
58 let url = self.client.build_url("/treasury");
59 self.client
60 .get_with_query(
61 &url,
62 &Query {
63 from,
64 to,
65 apikey: self.client.api_key(),
66 },
67 )
68 .await
69 }
70
71 pub async fn get_gdp(&self) -> Result<Vec<EconomicIndicator>> {
89 #[derive(Serialize)]
90 struct Query<'a> {
91 apikey: &'a str,
92 }
93
94 let url = self.client.build_url("/economic");
95 self.client
96 .get_with_query(
97 &url,
98 &Query {
99 apikey: self.client.api_key(),
100 },
101 )
102 .await
103 }
104
105 pub async fn get_real_gdp(&self) -> Result<Vec<EconomicIndicator>> {
122 #[derive(Serialize)]
123 struct Query<'a> {
124 apikey: &'a str,
125 }
126
127 let url = self.client.build_url("/economic_indicator/GDP");
128 self.client
129 .get_with_query(
130 &url,
131 &Query {
132 apikey: self.client.api_key(),
133 },
134 )
135 .await
136 }
137
138 pub async fn get_gdp_per_capita(&self) -> Result<Vec<EconomicIndicator>> {
155 #[derive(Serialize)]
156 struct Query<'a> {
157 apikey: &'a str,
158 }
159
160 let url = self.client.build_url("/economic_indicator/GDP_PER_CAPITA");
161 self.client
162 .get_with_query(
163 &url,
164 &Query {
165 apikey: self.client.api_key(),
166 },
167 )
168 .await
169 }
170
171 pub async fn get_cpi(&self) -> Result<Vec<EconomicIndicator>> {
189 #[derive(Serialize)]
190 struct Query<'a> {
191 apikey: &'a str,
192 }
193
194 let url = self.client.build_url("/economic_indicator/CPI");
195 self.client
196 .get_with_query(
197 &url,
198 &Query {
199 apikey: self.client.api_key(),
200 },
201 )
202 .await
203 }
204
205 pub async fn get_inflation_rate(&self) -> Result<Vec<EconomicIndicator>> {
222 #[derive(Serialize)]
223 struct Query<'a> {
224 apikey: &'a str,
225 }
226
227 let url = self.client.build_url("/economic_indicator/INFLATION");
228 self.client
229 .get_with_query(
230 &url,
231 &Query {
232 apikey: self.client.api_key(),
233 },
234 )
235 .await
236 }
237
238 pub async fn get_unemployment_rate(&self) -> Result<Vec<EconomicIndicator>> {
255 #[derive(Serialize)]
256 struct Query<'a> {
257 apikey: &'a str,
258 }
259
260 let url = self.client.build_url("/economic_indicator/UNEMPLOYMENT");
261 self.client
262 .get_with_query(
263 &url,
264 &Query {
265 apikey: self.client.api_key(),
266 },
267 )
268 .await
269 }
270
271 pub async fn get_federal_funds_rate(&self) -> Result<Vec<EconomicIndicator>> {
288 #[derive(Serialize)]
289 struct Query<'a> {
290 apikey: &'a str,
291 }
292
293 let url = self
294 .client
295 .build_url("/economic_indicator/FEDERAL_FUNDS_RATE");
296 self.client
297 .get_with_query(
298 &url,
299 &Query {
300 apikey: self.client.api_key(),
301 },
302 )
303 .await
304 }
305
306 pub async fn get_market_risk_premium(&self) -> Result<Vec<MarketRiskPremium>> {
326 #[derive(Serialize)]
327 struct Query<'a> {
328 apikey: &'a str,
329 }
330
331 let url = self.client.build_url("/market_risk_premium");
332 self.client
333 .get_with_query(
334 &url,
335 &Query {
336 apikey: self.client.api_key(),
337 },
338 )
339 .await
340 }
341}
342
343#[cfg(test)]
344mod tests {
345 use super::*;
346
347 #[tokio::test]
349 #[ignore = "requires FMP API key"]
350 async fn test_get_treasury_rates() {
351 let client = FmpClient::new().unwrap();
352 let result = client.economics().get_treasury_rates(None, None).await;
353 assert!(result.is_ok());
354 let rates = result.unwrap();
355 assert!(!rates.is_empty());
356 assert!(rates[0].year_10.is_some());
357 }
358
359 #[tokio::test]
360 #[ignore = "requires FMP API key"]
361 async fn test_get_gdp() {
362 let client = FmpClient::new().unwrap();
363 let result = client.economics().get_gdp().await;
364 assert!(result.is_ok());
365 let gdp = result.unwrap();
366 assert!(!gdp.is_empty());
367 }
368
369 #[tokio::test]
370 #[ignore = "requires FMP API key"]
371 async fn test_get_real_gdp() {
372 let client = FmpClient::new().unwrap();
373 let result = client.economics().get_real_gdp().await;
374 assert!(result.is_ok());
375 let real_gdp = result.unwrap();
376 assert!(!real_gdp.is_empty());
377 }
378
379 #[tokio::test]
380 #[ignore = "requires FMP API key"]
381 async fn test_get_gdp_per_capita() {
382 let client = FmpClient::new().unwrap();
383 let result = client.economics().get_gdp_per_capita().await;
384 assert!(result.is_ok());
385 let gdp_pc = result.unwrap();
386 assert!(!gdp_pc.is_empty());
387 }
388
389 #[tokio::test]
390 #[ignore = "requires FMP API key"]
391 async fn test_get_cpi() {
392 let client = FmpClient::new().unwrap();
393 let result = client.economics().get_cpi().await;
394 assert!(result.is_ok());
395 let cpi = result.unwrap();
396 assert!(!cpi.is_empty());
397 }
398
399 #[tokio::test]
400 #[ignore = "requires FMP API key"]
401 async fn test_get_inflation_rate() {
402 let client = FmpClient::new().unwrap();
403 let result = client.economics().get_inflation_rate().await;
404 assert!(result.is_ok());
405 let inflation = result.unwrap();
406 assert!(!inflation.is_empty());
407 }
408
409 #[tokio::test]
410 #[ignore = "requires FMP API key"]
411 async fn test_get_unemployment_rate() {
412 let client = FmpClient::new().unwrap();
413 let result = client.economics().get_unemployment_rate().await;
414 assert!(result.is_ok());
415 let unemployment = result.unwrap();
416 assert!(!unemployment.is_empty());
417 }
418
419 #[tokio::test]
420 #[ignore = "requires FMP API key"]
421 async fn test_get_federal_funds_rate() {
422 let client = FmpClient::new().unwrap();
423 let result = client.economics().get_federal_funds_rate().await;
424 assert!(result.is_ok());
425 let fed_rate = result.unwrap();
426 assert!(!fed_rate.is_empty());
427 }
428
429 #[tokio::test]
430 #[ignore = "requires FMP API key"]
431 async fn test_get_market_risk_premium() {
432 let client = FmpClient::new().unwrap();
433 let result = client.economics().get_market_risk_premium().await;
434 assert!(result.is_ok());
435 let premiums = result.unwrap();
436 assert!(!premiums.is_empty());
437 }
438
439 #[tokio::test]
441 #[ignore = "requires FMP API key"]
442 async fn test_treasury_rates_with_dates() {
443 let client = FmpClient::new().unwrap();
444 let result = client
445 .economics()
446 .get_treasury_rates(Some("2024-01-01"), Some("2024-12-31"))
447 .await;
448 assert!(result.is_ok());
449 }
450
451 #[tokio::test]
453 async fn test_invalid_api_key() {
454 let client = FmpClient::builder()
455 .api_key("invalid_key_12345")
456 .build()
457 .unwrap();
458 let result = client.economics().get_gdp().await;
459 assert!(result.is_err());
460 }
461
462 #[tokio::test]
464 #[ignore = "requires FMP API key"]
465 async fn test_get_inflation_rate() {
466 let client = FmpClient::new().unwrap();
467 let result = client.economics().get_inflation_rate().await;
468 assert!(result.is_ok());
469 let inflation = result.unwrap();
470 assert!(!inflation.is_empty());
471 if let Some(value) = inflation[0].value {
473 assert!(value >= -10.0 && value <= 20.0); }
475 }
476
477 #[tokio::test]
478 #[ignore = "requires FMP API key"]
479 async fn test_get_unemployment_rate() {
480 let client = FmpClient::new().unwrap();
481 let result = client.economics().get_unemployment_rate().await;
482 assert!(result.is_ok());
483 let unemployment = result.unwrap();
484 assert!(!unemployment.is_empty());
485 if let Some(value) = unemployment[0].value {
487 assert!(value >= 0.0 && value <= 50.0); }
489 }
490
491 #[tokio::test]
492 #[ignore = "requires FMP API key"]
493 async fn test_get_federal_funds_rate() {
494 let client = FmpClient::new().unwrap();
495 let result = client.economics().get_federal_funds_rate().await;
496 assert!(result.is_ok());
497 let fed_rate = result.unwrap();
498 assert!(!fed_rate.is_empty());
499 if let Some(value) = fed_rate[0].value {
501 assert!(value >= 0.0 && value <= 25.0); }
503 }
504
505 #[tokio::test]
506 #[ignore = "requires FMP API key"]
507 async fn test_treasury_rates_invalid_dates() {
508 let client = FmpClient::new().unwrap();
509 let result = client
511 .economics()
512 .get_treasury_rates(Some("invalid-date"), Some("2024-12-31"))
513 .await;
514 match result {
516 Ok(_) => {} Err(_) => {} }
519 }
520
521 #[tokio::test]
522 #[ignore = "requires FMP API key"]
523 async fn test_treasury_rates_future_dates() {
524 let client = FmpClient::new().unwrap();
525 let result = client
527 .economics()
528 .get_treasury_rates(Some("2030-01-01"), Some("2030-12-31"))
529 .await;
530 assert!(result.is_ok());
531 let rates = result.unwrap();
532 assert!(rates.is_empty() || !rates.is_empty());
534 }
535}