xapi/
lib.rs

1mod connection;
2mod credentials;
3mod data;
4mod enums;
5mod error;
6mod socket;
7mod stream;
8
9use std::borrow::Cow;
10
11pub use credentials::Credentials;
12pub use data::*;
13pub use enums::*;
14pub use error::Error;
15pub use socket::Socket;
16pub use stream::Stream;
17
18#[derive(Debug, Clone)]
19pub struct XApi {
20    pub socket: Socket,
21    pub stream: Stream,
22}
23
24pub async fn connect(credentials: &Credentials) -> Result<XApi, Error> {
25    let mut host = Cow::Borrowed(&credentials.host);
26    if !host.starts_with("wss://") && !host.starts_with("ws://") {
27        host.to_mut().insert_str(0, "wss://");
28    }
29
30    let socket_url = format!("{}/{}", &host, &credentials.type_);
31    let stream_url = format!("{}/{}Stream", &host, &credentials.type_);
32
33    let socket = Socket::connect(&socket_url, credentials.safe).await?;
34    let login = socket.login(&credentials.account_id, &credentials.password).await?;
35
36    let stream = Stream::connect(&stream_url, login.stream_session_id).await?;
37
38    Ok(XApi { socket, stream })
39}