finnhub_rs/
client.rs

1#![allow(dead_code)]
2
3use super::types::*;
4use exitfailure::ExitFailure;
5use reqwest::Url;
6
7/// Finnhub API Client object.
8#[derive(Debug)]
9pub struct Client {
10    /// API key from the Finnhub dashboard.
11    pub api_key: String,
12}
13
14impl Client {
15    /// Creates a new Finnhub Client
16    pub fn new(api_key: String) -> Self {
17        Self { api_key }
18    }
19
20    /// Lookups a symbol in the Finnhub API
21    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    /// Returns a list of supported stocks given the exchange.
34    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    /// Returns the profile of the company specified.
47    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    /// Returns the latest market news in the given category.
60    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    /// Returns the company news from the company specified in the given time period.
73    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    // /// Returns the latest sentiment of news of the company specified.
91    // pub async fn news_sentiment(self, symbol: String) -> Result<NewsSentiment, ExitFailure> {
92    //     let url = format!(
93    //         "https://finnhub.io/api/v1/news-sentiment?symbol={}&token={}",
94    //         symbol, self.api_key
95    //     );
96
97    //     let url = Url::parse(&*url)?;
98    //     let res = reqwest::get(url).await?.json::<NewsSentiment>().await?;
99
100    //     Ok(res)
101    // }
102
103    /// Returns the specified companies peers.
104    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    /// Returns the specified company's current stock quote.
117    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    // /// Returns the basic financials of the company specified according to the given metric.
130    // pub async fn basic_financials(
131    //     self,
132    //     symbol: String,
133    //     metric: String,
134    // ) -> Result<BasicFinancials, ExitFailure> {
135    //     let url = format!(
136    //         "https://finnhub.io/api/v1/stock/metric?symbol={}&metric={}&token={}",
137    //         symbol, metric, self.api_key
138    //     );
139
140    //     let url = Url::parse(&*url)?;
141    //     let res = reqwest::get(url).await?.json::<BasicFinancials>().await?;
142
143    //     Ok(res)
144    // }
145}