bybit_api/websocket/
models.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize)]
7pub struct WsRequest {
8 pub req_id: String,
10 pub op: String,
12 pub args: Vec<String>,
14}
15
16#[derive(Debug, Clone, Serialize)]
18pub struct WsAuthRequest {
19 pub req_id: String,
21 pub op: String,
23 pub args: Vec<serde_json::Value>,
25}
26
27#[derive(Debug, Clone, Deserialize)]
29pub struct WsResponse {
30 #[serde(default)]
32 pub success: Option<bool>,
33 #[serde(default)]
35 pub ret_msg: Option<String>,
36 #[serde(default)]
38 pub conn_id: Option<String>,
39 #[serde(default)]
41 pub req_id: Option<String>,
42 #[serde(default)]
44 pub op: Option<String>,
45}
46
47#[derive(Debug, Clone, Deserialize)]
49pub struct WsMessage {
50 pub topic: String,
52 #[serde(rename = "type")]
54 pub msg_type: Option<String>,
55 pub ts: Option<u64>,
57 pub data: serde_json::Value,
59}
60
61#[derive(Debug, Clone, Serialize)]
63pub struct WsPing {
64 pub req_id: String,
66 pub op: String,
68}
69
70impl WsPing {
71 pub fn new() -> Self {
73 Self {
74 req_id: uuid::Uuid::new_v4().to_string(),
75 op: "ping".to_string(),
76 }
77 }
78}
79
80impl Default for WsPing {
81 fn default() -> Self {
82 Self::new()
83 }
84}
85
86#[derive(Debug, Clone, Deserialize)]
88pub struct WsPong {
89 #[serde(default)]
91 pub success: Option<bool>,
92 #[serde(default)]
94 pub ret_msg: Option<String>,
95 #[serde(default)]
97 pub conn_id: Option<String>,
98 #[serde(default)]
100 pub req_id: Option<String>,
101 #[serde(default)]
103 pub op: Option<String>,
104}
105
106pub fn is_pong(msg: &serde_json::Value) -> bool {
108 if let Some(op) = msg.get("op").and_then(|v| v.as_str()) {
109 return op == "pong";
110 }
111 if let Some(ret_msg) = msg.get("ret_msg").and_then(|v| v.as_str()) {
112 return ret_msg == "pong";
113 }
114 false
115}
116
117pub fn is_auth_response(msg: &serde_json::Value) -> bool {
119 msg.get("op")
120 .and_then(|v| v.as_str())
121 .map(|op| op == "auth")
122 .unwrap_or(false)
123}
124
125pub fn is_subscription_response(msg: &serde_json::Value) -> bool {
127 msg.get("op")
128 .and_then(|v| v.as_str())
129 .map(|op| op == "subscribe")
130 .unwrap_or(false)
131}
132
133pub fn is_data_message(msg: &serde_json::Value) -> bool {
135 msg.get("topic").is_some()
136}