use chrono::{DateTime, Utc};
use crate::block_on;
use crate::client::{Client as AsyncClient, DownloadResult, Ticker as AsyncTicker};
use crate::config::Config;
use crate::error::Result;
use crate::history::{History, HistoryMeta, HistoryOptions};
use crate::quote::{Calendar, FundsData, NamedTable, SecFiling, UpgradesDowngrades};
#[derive(Clone)]
pub struct Client {
inner: AsyncClient,
}
impl Client {
pub fn new() -> Result<Self> {
Ok(Self {
inner: AsyncClient::new()?,
})
}
pub fn with_config(config: Config) -> Result<Self> {
Ok(Self {
inner: AsyncClient::with_config(config)?,
})
}
pub fn history(&self, ticker: &str, opts: &HistoryOptions) -> Result<History> {
block_on(self.inner.history(ticker, opts))
}
pub fn download(&self, tickers: &[&str], opts: &HistoryOptions) -> Result<DownloadResult> {
block_on(crate::download(tickers, opts, &self.inner))
}
pub fn info(&self, ticker: &str) -> Result<crate::quote::Info> {
block_on(self.inner.info(ticker))
}
pub fn fast_info(&self, ticker: &str) -> Result<crate::quote::FastInfo> {
block_on(self.inner.fast_info(ticker))
}
pub fn holders(&self, ticker: &str) -> Result<crate::quote::Holders> {
block_on(self.inner.holders(ticker))
}
pub fn sustainability(&self, ticker: &str) -> Result<crate::quote::Sustainability> {
block_on(self.inner.sustainability(ticker))
}
pub fn analyst_price_targets(&self, ticker: &str) -> Result<crate::quote::AnalystPriceTargets> {
block_on(self.inner.analyst_price_targets(ticker))
}
pub fn recommendation_trend(
&self,
ticker: &str,
) -> Result<Vec<crate::quote::RecommendationTrend>> {
block_on(self.inner.recommendation_trend(ticker))
}
pub fn financials(
&self,
ticker: &str,
statement: crate::fundamentals::Statement,
freq: crate::fundamentals::Freq,
) -> Result<crate::fundamentals::Financials> {
block_on(self.inner.financials(ticker, statement, freq))
}
pub fn option_chain(&self, ticker: &str) -> Result<crate::options::OptionChain> {
block_on(self.inner.option_chain(ticker))
}
pub fn search(
&self,
query: &str,
quotes_count: usize,
news_count: usize,
) -> Result<crate::search::SearchResult> {
block_on(self.inner.search(query, quotes_count, news_count))
}
pub fn lookup(
&self,
query: &str,
limit: usize,
lookup_type: &str,
) -> Result<crate::lookup::LookupResult> {
block_on(self.inner.lookup(query, limit, lookup_type))
}
pub fn sector(&self, key: &str) -> Result<crate::domain::Sector> {
block_on(self.inner.sector(key))
}
pub fn industry(&self, key: &str) -> Result<crate::domain::Industry> {
block_on(self.inner.industry(key))
}
pub fn market(&self, region: crate::domain::MarketRegion) -> Result<crate::domain::Market> {
block_on(self.inner.market(region))
}
pub fn earnings_calendar(
&self,
start: &str,
end: &str,
limit: usize,
) -> Result<Vec<crate::calendars::EarningsEvent>> {
block_on(self.inner.earnings_calendar(start, end, limit))
}
pub fn ipo_calendar(
&self,
start: &str,
end: &str,
limit: usize,
) -> Result<Vec<crate::calendars::IpoEvent>> {
block_on(self.inner.ipo_calendar(start, end, limit))
}
pub fn economic_calendar(
&self,
start: &str,
end: &str,
limit: usize,
) -> Result<Vec<crate::calendars::EconomicEvent>> {
block_on(self.inner.economic_calendar(start, end, limit))
}
pub fn splits_calendar(
&self,
start: &str,
end: &str,
limit: usize,
) -> Result<Vec<crate::calendars::SplitEvent>> {
block_on(self.inner.splits_calendar(start, end, limit))
}
pub fn screen(
&self,
query: impl Into<crate::screener::ScreenerQuery>,
opts: &crate::screener::ScreenOptions,
) -> Result<crate::screener::ScreenerResult> {
block_on(self.inner.screen(query, opts))
}
pub fn resolve_isin(&self, isin: &str) -> Result<String> {
block_on(self.inner.resolve_isin(isin))
}
pub fn news(
&self,
ticker: &str,
count: usize,
tab: &str,
) -> Result<Vec<crate::news::NewsArticle>> {
block_on(self.inner.news(ticker, count, tab))
}
pub fn isin(&self, ticker: &str) -> Result<String> {
block_on(self.inner.isin(ticker))
}
pub fn earnings_dates(
&self,
ticker: &str,
limit: usize,
) -> Result<Vec<crate::earnings::EarningsDate>> {
block_on(self.inner.earnings_dates(ticker, limit))
}
pub fn earnings_estimate(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.earnings_estimate(ticker))
}
pub fn revenue_estimate(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.revenue_estimate(ticker))
}
pub fn earnings_history(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.earnings_history(ticker))
}
pub fn eps_trend(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.eps_trend(ticker))
}
pub fn eps_revisions(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.eps_revisions(ticker))
}
pub fn growth_estimates(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.growth_estimates(ticker))
}
pub fn recommendations(&self, ticker: &str) -> Result<Vec<crate::quote::RecommendationTrend>> {
block_on(self.inner.recommendations(ticker))
}
pub fn upgrades_downgrades(&self, ticker: &str) -> Result<Vec<UpgradesDowngrades>> {
block_on(self.inner.upgrades_downgrades(ticker))
}
pub fn valuation_measures(&self, ticker: &str) -> Result<NamedTable> {
block_on(self.inner.valuation_measures(ticker))
}
pub fn calendar(&self, ticker: &str) -> Result<Calendar> {
block_on(self.inner.calendar(ticker))
}
pub fn sec_filings(&self, ticker: &str) -> Result<Vec<SecFiling>> {
block_on(self.inner.sec_filings(ticker))
}
pub fn shares(&self, ticker: &str) -> Result<Option<f64>> {
block_on(self.inner.shares(ticker))
}
pub fn shares_full(
&self,
ticker: &str,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
) -> Result<Vec<(DateTime<Utc>, f64)>> {
block_on(self.inner.shares_full(ticker, start, end))
}
pub fn funds_data(&self, ticker: &str) -> Result<FundsData> {
block_on(self.inner.funds_data(ticker))
}
pub fn history_metadata(&self, ticker: &str) -> Result<HistoryMeta> {
block_on(self.inner.history_metadata(ticker))
}
pub fn ticker_from_mic(&self, symbol: &str, mic: &str) -> Result<Ticker> {
Ok(Ticker {
inner: AsyncTicker::from_mic(symbol, mic, self.inner.clone())?,
})
}
pub fn ticker_from_isin(&self, isin: &str) -> Result<Ticker> {
Ok(Ticker {
inner: block_on(AsyncTicker::from_isin(isin, self.inner.clone()))?,
})
}
pub fn ticker_from_id(&self, id: crate::TickerId) -> Result<Ticker> {
Ok(Ticker {
inner: block_on(AsyncTicker::from_id(id, self.inner.clone()))?,
})
}
pub fn auth(&self) -> crate::auth::Auth {
self.inner.auth()
}
pub fn live(&self) -> crate::live::LiveWebSocket {
self.inner.live()
}
pub fn stream_live<F>(&self, symbols: &[&str], handler: F) -> Result<()>
where
F: FnMut(crate::live::PricingData) + Send,
{
block_on(self.inner.live().stream(symbols, handler))
}
pub fn ticker(&self, symbol: &str) -> Ticker {
Ticker {
inner: AsyncTicker::new(symbol, self.inner.clone()),
}
}
}
impl Default for Client {
fn default() -> Self {
Self::new().expect("default blocking client should build")
}
}
#[derive(Clone)]
pub struct Ticker {
inner: AsyncTicker,
}
impl Ticker {
pub fn symbol(&self) -> &str {
self.inner.symbol()
}
pub fn history(&self, opts: &HistoryOptions) -> Result<History> {
block_on(self.inner.history(opts))
}
pub fn dividends(&self, opts: &HistoryOptions) -> Result<Vec<crate::history::Dividend>> {
block_on(self.inner.dividends(opts))
}
pub fn splits(&self, opts: &HistoryOptions) -> Result<Vec<crate::history::Split>> {
block_on(self.inner.splits(opts))
}
pub fn actions(&self, opts: &HistoryOptions) -> Result<Option<crate::history::Actions>> {
block_on(self.inner.actions(opts))
}
pub fn info(&self) -> Result<crate::quote::Info> {
block_on(self.inner.info())
}
pub fn fast_info(&self) -> Result<crate::quote::FastInfo> {
block_on(self.inner.fast_info())
}
pub fn holders(&self) -> Result<crate::quote::Holders> {
block_on(self.inner.holders())
}
pub fn sustainability(&self) -> Result<crate::quote::Sustainability> {
block_on(self.inner.sustainability())
}
pub fn analyst_price_targets(&self) -> Result<crate::quote::AnalystPriceTargets> {
block_on(self.inner.analyst_price_targets())
}
pub fn recommendation_trend(&self) -> Result<Vec<crate::quote::RecommendationTrend>> {
block_on(self.inner.recommendation_trend())
}
pub fn financials(
&self,
statement: crate::fundamentals::Statement,
freq: crate::fundamentals::Freq,
) -> Result<crate::fundamentals::Financials> {
block_on(self.inner.financials(statement, freq))
}
pub fn option_chain(&self) -> Result<crate::options::OptionChain> {
block_on(self.inner.option_chain())
}
pub fn news(&self, count: usize, tab: &str) -> Result<Vec<crate::news::NewsArticle>> {
block_on(self.inner.news(count, tab))
}
pub fn isin(&self) -> Result<String> {
block_on(self.inner.isin())
}
pub fn earnings_dates(&self, limit: usize) -> Result<Vec<crate::earnings::EarningsDate>> {
block_on(self.inner.earnings_dates(limit))
}
pub fn earnings_estimate(&self) -> Result<NamedTable> {
block_on(self.inner.earnings_estimate())
}
pub fn revenue_estimate(&self) -> Result<NamedTable> {
block_on(self.inner.revenue_estimate())
}
pub fn earnings_history(&self) -> Result<NamedTable> {
block_on(self.inner.earnings_history())
}
pub fn eps_trend(&self) -> Result<NamedTable> {
block_on(self.inner.eps_trend())
}
pub fn eps_revisions(&self) -> Result<NamedTable> {
block_on(self.inner.eps_revisions())
}
pub fn growth_estimates(&self) -> Result<NamedTable> {
block_on(self.inner.growth_estimates())
}
pub fn recommendations(&self) -> Result<Vec<crate::quote::RecommendationTrend>> {
block_on(self.inner.recommendations())
}
pub fn upgrades_downgrades(&self) -> Result<Vec<UpgradesDowngrades>> {
block_on(self.inner.upgrades_downgrades())
}
pub fn valuation_measures(&self) -> Result<NamedTable> {
block_on(self.inner.valuation_measures())
}
pub fn calendar(&self) -> Result<Calendar> {
block_on(self.inner.calendar())
}
pub fn sec_filings(&self) -> Result<Vec<SecFiling>> {
block_on(self.inner.sec_filings())
}
pub fn shares(&self) -> Result<Option<f64>> {
block_on(self.inner.shares())
}
pub fn shares_full(
&self,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
) -> Result<Vec<(DateTime<Utc>, f64)>> {
block_on(self.inner.shares_full(start, end))
}
pub fn funds_data(&self) -> Result<FundsData> {
block_on(self.inner.funds_data())
}
pub fn history_metadata(&self) -> Result<HistoryMeta> {
block_on(self.inner.history_metadata())
}
}