finnhub_rust/
client.rs

1pub struct FinnClient {
2    pub api_key: String,
3}
4
5impl FinnClient {
6    pub fn new(api_key: String) -> Self {
7        Self {
8            api_key
9        }
10    }
11
12    pub fn ping() -> String {
13        "pong".to_string()
14    }
15
16    pub async fn quote(self, symbol: &str) -> Result<QuoteResponse, reqwest::Error> {
17        let client = reqwest::Client::new();
18        let url = format!("https://finnhub.io/api/v1/quote?symbol={}&token={}", symbol, self.api_key);
19        let response = client
20            .get(url)
21            .send()
22            .await
23            .unwrap()
24            .text()
25            .await
26            .unwrap();
27
28        let parsed_response: QuoteResponse = serde_json::from_str(&response).unwrap();
29        Ok(parsed_response)
30    }
31}
32
33#[derive(Debug, serde::Deserialize)]
34pub struct QuoteResponse {
35    pub c: f64,
36    pub d: f64,
37    pub dp: f64,
38    pub h: f64,
39    pub l: f64,
40    pub o: f64,
41    pub pc: f64,
42    pub t: i64,
43}
44
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[tokio::test]
51    async fn quote_test() {
52        let api_key = "<your api key>".to_string();
53        let client = FinnClient::new(api_key);
54        let resp = client.quote("AAPL").await.unwrap();
55        println!("{:?}", resp)
56    }
57}