use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::header::AUTHORIZATION;
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use crate::client::{to_ws_url, Client};
use crate::error::BoxxkiteError;
pub type DesktopStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
impl Client {
pub async fn desktop_takeover(&self, session_id: &str) -> Result<DesktopStream, BoxxkiteError> {
let ws_url = to_ws_url(
&self.base_url,
&format!("/v1/sandboxes/{session_id}/desktop"),
);
let mut request = ws_url.into_client_request()?;
let header_value = HeaderValue::from_str(&format!("Bearer {}", self.api_key))
.map_err(|err| BoxxkiteError::Config(format!("invalid api_key: {err}")))?;
request.headers_mut().insert(AUTHORIZATION, header_value);
let (stream, _response) = tokio_tungstenite::connect_async(request).await?;
Ok(stream)
}
}