Skip to main content

polymarket_client/secure/
subscribe.rs

1//! SecureClient realtime subscriptions (includes user channel).
2
3use crate::secure::secure_client::SecureClient;
4use crate::subscriptions::{
5    merge_streams, subscribe_one, subscribe_user, SubscribeError, SubscriptionHandle,
6    SubscriptionSpec,
7};
8
9impl SecureClient {
10    /// Subscribe to realtime channels, including authenticated user events.
11    pub fn subscribe(
12        &self,
13        specs: Vec<SubscriptionSpec>,
14    ) -> Result<SubscriptionHandle, SubscribeError> {
15        if specs.is_empty() {
16            return Err(SubscribeError::UserInput(crate::error::user_input(
17                "at least one subscription spec is required",
18            )));
19        }
20
21        let ws = &self.public().ws;
22        let mut streams = Vec::with_capacity(specs.len());
23
24        for spec in specs {
25            let stream = match spec {
26                SubscriptionSpec::User(user) => subscribe_user(
27                    ws,
28                    self.credentials().to_sdk_credentials().map_err(|e| {
29                        SubscribeError::Transport(format!("invalid credentials: {e}"))
30                    })?,
31                    self.wallet(),
32                    user,
33                )?,
34                other => subscribe_one(ws, other)?,
35            };
36            streams.push(stream);
37        }
38
39        Ok(SubscriptionHandle::new(merge_streams(streams), None))
40    }
41}