ironbeam-rs 0.2.0

Async Rust client for the Ironbeam futures trading API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
mod connection;
pub(crate) mod handler;
mod subscriptions;

use hyper::header::{AUTHORIZATION, HeaderMap};
use tokio::sync::{mpsc, watch};
use tokio::task::JoinHandle;

use crate::error::{Error, Result};
use crate::types::streaming::{IndicatorSubscribeResponse, StreamIdResponse, SubscribeBarsRequest};

use super::http::HttpTransport;
use super::{Client, RequestHelper};

pub use handler::StreamEvent;
use subscriptions::{BarKind, MarketFeed};

const DEFAULT_CHANNEL_CAPACITY: usize = 256;

// ---------------------------------------------------------------------------
// StreamBuilder
// ---------------------------------------------------------------------------

/// Builder for creating a streaming WebSocket connection.
///
/// Obtained via [`Client::stream()`]. Call [`start()`](StreamBuilder::start)
/// to create the stream session and open the WebSocket.
///
/// # Example
///
/// ```no_run
/// # use ironbeam_rs::client::{Client, Credentials};
/// # async fn example() -> ironbeam_rs::error::Result<()> {
/// # let client = Client::builder()
/// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
/// #     .connect().await?;
/// let mut stream = client.stream().start().await?;
/// stream.subscribe_quotes(&["XCME:ES.U26"]).await?;
/// # Ok(())
/// # }
/// ```
pub struct StreamBuilder<'a, H: HttpTransport> {
    client: &'a Client<H>,
    channel_capacity: usize,
}

impl<'a, H: HttpTransport> StreamBuilder<'a, H> {
    /// Set the event channel capacity (default: 256).
    ///
    /// Too small and the message loop blocks on send (backpressure);
    /// too large and stale quotes may buffer before consumption.
    #[must_use]
    pub fn channel_capacity(mut self, capacity: usize) -> Self {
        self.channel_capacity = capacity;
        self
    }

    /// Create the stream session, open the WebSocket, and return a live
    /// [`StreamHandle`] for subscribing to feeds and receiving events.
    pub async fn start(self) -> Result<StreamHandle<H>> {
        let (stream_id, token) = self.create_session().await?;

        let ws = match connection::connect(&self.client.request.base_url, &stream_id, &token).await
        {
            Ok(ws) => ws,
            Err(e) => {
                tracing::warn!(stream_id = %stream_id, error = %e, "websocket connect failed after stream session created");
                return Err(e);
            }
        };
        tracing::info!(stream_id = %stream_id, "websocket connected");

        self.spawn_loop(stream_id, ws)
    }

    /// Start with a pre-built WebSocket transport (test seam).
    #[cfg(test)]
    pub(crate) async fn start_with_ws<W: connection::WsTransport>(
        self,
        ws: W,
    ) -> Result<StreamHandle<H>> {
        let (stream_id, _token) = self.create_session().await?;
        self.spawn_loop(stream_id, ws)
    }

    /// Create the stream session via REST and extract the bearer token.
    async fn create_session(&self) -> Result<(String, String)> {
        let resp: StreamIdResponse = self.client.get("/stream/create").await?;
        let stream_id = resp.stream_id;
        tracing::info!(stream_id = %stream_id, "stream session created");
        let token = extract_token(&self.client.request.auth_headers)?;
        Ok((stream_id, token))
    }

    /// Spawn the message loop and build the [`StreamHandle`].
    fn spawn_loop<W: connection::WsTransport>(
        self,
        stream_id: String,
        ws: W,
    ) -> Result<StreamHandle<H>> {
        let (tx, rx) = mpsc::channel(self.channel_capacity);
        let (shutdown_tx, shutdown_rx) = watch::channel(false);
        let task = tokio::spawn(connection::message_loop(
            ws,
            tx,
            shutdown_rx,
            stream_id.clone(),
        ));

        Ok(StreamHandle {
            stream_id,
            request: self.client.request.clone(),
            rx,
            shutdown_tx,
            task: Some(task),
        })
    }
}

