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