#[cfg(feature = "dotenvy")]
use crate::error::{Error, Result};
use crate::request::Request;
#[cfg(feature = "reqwest")]
pub struct Polygon<Client: Request = reqwest::Client> {
client: Client,
api_key: Option<String>,
}
#[cfg(not(feature = "reqwest"))]
pub struct Polygon<Client: Request> {
client: Client,
api_key: Option<String>,
}
impl<Client: Request> Polygon<Client> {
#[cfg(feature = "dotenvy")]
pub fn new() -> Result<Self> {
dotenvy::dotenv().ok();
let api_key = std::env::var("POLYGON_API_KEY").map_err(|_| Error::MissingApiKey)?;
Ok(Self {
client: Client::new(),
api_key: Some(api_key),
})
}
#[cfg(feature = "dotenvy")]
pub fn with_client(client: Client) -> Result<Self> {
dotenvy::dotenv().ok();
let api_key = std::env::var("POLYGON_API_KEY").map_err(|_| Error::MissingApiKey)?;
Ok(Self {
client,
api_key: Some(api_key),
})
}
#[cfg(not(feature = "dotenvy"))]
pub fn with_client(client: Client) -> Self {
Self {
client,
api_key: None,
}
}
pub fn with_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into());
self
}
pub fn api_key(&self) -> Option<&str> {
self.api_key.as_deref()
}
pub fn client(&self) -> &Client {
&self.client
}
}
#[cfg(feature = "reqwest")]
impl Default for Polygon<reqwest::Client> {
fn default() -> Self {
Self {
client: reqwest::Client::new(),
api_key: None,
}
}
}
#[cfg(not(feature = "reqwest"))]
impl<Client: Request> Default for Polygon<Client> {
fn default() -> Self {
Self {
client: Client::new(),
api_key: None,
}
}
}