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
53pub use chart::*;
55pub use corporate::*;
56pub use discovery::*;
57pub use fundamentals::*;
58pub use options::snapshots::fetch_options_response;
59pub use quote::*;
60
61pub use crypto::aggregates::fetch_crypto_grouped_daily_response;
63pub use crypto::snapshots::*;
64pub use forex::aggregates::fetch_forex_grouped_daily_response;
65pub use forex::quotes::*;
66pub use futures::snapshots::*;
67pub use indices::snapshots::*;
68
69pub use economic::*;
71pub use filings::*;
72
73const PG_RATE_PER_SEC: f64 = 5.0;
74
75provider_singleton_state!(
76 name = PolygonSingleton,
77 static_name = PG_SINGLETON,
78 rate_const = PG_RATE_PER_SEC,
79 provider_key = "polygon",
80 already_init_reason = "Polygon client already initialized",
81);
82
83provider_build_client!(
84 name = PolygonSingleton,
85 static_name = PG_SINGLETON,
86 rate_const = PG_RATE_PER_SEC,
87 provider_key = "polygon",
88 env_var = "POLYGON_API_KEY",
89 env_missing_reason =
90 "POLYGON_API_KEY not set. Call polygon::init(key) or set POLYGON_API_KEY env var.",
91 builder = client::PolygonClientBuilder,
92 client_ty = client::PolygonClient,
93);
94
95#[allow(dead_code)] pub fn init(api_key: impl Into<String>) -> Result<()> {
104 init_with_timeout(api_key, Duration::from_secs(30))
105}
106
107#[allow(dead_code)] pub fn init_with_timeout(api_key: impl Into<String>, timeout: Duration) -> Result<()> {
110 set_singleton(api_key, timeout)
111}
112
113pub(crate) fn api_key() -> Result<String> {
115 if PG_SINGLETON.get().is_none() {
116 let _ = build_client()?;
117 }
118 PG_SINGLETON
119 .get()
120 .map(|s| s.api_key.clone())
121 .ok_or_else(|| FinanceError::InvalidParameter {
122 param: "polygon".to_string(),
123 reason: "Polygon not initialized. Call polygon::init(api_key) first.".to_string(),
124 })
125}
126
127#[cfg(test)]
128mod live_tests;
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn test_init_errors_on_double_init() {
136 let _ = init("test-key-1");
137 let result = init("test-key-2");
138 assert!(matches!(result, Err(FinanceError::InvalidParameter { .. })));
139 }
140}