cookie_agent_server/
client.rs1use std::{ops::Deref, sync::Arc};
2
3use cookie_agent_protocol::{ClientError, Transport};
4
5use crate::{Server, WebSocketTransport, in_process_pair};
6
7#[derive(Clone)]
9pub struct Client(cookie_agent_protocol::Client);
10
11impl Client {
12 pub fn connect_stream<T>(transport: T) -> Self
14 where
15 T: Transport + 'static,
16 {
17 Self(cookie_agent_protocol::Client::connect_stream(transport))
18 }
19
20 pub fn connect_in_process(server: Arc<Server>) -> Self {
22 let (client, service) = in_process_pair(128);
23 tokio::spawn(async move {
24 let _ = server.serve_stream(service).await;
25 });
26 Self::connect_stream(client)
27 }
28
29 pub async fn connect_websocket_with_token(url: &str, token: &str) -> Result<Self, ClientError> {
31 WebSocketTransport::connect_with_token(url, token)
32 .await
33 .map(Self::connect_stream)
34 .map_err(|error| {
35 ClientError::WebSocket(cookie_agent_protocol::diagnostics::sanitize(
36 &cookie_agent_protocol::diagnostics::error_chain(&error),
37 4096,
38 ))
39 })
40 }
41}
42
43impl Deref for Client {
44 type Target = cookie_agent_protocol::Client;
45
46 fn deref(&self) -> &Self::Target {
47 &self.0
48 }
49}