1#![allow(dead_code)]
2
3use super::types::*;
4use exitfailure::ExitFailure;
5use reqwest::Url;
6
7#[derive(Debug)]
9pub struct Client {
10 pub api_key: String,
12}
13
14impl Client {
15 pub fn new(api_key: String) -> Self {
17 Self { api_key }
18 }
19
20 pub async fn symbol_lookup(self, query: String) -> Result<SymbolLookup, ExitFailure> {
22 let url = format!(
23 "https://finnhub.io/api/v1/search?q={}&token={}",
24 query, self.api_key
25 );
26
27 let url = Url::parse(&*url)?;
28 let res = reqwest::get(url).await?.json::<SymbolLookup>().await?;
29
30 Ok(res)
31 }
32
33 pub async fn stock_symbol(self, exchange: String) -> Result<Vec<StockSymbol>, ExitFailure> {
35 let url = format!(
36 "https://finnhub.io/api/v1/stock/symbol?exchange={}&token={}",
37 exchange, self.api_key
38 );
39
40 let url = Url::parse(&*url)?;
41 let res = reqwest::get(url).await?.json::<Vec<StockSymbol>>().await?;
42
43 Ok(res)
44 }
45
46 pub async fn company_profile2(self, symbol: String) -> Result<CompanyProfile, ExitFailure> {
48 let url = format!(
49 "https://finnhub.io/api/v1/stock/profile2?symbol={}&token={}",
50 symbol, self.api_key
51 );
52
53 let url = Url::parse(&*url)?;
54 let res = reqwest::get(url).await?.json::<CompanyProfile>().await?;
55
56 Ok(res)
57 }
58
59 pub async fn market_news(self, category: String) -> Result<Vec<MarketNews>, ExitFailure> {
61 let url = format!(
62 "https://finnhub.io/api/v1/news?category={}&token={}",
63 category, self.api_key
64 );
65
66 let url = Url::parse(&*url)?;
67 let res = reqwest::get(url).await?.json::<Vec<MarketNews>>().await?;
68
69 Ok(res)
70 }
71
72 pub async fn company_news(
74 self,
75 symbol: String,
76 from: String,
77 to: String,
78 ) -> Result<Vec<CompanyNews>, ExitFailure> {
79 let url = format!(
80 "https://finnhub.io/api/v1/company-news?symbol={}&from={}&to={}&token={}",
81 symbol, from, to, self.api_key
82 );
83
84 let url = Url::parse(&*url)?;
85 let res = reqwest::get(url).await?.json::<Vec<CompanyNews>>().await?;
86
87 Ok(res)
88 }
89
90 pub async fn peers(self, symbol: String) -> Result<Vec<String>, ExitFailure> {
105 let url = format!(
106 "https://finnhub.io/api/v1/stock/peers?symbol={}&token={}",
107 symbol, self.api_key
108 );
109
110 let url = Url::parse(&*url)?;
111 let res = reqwest::get(url).await?.json::<Vec<String>>().await?;
112
113 Ok(res)
114 }
115
116 pub async fn quote(self, symbol: String) -> Result<CompanyQuote, ExitFailure> {
118 let url = format!(
119 "https://finnhub.io/api/v1/quote?symbol={}&token={}",
120 symbol, self.api_key
121 );
122
123 let url = Url::parse(&*url)?;
124 let res = reqwest::get(url).await?.json::<CompanyQuote>().await?;
125
126 Ok(res)
127 }
128
129 }