finance_query/providers/
config.rs1use crate::adapters::yahoo::client::ClientConfig;
2use crate::error::Result;
3use crate::providers::{Fetch, Provider, ProviderSet, Routes, build_providers};
4use std::sync::Arc;
5use std::time::Duration;
6
7pub struct Providers {
31 pub(crate) set: Arc<ProviderSet>,
32}
33
34impl Providers {
35 pub fn builder() -> ProvidersBuilder {
37 ProvidersBuilder::default()
38 }
39
40 pub fn ticker(&self, symbol: impl Into<String>) -> crate::TickerBuilder {
46 crate::Ticker::builder(symbol).with_provider_set(Arc::clone(&self.set))
47 }
48
49 pub fn tickers<S, I>(&self, symbols: I) -> crate::TickersBuilder
55 where
56 S: Into<String>,
57 I: IntoIterator<Item = S>,
58 {
59 crate::Tickers::builder(symbols).with_provider_set(Arc::clone(&self.set))
60 }
61
62 #[cfg(any(
64 feature = "alphavantage",
65 feature = "crypto",
66 feature = "fmp",
67 feature = "polygon"
68 ))]
69 pub fn crypto(&self, id: impl Into<String>) -> crate::domains::CryptoCoin {
70 crate::domains::CryptoCoin::with_providers(id.into().into(), Arc::clone(&self.set))
71 }
72
73 #[cfg(any(feature = "alphavantage", feature = "fmp", feature = "polygon"))]
75 pub fn forex(
76 &self,
77 from: impl Into<String>,
78 to: impl Into<String>,
79 ) -> crate::domains::ForexPair {
80 crate::domains::ForexPair::with_providers(
81 from.into().into(),
82 to.into().into(),
83 Arc::clone(&self.set),
84 )
85 }
86
87 #[cfg(any(feature = "alphavantage", feature = "polygon", feature = "fred"))]
89 pub fn economic(&self, series_id: impl Into<String>) -> crate::domains::EconomicIndicator {
90 crate::domains::EconomicIndicator::with_providers(
91 series_id.into().into(),
92 Arc::clone(&self.set),
93 )
94 }
95
96 #[cfg(any(feature = "polygon", feature = "fmp"))]
98 pub fn index(&self, symbol: impl Into<String>) -> crate::domains::Index {
99 crate::domains::Index::with_providers(symbol.into().into(), Arc::clone(&self.set))
100 }
101
102 #[cfg(feature = "polygon")]
104 pub fn futures(&self, symbol: impl Into<String>) -> crate::domains::FuturesContract {
105 crate::domains::FuturesContract::with_providers(symbol.into().into(), Arc::clone(&self.set))
106 }
107
108 #[cfg(any(feature = "fmp", feature = "alphavantage"))]
110 pub fn commodity(&self, symbol: impl Into<String>) -> crate::domains::Commodity {
111 crate::domains::Commodity::with_providers(symbol.into().into(), Arc::clone(&self.set))
112 }
113
114 pub fn filings(&self, symbol: impl Into<String>) -> crate::domains::Filings {
119 crate::domains::Filings::with_providers(symbol.into().into(), Arc::clone(&self.set))
120 }
121}
122
123pub struct ProvidersBuilder {
125 provider_ids: Vec<Provider>,
126 config: ClientConfig,
127 routes: Routes,
128}
129
130impl Default for ProvidersBuilder {
131 fn default() -> Self {
132 Self {
133 provider_ids: vec![Provider::Yahoo],
134 config: ClientConfig::default(),
135 routes: Routes::new(Fetch::Sequential),
136 }
137 }
138}
139
140impl ProvidersBuilder {
141 pub fn fetch(mut self, mode: Fetch) -> Self {
145 self.routes.fetch = mode;
146 self
147 }
148
149 pub fn route(mut self, cap: crate::providers::Capability, providers: &[Provider]) -> Self {
155 self.routes.map.insert(cap, providers.to_vec());
156 for provider in providers {
157 if !self.provider_ids.contains(provider) {
158 self.provider_ids.push(*provider);
159 }
160 }
161 self
162 }
163
164 pub fn region(mut self, region: crate::constants::Region) -> Self {
166 self.config.lang = region.lang().to_string();
167 self.config.region = region.region().to_string();
168 self
169 }
170
171 pub fn timeout(mut self, t: Duration) -> Self {
173 self.config.timeout = t;
174 self
175 }
176
177 pub fn proxy(mut self, p: impl Into<String>) -> Self {
179 self.config.proxy = Some(p.into());
180 self
181 }
182
183 pub async fn build(self) -> Result<Providers> {
185 let set = build_providers(&self.provider_ids, &self.config, self.routes).await?;
186 Ok(Providers { set: Arc::new(set) })
187 }
188}