impl<H: HttpTransport> Client<H> {
    /// Begin building a streaming WebSocket connection.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use ironbeam_rs::client::{Client, Credentials};
    /// # async fn example() -> ironbeam_rs::error::Result<()> {
    /// # let client = Client::builder()
    /// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
    /// #     .connect().await?;
    /// let mut stream = client.stream().start().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn stream(&self) -> StreamBuilder<'_, H> {
        StreamBuilder {
            client: self,
            channel_capacity: DEFAULT_CHANNEL_CAPACITY,
        }
    }
}

// ---------------------------------------------------------------------------
// StreamHandle
// ---------------------------------------------------------------------------

/// A live streaming connection.
///
/// Receives [`StreamEvent`]s via [`next()`](StreamHandle::next) and manages
/// subscriptions via `subscribe_*` / `unsubscribe_*` methods.
///
/// # Example
///
/// ```no_run
/// # use ironbeam_rs::client::{Client, Credentials};
/// # use ironbeam_rs::client::stream::StreamEvent;
/// # async fn example() -> ironbeam_rs::error::Result<()> {
/// # let client = Client::builder()
/// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
/// #     .connect().await?;
/// let mut stream = client.stream().start().await?;
/// stream.subscribe_quotes(&["XCME:ES.U26"]).await?;
///
/// while let Some(event) = stream.next().await {
///     match event? {
///         StreamEvent::Quotes(quotes) => println!("{quotes:?}"),
///         _ => {}
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub struct StreamHandle<H: HttpTransport> {
    stream_id: String,
    request: RequestHelper<H>,
    rx: mpsc::Receiver<Result<StreamEvent>>,
    shutdown_tx: watch::Sender<bool>,
    task: Option<JoinHandle<()>>,
}

impl<H: HttpTransport> StreamHandle<H> {
    /// Receive the next streaming event.
    ///
    /// Returns `None` when the stream is closed.
    pub async fn next(&mut self) -> Option<Result<StreamEvent>> {
        self.rx.recv().await
    }

    /// Gracefully close the stream and await the message loop.
    pub async fn close(mut self) -> Result<()> {
        tracing::info!(stream_id = %self.stream_id, "closing stream");
        let _ = self.shutdown_tx.send(true);
        if let Some(task) = self.task.take() {
            task.await.map_err(|e| Error::WebSocket(e.to_string()))?;
        }
        tracing::info!(stream_id = %self.stream_id, "stream closed");
        Ok(())
    }

    /// The stream session identifier.
    #[must_use]
    pub fn stream_id(&self) -> &str {
        &self.stream_id
    }

    // -- helpers ------------------------------------------------------------

    async fn sub_market(&self, feed: MarketFeed, symbols: &[&str]) -> Result<()> {
        tracing::info!(stream_id = %self.stream_id, feed = feed.as_str(), ?symbols, "subscribing");
        subscriptions::subscribe_market(&self.request, feed, &self.stream_id, symbols).await
    }

    async fn unsub_market(&self, feed: MarketFeed, symbols: &[&str]) -> Result<()> {
        tracing::info!(stream_id = %self.stream_id, feed = feed.as_str(), ?symbols, "unsubscribing");
        subscriptions::unsubscribe_market(&self.request, feed, &self.stream_id, symbols).await
    }

    async fn sub_indicator(
        &self,
        kind: BarKind,
        req: &SubscribeBarsRequest,
    ) -> Result<IndicatorSubscribeResponse> {
        tracing::info!(stream_id = %self.stream_id, kind = kind.as_str(), symbol = %req.symbol, "subscribing indicator");
        subscriptions::subscribe_indicator(&self.request, kind, &self.stream_id, req).await
    }

    // -- Market data subscriptions ------------------------------------------

