Skip to main content

stock_rust/stocks/
mod.rs

1pub mod base;
2pub mod netease;
3pub mod sina;
4pub mod tencent;
5pub mod transforms;
6pub mod xueqiu;
7
8use anyhow::Result;
9use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
10
11#[derive(Clone)]
12pub struct Stocks {
13    pub base: base::BaseApi,
14    pub sina: sina::SinaApi,
15    pub xueqiu: xueqiu::XueqiuApi,
16    pub netease: netease::NeteaseApi,
17    pub tencent: tencent::TencentApi,
18}
19
20impl Stocks {
21    pub fn new() -> Result<Self> {
22        let mut headers = HeaderMap::new();
23        headers.insert(
24            USER_AGENT,
25            HeaderValue::from_static(
26                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36",
27            ),
28        );
29
30        let client = reqwest::Client::builder().default_headers(headers).build()?;
31
32        Ok(Self {
33            base: base::BaseApi,
34            sina: sina::SinaApi::new(client.clone()),
35            xueqiu: xueqiu::XueqiuApi::new(client.clone()),
36            netease: netease::NeteaseApi::new(client.clone()),
37            tencent: tencent::TencentApi::new(client),
38        })
39    }
40}
41
42impl Default for Stocks {
43    fn default() -> Self {
44        Self::new().expect("failed to build HTTP client")
45    }
46}