nautilus_bybit/common/
urls.rs1use super::enums::{BybitEnvironment, BybitProductType};
19
20const STREAM_MAINNET: &str = "stream";
21const STREAM_TESTNET: &str = "stream-testnet";
22const STREAM_DEMO: &str = "stream-demo";
23
24#[must_use]
26pub const fn bybit_http_base_url(environment: BybitEnvironment) -> &'static str {
27 match environment {
28 BybitEnvironment::Mainnet => "https://api.bybit.com",
29 BybitEnvironment::Demo => "https://api-demo.bybit.com",
30 BybitEnvironment::Testnet => "https://api-testnet.bybit.com",
31 }
32}
33
34fn ws_public_subdomain(environment: BybitEnvironment) -> &'static str {
35 match environment {
36 BybitEnvironment::Mainnet => STREAM_MAINNET,
37 BybitEnvironment::Demo => STREAM_DEMO,
38 BybitEnvironment::Testnet => STREAM_TESTNET,
39 }
40}
41
42#[must_use]
44pub fn bybit_ws_public_url(
45 product_type: BybitProductType,
46 environment: BybitEnvironment,
47) -> String {
48 let subdomain = ws_public_subdomain(environment);
49 format!(
50 "wss://{subdomain}.bybit.com/v5/public/{}",
51 product_type.as_str()
52 )
53}
54
55#[must_use]
57pub const fn bybit_ws_private_url(environment: BybitEnvironment) -> &'static str {
58 match environment {
59 BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/private",
60 BybitEnvironment::Demo => "wss://stream-demo.bybit.com/v5/private",
61 BybitEnvironment::Mainnet => "wss://stream.bybit.com/v5/private",
62 }
63}
64
65#[must_use]
70pub const fn bybit_ws_trade_url(environment: BybitEnvironment) -> &'static str {
71 match environment {
72 BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/trade",
73 BybitEnvironment::Mainnet | BybitEnvironment::Demo => "wss://stream.bybit.com/v5/trade",
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use rstest::rstest;
80
81 use super::*;
82
83 #[rstest]
84 fn resolves_public_urls() {
85 assert_eq!(
86 bybit_ws_public_url(BybitProductType::Linear, BybitEnvironment::Mainnet),
87 "wss://stream.bybit.com/v5/public/linear"
88 );
89 assert_eq!(
90 bybit_ws_public_url(BybitProductType::Option, BybitEnvironment::Demo),
91 "wss://stream-demo.bybit.com/v5/public/option"
92 );
93 assert_eq!(
94 bybit_ws_public_url(BybitProductType::Inverse, BybitEnvironment::Testnet),
95 "wss://stream-testnet.bybit.com/v5/public/inverse"
96 );
97 }
98
99 #[rstest]
100 fn resolves_private_urls() {
101 assert_eq!(
102 bybit_ws_private_url(BybitEnvironment::Mainnet),
103 "wss://stream.bybit.com/v5/private"
104 );
105 assert_eq!(
106 bybit_ws_private_url(BybitEnvironment::Demo),
107 "wss://stream-demo.bybit.com/v5/private"
108 );
109 assert_eq!(
110 bybit_ws_private_url(BybitEnvironment::Testnet),
111 "wss://stream-testnet.bybit.com/v5/private"
112 );
113 }
114
115 #[rstest]
116 fn resolves_trade_urls() {
117 assert_eq!(
118 bybit_ws_trade_url(BybitEnvironment::Mainnet),
119 "wss://stream.bybit.com/v5/trade"
120 );
121 assert_eq!(
122 bybit_ws_trade_url(BybitEnvironment::Demo),
123 "wss://stream.bybit.com/v5/trade"
124 );
125 assert_eq!(
126 bybit_ws_trade_url(BybitEnvironment::Testnet),
127 "wss://stream-testnet.bybit.com/v5/trade"
128 );
129 }
130
131 #[rstest]
132 fn resolves_http_urls() {
133 assert_eq!(
134 bybit_http_base_url(BybitEnvironment::Mainnet),
135 "https://api.bybit.com"
136 );
137 assert_eq!(
138 bybit_http_base_url(BybitEnvironment::Demo),
139 "https://api-demo.bybit.com"
140 );
141 assert_eq!(
142 bybit_http_base_url(BybitEnvironment::Testnet),
143 "https://api-testnet.bybit.com"
144 );
145 }
146}