    /// Subscribe to quote updates.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use ironbeam_rs::client::{Client, Credentials};
    /// # async fn example() -> ironbeam_rs::error::Result<()> {
    /// # let client = Client::builder()
    /// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
    /// #     .connect().await?;
    /// # let mut stream = client.stream().start().await?;
    /// stream.subscribe_quotes(&["XCME:ES.U26", "XCME:NQ.U26"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe_quotes(&self, symbols: &[&str]) -> Result<()> {
        self.sub_market(MarketFeed::Quotes, symbols).await
    }

    /// Unsubscribe from quote updates.
    pub async fn unsubscribe_quotes(&self, symbols: &[&str]) -> Result<()> {
        self.unsub_market(MarketFeed::Quotes, symbols).await
    }

    /// Subscribe to depth (order book) updates.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use ironbeam_rs::client::{Client, Credentials};
    /// # async fn example() -> ironbeam_rs::error::Result<()> {
    /// # let client = Client::builder()
    /// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
    /// #     .connect().await?;
    /// # let mut stream = client.stream().start().await?;
    /// stream.subscribe_depth(&["XCME:ES.U26"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe_depth(&self, symbols: &[&str]) -> Result<()> {
        self.sub_market(MarketFeed::Depths, symbols).await
    }

    /// Unsubscribe from depth updates.
    pub async fn unsubscribe_depth(&self, symbols: &[&str]) -> Result<()> {
        self.unsub_market(MarketFeed::Depths, symbols).await
    }

    /// Subscribe to trade updates.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use ironbeam_rs::client::{Client, Credentials};
    /// # async fn example() -> ironbeam_rs::error::Result<()> {
    /// # let client = Client::builder()
    /// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
    /// #     .connect().await?;
    /// # let mut stream = client.stream().start().await?;
    /// stream.subscribe_trades(&["XCME:ES.U26"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe_trades(&self, symbols: &[&str]) -> Result<()> {
        self.sub_market(MarketFeed::Trades, symbols).await
    }

    /// Unsubscribe from trade updates.
    pub async fn unsubscribe_trades(&self, symbols: &[&str]) -> Result<()> {
        self.unsub_market(MarketFeed::Trades, symbols).await
    }

    // -- Indicator subscriptions --------------------------------------------

    /// Subscribe to trade bar indicators.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use ironbeam_rs::client::{Client, Credentials};
    /// # use ironbeam_rs::types::streaming::SubscribeBarsRequest;
    /// # use ironbeam_rs::types::BarType;
    /// # async fn example() -> ironbeam_rs::error::Result<()> {
    /// # let client = Client::builder()
    /// #     .credentials(Credentials { username: "u".into(), password: "p".into(), api_key: "k".into() })
    /// #     .connect().await?;
    /// # let mut stream = client.stream().start().await?;
    /// let resp = stream.subscribe_trade_bars(&SubscribeBarsRequest {
    ///     symbol: "XCME:ES.U26".into(),
    ///     period: 1,
    ///     bar_type: BarType::Minute,
    ///     load_size: 100,
    /// }).await?;
    /// println!("indicator_id: {}", resp.indicator_id);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe_trade_bars(
        &self,
        req: &SubscribeBarsRequest,
    ) -> Result<IndicatorSubscribeResponse> {
        self.sub_indicator(BarKind::Trade, req).await
    }

    /// Subscribe to tick bar indicators.
    pub async fn subscribe_tick_bars(
        &self,
        req: &SubscribeBarsRequest,
    ) -> Result<IndicatorSubscribeResponse> {
        self.sub_indicator(BarKind::Tick, req).await
    }

    /// Subscribe to time bar indicators.
    pub async fn subscribe_time_bars(
        &self,
        req: &SubscribeBarsRequest,
    ) -> Result<IndicatorSubscribeResponse> {
        self.sub_indicator(BarKind::Time, req).await
    }

    /// Subscribe to volume bar indicators.
    pub async fn subscribe_volume_bars(
        &self,
        req: &SubscribeBarsRequest,
    ) -> Result<IndicatorSubscribeResponse> {
        self.sub_indicator(BarKind::Volume, req).await
    }

    /// Unsubscribe from an indicator by its identifier.
    ///
    /// The `indicator_id` is returned by the `subscribe_*_bars` methods.
    pub async fn unsubscribe_indicator(&self, indicator_id: &str) -> Result<()> {
        tracing::info!(stream_id = %self.stream_id, indicator_id, "unsubscribing indicator");
        subscriptions::unsubscribe_indicator(&self.request, &self.stream_id, indicator_id).await
    }
}

