finance_query/adapters/polygon/
mod.rs1mod client;
29pub(crate) mod models;
30
31mod chart; mod corporate; mod discovery; mod economic; mod filings; mod fundamentals; mod quote; mod crypto; mod forex; mod futures; mod indices; pub(crate) mod options; pub(crate) mod websocket;
48
49use crate::adapters::singleton::{provider_build_client, provider_singleton_state};
50use crate::error::{FinanceError, Result};
51use std::time::Duration;
52
53fn invalid_page(field: &str) -> FinanceError {
54 FinanceError::ResponseStructureError {
55 field: field.into(),
56 context: "invalid stock provider response".into(),
57 }
58}
59
60pub use chart::*;
62pub use corporate::*;
63pub use discovery::*;
64pub use fundamentals::*;
65pub use options::snapshots::fetch_options_response;
66pub use quote::*;
67
68pub use crypto::aggregates::fetch_crypto_grouped_daily_response;
70pub use crypto::snapshots::*;
71pub use forex::aggregates::fetch_forex_grouped_daily_response;
72pub use forex::quotes::*;
73pub use futures::snapshots::*;
74pub use indices::snapshots::*;
75
76pub use economic::*;
78pub use filings::*;
79
80const PG_RATE_PER_SEC: f64 = 5.0;
81
82provider_singleton_state!(
83 name = PolygonSingleton,
84 static_name = PG_SINGLETON,
85 rate_const = PG_RATE_PER_SEC,
86 provider_key = "polygon",
87 already_init_reason = "Polygon client already initialized",
88);
89
90provider_build_client!(
91 name = PolygonSingleton,
92 static_name = PG_SINGLETON,
93 rate_const = PG_RATE_PER_SEC,
94 provider_key = "polygon",
95 env_var = "POLYGON_API_KEY",
96 env_missing_reason =
97 "POLYGON_API_KEY not set. Call polygon::init(key) or set POLYGON_API_KEY env var.",
98 builder = client::PolygonClientBuilder,
99 client_ty = client::PolygonClient,
100);
101
102#[allow(dead_code)] pub fn init(api_key: impl Into<String>) -> Result<()> {
111 init_with_timeout(api_key, Duration::from_secs(30))
112}
113
114#[allow(dead_code)] pub fn init_with_timeout(api_key: impl Into<String>, timeout: Duration) -> Result<()> {
117 set_singleton(api_key, timeout)
118}
119
120pub(crate) fn api_key() -> Result<String> {
122 if PG_SINGLETON.get().is_none() {
123 let _ = build_client()?;
124 }
125 PG_SINGLETON
126 .get()
127 .map(|s| s.api_key.clone())
128 .ok_or_else(|| FinanceError::InvalidParameter {
129 param: "polygon".to_string(),
130 reason: "Polygon not initialized. Call polygon::init(api_key) first.".to_string(),
131 })
132}
133
134#[cfg(test)]
135mod live_tests;
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[test]
142 fn test_init_errors_on_double_init() {
143 let _ = init("test-key-1");
144 let result = init("test-key-2");
145 assert!(matches!(result, Err(FinanceError::InvalidParameter { .. })));
146 }
147}