bothan_okx/api/types/
subscription.rs

1//! Types for OKX WebSocket subscription management.
2//!
3//! This module provides types for handling WebSocket subscription requests and responses
4//! from the OKX API. It includes structures for subscribing to and unsubscribing from
5//! various data channels, as well as processing subscription confirmations and errors.
6//!
7//! # Key Types
8//!
9//! - [`Request<T>`] - Subscription request structure
10//! - [`Response<T>`] - Subscription response structure
11//! - [`Operation`] - Available subscription operations
12
13use serde::{Deserialize, Serialize};
14
15/// Represents a request to the OKX WebSocket API.
16///
17/// This generic struct defines the structure for subscription and unsubscription
18/// requests sent to the OKX WebSocket API. It includes the operation type and
19/// optional arguments specific to the subscription.
20#[derive(Clone, Debug, Serialize, Deserialize)]
21pub struct Request<T> {
22    /// The operation type (subscribe or unsubscribe).
23    pub op: Operation,
24
25    /// The arguments for the message.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub args: Option<Vec<T>>,
28}
29
30/// Represents the available subscription operations.
31///
32/// This enum defines the two main operations that can be performed on OKX WebSocket
33/// channels: subscribing to receive data and unsubscribing to stop receiving data.
34#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub enum Operation {
37    /// Subscribe to a channel to receive data updates.
38    Subscribe,
39
40    /// Unsubscribe from a channel to stop receiving data updates.
41    Unsubscribe,
42}
43
44/// Represents a subscription response from the OKX WebSocket API.
45///
46/// This generic struct contains the response information for subscription and
47/// unsubscription operations, including success/failure status, error codes,
48/// and connection metadata.
49#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
50#[serde(rename_all = "camelCase")]
51pub struct Response<T> {
52    /// The event name (e.g., "subscribe", "unsubscribe").
53    pub event: String,
54
55    /// The argument related to the response.
56    pub arg: Option<T>,
57
58    /// The response code, if any (0 indicates success).
59    pub code: Option<String>,
60
61    /// The response message, if any (error description for failures).
62    pub msg: Option<String>,
63
64    /// The connection ID for the WebSocket connection.
65    pub conn_id: String,
66}