/// Best-effort shutdown on drop. Signals the message loop to close but
/// cannot await completion. Prefer [`StreamHandle::close()`] for guaranteed cleanup.
impl<H: HttpTransport> Drop for StreamHandle<H> {
    fn drop(&mut self) {
        let _ = self.shutdown_tx.send(true);
    }
}

impl<H: HttpTransport> std::fmt::Debug for StreamHandle<H> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StreamHandle")
            .field("stream_id", &self.stream_id)
            .field("base_url", &self.request.base_url)
            .finish()
    }
}

/// Extract the bearer token from auth headers.
fn extract_token(headers: &HeaderMap) -> Result<String> {
    let value = headers
        .get(AUTHORIZATION)
        .ok_or_else(|| Error::Auth("missing authorization header".into()))?
        .to_str()
        .map_err(|e| Error::Auth(e.to_string()))?;

    value
        .strip_prefix("Bearer ")
        .map(|t| t.to_owned())
        .ok_or_else(|| Error::Auth("invalid authorization header format".into()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::http::mock::{MockHttp, MockResponse};
    use crate::client::test_support::test_client_with_auth;
    use connection::mock::MockWsTransport;

    #[test]
    fn extract_token_strips_bearer() {
        let mut headers = HeaderMap::new();
        headers.insert(AUTHORIZATION, "Bearer my_token".parse().unwrap());
        assert_eq!(extract_token(&headers).unwrap(), "my_token");
    }

    #[test]
    fn extract_token_missing_header() {
        let headers = HeaderMap::new();
        assert!(extract_token(&headers).is_err());
    }

    #[test]
    fn extract_token_invalid_format() {
        let mut headers = HeaderMap::new();
        headers.insert(AUTHORIZATION, "Basic abc".parse().unwrap());
        assert!(extract_token(&headers).is_err());
    }

    #[tokio::test]
    async fn start_creates_session_and_streams_events() {
        let mock = MockHttp::new(vec![MockResponse::ok(
            r#"{"status":"OK","streamId":"s-123"}"#,
        )]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::from_json(&[r#"{"q":[{"s":"XCME:ES.U25"}]}"#]);

        let mut stream = client.stream().start_with_ws(ws).await.unwrap();

        assert_eq!(stream.stream_id(), "s-123");

        let event = stream.next().await.unwrap().unwrap();
        assert!(matches!(event, StreamEvent::Quotes(..)));

        // Verify REST call was made
        let reqs = client.request.http.recorded_requests();
        assert_eq!(reqs.len(), 1);
        assert!(reqs[0].uri.to_string().contains("/stream/create"));
    }

    #[tokio::test]
    async fn start_propagates_create_error() {
        let mock = MockHttp::new(vec![MockResponse::error(
            hyper::StatusCode::INTERNAL_SERVER_ERROR,
            r#"{"error1":"Server Error"}"#,
        )]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::from_json(&[]);

        let err = client.stream().start_with_ws(ws).await.unwrap_err();
        assert!(matches!(err, Error::Api { status: 500, .. }));
    }

    #[tokio::test]
    async fn stream_close_signals_shutdown() {
        let mock = MockHttp::new(vec![MockResponse::ok(
            r#"{"status":"OK","streamId":"s-456"}"#,
        )]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.close().await.unwrap();
    }

    #[tokio::test]
    async fn channel_capacity_is_applied() {
        // 2 responses: stream/create + subscribe_quotes
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-cap"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::from_json(&[r#"{"q":[{"s":"SYM"}]}"#]);

        let mut stream = client
            .stream()
            .channel_capacity(4)
            .start_with_ws(ws)
            .await
            .unwrap();

        let event = stream.next().await.unwrap().unwrap();
        assert!(matches!(event, StreamEvent::Quotes(..)));
    }

    #[tokio::test]
    async fn subscribe_quotes_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.subscribe_quotes(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert_eq!(reqs.len(), 2);
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/quotes/subscribe/s-1")
        );
    }

    #[tokio::test]
    async fn unsubscribe_quotes_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.unsubscribe_quotes(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/quotes/unsubscribe/s-1")
        );
    }

    #[tokio::test]
    async fn subscribe_depth_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.subscribe_depth(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/depths/subscribe/s-1")
        );
    }

    #[tokio::test]
    async fn unsubscribe_depth_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.unsubscribe_depth(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/depths/unsubscribe/s-1")
        );
    }

    #[tokio::test]
    async fn subscribe_trades_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.subscribe_trades(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/trades/subscribe/s-1")
        );
    }

    #[tokio::test]
    async fn unsubscribe_trades_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.unsubscribe_trades(&["XCME:ES.U25"]).await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/market/trades/unsubscribe/s-1")
        );
    }

    #[tokio::test]
    async fn subscribe_trade_bars_via_handle() {
        use crate::types::{BarType, streaming::SubscribeBarsRequest};

        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(
                r#"{"indicatorId":"IND1","valueNames":["date"],"valueTypes":["date"]}"#,
            ),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        let resp = stream
            .subscribe_trade_bars(&SubscribeBarsRequest {
                symbol: "XCME:ES.U25".into(),
                period: 1,
                bar_type: BarType::Minute,
                load_size: 100,
            })
            .await
            .unwrap();

        assert_eq!(resp.indicator_id, "IND1");
        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/indicator/s-1/tradeBars/subscribe")
        );
    }

    #[tokio::test]
    async fn subscribe_tick_bars_via_handle() {
        use crate::types::{BarType, streaming::SubscribeBarsRequest};

        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(
                r#"{"indicatorId":"IND2","valueNames":["date"],"valueTypes":["date"]}"#,
            ),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        let resp = stream
            .subscribe_tick_bars(&SubscribeBarsRequest {
                symbol: "SYM".into(),
                period: 5,
                bar_type: BarType::Tick,
                load_size: 50,
            })
            .await
            .unwrap();

        assert_eq!(resp.indicator_id, "IND2");
        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/indicator/s-1/tickBars/subscribe")
        );
    }

    #[tokio::test]
    async fn subscribe_time_bars_via_handle() {
        use crate::types::{BarType, streaming::SubscribeBarsRequest};

        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(
                r#"{"indicatorId":"IND3","valueNames":["date"],"valueTypes":["date"]}"#,
            ),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        let resp = stream
            .subscribe_time_bars(&SubscribeBarsRequest {
                symbol: "SYM".into(),
                period: 1,
                bar_type: BarType::Minute,
                load_size: 10,
            })
            .await
            .unwrap();

        assert_eq!(resp.indicator_id, "IND3");
        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/indicator/s-1/timeBars/subscribe")
        );
    }

    #[tokio::test]
    async fn subscribe_volume_bars_via_handle() {
        use crate::types::{BarType, streaming::SubscribeBarsRequest};

        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(
                r#"{"indicatorId":"IND4","valueNames":["date"],"valueTypes":["date"]}"#,
            ),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        let resp = stream
            .subscribe_volume_bars(&SubscribeBarsRequest {
                symbol: "SYM".into(),
                period: 100,
                bar_type: BarType::Tick,
                load_size: 20,
            })
            .await
            .unwrap();

        assert_eq!(resp.indicator_id, "IND4");
        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/indicator/s-1/volumeBars/subscribe")
        );
    }

    #[tokio::test]
    async fn unsubscribe_indicator_via_handle() {
        let mock = MockHttp::new(vec![
            MockResponse::ok(r#"{"status":"OK","streamId":"s-1"}"#),
            MockResponse::ok(r#"{"status":"OK"}"#),
        ]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        stream.unsubscribe_indicator("IND1").await.unwrap();

        let reqs = client.request.http.recorded_requests();
        assert!(
            reqs[1]
                .uri
                .to_string()
                .contains("/indicator/s-1/unsubscribe/IND1")
        );
    }

    #[tokio::test]
    async fn stream_handle_debug() {
        let mock = MockHttp::new(vec![MockResponse::ok(
            r#"{"status":"OK","streamId":"s-dbg"}"#,
        )]);
        let client = test_client_with_auth(mock);
        let ws = MockWsTransport::new(vec![]);

        let stream = client.stream().start_with_ws(ws).await.unwrap();
        let debug = format!("{stream:?}");
        assert!(debug.contains("s-dbg"));
        assert!(debug.contains("http://test"));
    }
}