binance/userstream.rs
1use crate::client::*;
2use crate::errors::*;
3use crate::rest_model::*;
4
5static USER_DATA_STREAM: &str = "/api/v3/userDataStream";
6
7#[derive(Clone)]
8pub struct UserStream {
9 pub client: Client,
10 pub recv_window: u64,
11}
12
13impl UserStream {
14 /// Get a listen key for the stream
15 /// # Examples
16 /// ```rust,no_run
17 /// use binance::{api::*, userstream::*, config::*};
18 /// let userstream: UserStream = Binance::new_with_env(&Config::testnet());
19 /// let start = tokio_test::block_on(userstream.start());
20 /// assert!(start.is_ok(), "{:?}", start);
21 /// assert!(start.unwrap().listen_key.len() > 0)
22 /// ```
23 pub async fn start(&self) -> Result<UserDataStream> { self.client.post(USER_DATA_STREAM, None).await }
24
25 /// Keep the connection alive, as the listen key becomes invalid after 60mn
26 /// # Examples
27 /// ```rust,no_run
28 /// use binance::{api::*, userstream::*, config::*};
29 /// let userstream: UserStream = Binance::new_with_env(&Config::testnet());
30 /// let start = tokio_test::block_on(userstream.start());
31 /// assert!(start.is_ok(), "{:?}", start);
32 /// let keep_alive = tokio_test::block_on(userstream.keep_alive(&start.unwrap().listen_key));
33 /// assert!(keep_alive.is_ok())
34 /// ```
35 pub async fn keep_alive(&self, listen_key: &str) -> Result<Success> {
36 self.client.put(USER_DATA_STREAM, listen_key, None).await
37 }
38
39 /// Invalidate the listen key
40 /// # Examples
41 /// ```rust,no_run
42 /// use binance::{api::*, userstream::*, config::*};
43 /// let userstream: UserStream = Binance::new_with_env(&Config::testnet());
44 /// let start = tokio_test::block_on(userstream.start());
45 /// assert!(start.is_ok(), "{:?}", start);
46 /// let close = tokio_test::block_on(userstream.close(&start.unwrap().listen_key));
47 /// assert!(close.is_ok())
48 /// ```
49 pub async fn close(&self, listen_key: &str) -> Result<Success> {
50 self.client.delete(USER_DATA_STREAM, listen_key, None).await
51 }
52}