mod client;
pub(crate) mod models;
mod chart; mod corporate; mod discovery; mod economic; mod filings; mod fundamentals; mod market; mod quote; mod sentiment; mod technicals;
mod crypto; mod forex; mod futures; mod indices; mod options;
pub(crate) mod websocket;
use crate::adapters::singleton::{provider_build_client, provider_singleton_state};
use crate::error::{FinanceError, Result};
use std::time::Duration;
pub use chart::*;
pub use corporate::*;
pub use discovery::*;
pub use fundamentals::*;
pub use options::snapshots::fetch_options_response;
pub use quote::*;
pub use crypto::snapshots::*;
pub use forex::quotes::*;
pub use futures::snapshots::*;
pub use indices::snapshots::*;
pub use economic::*;
pub use filings::*;
const PG_RATE_PER_SEC: f64 = 5.0;
provider_singleton_state!(
name = PolygonSingleton,
static_name = PG_SINGLETON,
rate_const = PG_RATE_PER_SEC,
provider_key = "polygon",
already_init_reason = "Polygon client already initialized",
);
provider_build_client!(
name = PolygonSingleton,
static_name = PG_SINGLETON,
rate_const = PG_RATE_PER_SEC,
provider_key = "polygon",
env_var = "POLYGON_API_KEY",
env_missing_reason =
"POLYGON_API_KEY not set. Call polygon::init(key) or set POLYGON_API_KEY env var.",
builder = client::PolygonClientBuilder,
client_ty = client::PolygonClient,
);
#[allow(dead_code)]
pub fn init(api_key: impl Into<String>) -> Result<()> {
init_with_timeout(api_key, Duration::from_secs(30))
}
#[allow(dead_code)]
pub fn init_with_timeout(api_key: impl Into<String>, timeout: Duration) -> Result<()> {
set_singleton(api_key, timeout)
}
pub(crate) fn api_key() -> Result<String> {
PG_SINGLETON
.get()
.map(|s| s.api_key.clone())
.ok_or_else(|| FinanceError::InvalidParameter {
param: "polygon".to_string(),
reason: "Polygon not initialized. Call polygon::init(api_key) first.".to_string(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_init_errors_on_double_init() {
let _ = init("test-key-1");
let result = init("test-key-2");
assert!(matches!(result, Err(FinanceError::InvalidParameter { .. })));
}
}