webull_rs/endpoints/
watchlists.rs

1use crate::auth::AuthManager;
2use crate::endpoints::base::BaseEndpoint;
3use crate::error::WebullResult;
4use reqwest::Client;
5use serde::{Deserialize, Serialize};
6use std::sync::Arc;
7
8/// Watchlist information.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Watchlist {
11    /// Watchlist ID
12    pub id: String,
13
14    /// Watchlist name
15    pub name: String,
16
17    /// Watchlist symbols
18    pub symbols: Vec<String>,
19}
20
21/// Request to create a watchlist.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct CreateWatchlistRequest {
24    /// Watchlist name
25    pub name: String,
26
27    /// Watchlist symbols
28    pub symbols: Vec<String>,
29}
30
31/// Request to modify a watchlist.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct ModifyWatchlistRequest {
34    /// Watchlist ID
35    pub id: String,
36
37    /// Watchlist name
38    pub name: Option<String>,
39
40    /// Watchlist symbols to add
41    pub add_symbols: Option<Vec<String>>,
42
43    /// Watchlist symbols to remove
44    pub remove_symbols: Option<Vec<String>>,
45}
46
47/// Endpoints for watchlist operations.
48pub struct WatchlistEndpoints {
49    /// Base endpoint
50    base: BaseEndpoint,
51}
52
53impl WatchlistEndpoints {
54    /// Create new watchlist endpoints.
55    pub fn new(client: Client, base_url: String, auth_manager: Arc<AuthManager>) -> Self {
56        Self {
57            base: BaseEndpoint::new(client, base_url, auth_manager),
58        }
59    }
60
61    /// Get all watchlists.
62    pub async fn get_watchlists(&self) -> WebullResult<Vec<Watchlist>> {
63        self.base.get("/api/wlas/watchlist").await
64    }
65
66    /// Get a watchlist by ID.
67    pub async fn get_watchlist(&self, watchlist_id: &str) -> WebullResult<Watchlist> {
68        let path = format!("/api/wlas/watchlist/{}", watchlist_id);
69        self.base.get(&path).await
70    }
71
72    /// Create a watchlist.
73    pub async fn create_watchlist(
74        &self,
75        request: &CreateWatchlistRequest,
76    ) -> WebullResult<Watchlist> {
77        self.base.post("/api/wlas/watchlist", request).await
78    }
79
80    /// Modify a watchlist.
81    pub async fn modify_watchlist(
82        &self,
83        request: &ModifyWatchlistRequest,
84    ) -> WebullResult<Watchlist> {
85        self.base.post("/api/wlas/watchlist/modify", request).await
86    }
87
88    /// Delete a watchlist.
89    pub async fn delete_watchlist(&self, watchlist_id: &str) -> WebullResult<()> {
90        let path = format!("/api/wlas/watchlist/delete/{}", watchlist_id);
91        self.base.delete(&path).await
92    }
93
94    /// Add symbols to a watchlist.
95    pub async fn add_symbols(
96        &self,
97        watchlist_id: &str,
98        symbols: &[String],
99    ) -> WebullResult<Watchlist> {
100        let request = ModifyWatchlistRequest {
101            id: watchlist_id.to_string(),
102            name: None,
103            add_symbols: Some(symbols.to_vec()),
104            remove_symbols: None,
105        };
106
107        self.modify_watchlist(&request).await
108    }
109
110    /// Remove symbols from a watchlist.
111    pub async fn remove_symbols(
112        &self,
113        watchlist_id: &str,
114        symbols: &[String],
115    ) -> WebullResult<Watchlist> {
116        let request = ModifyWatchlistRequest {
117            id: watchlist_id.to_string(),
118            name: None,
119            add_symbols: None,
120            remove_symbols: Some(symbols.to_vec()),
121        };
122
123        self.modify_watchlist(&request).await
124    }
125
126    /// Rename a watchlist.
127    pub async fn rename_watchlist(
128        &self,
129        watchlist_id: &str,
130        name: &str,
131    ) -> WebullResult<Watchlist> {
132        let request = ModifyWatchlistRequest {
133            id: watchlist_id.to_string(),
134            name: Some(name.to_string()),
135            add_symbols: None,
136            remove_symbols: None,
137        };
138
139        self.modify_watchlist(&request).await
140    }
141}