use crate::{config::Config, types::alpaca::websocket}; use core::panic; use futures_util::{ stream::{SplitSink, SplitStream}, SinkExt, StreamExt, }; use serde_json::{from_str, to_string}; use std::sync::Arc; use tokio::net::TcpStream; use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream}; pub async fn authenticate( app_config: &Arc, sink: &mut SplitSink>, Message>, stream: &mut SplitStream>>, ) { match stream.next().await.unwrap().unwrap() { Message::Text(data) if from_str::>(&data) .unwrap() .first() == Some(&websocket::incoming::Message::Success( websocket::incoming::success::Message::Connected, )) => {} _ => panic!("Failed to connect to Alpaca websocket."), } sink.send(Message::Text( to_string(&websocket::outgoing::Message::Auth( websocket::outgoing::auth::Message { key: app_config.alpaca_api_key.clone(), secret: app_config.alpaca_api_secret.clone(), }, )) .unwrap(), )) .await .unwrap(); match stream.next().await.unwrap().unwrap() { Message::Text(data) if from_str::>(&data) .unwrap() .first() == Some(&websocket::incoming::Message::Success( websocket::incoming::success::Message::Authenticated, )) => {} _ => panic!("Failed to authenticate with Alpaca websocket."), }; }