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]
62pub const fn bybit_ws_private_url(environment: BybitEnvironment) -> &'static str {
63 match environment {
64 BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/private",
65 BybitEnvironment::Mainnet | BybitEnvironment::Demo => "wss://stream.bybit.com/v5/private",
66 }
67}
68
69#[must_use]
71pub const fn bybit_ws_trade_url(environment: BybitEnvironment) -> &'static str {
72 match environment {
73 BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/trade",
74 BybitEnvironment::Mainnet | BybitEnvironment::Demo => "wss://stream.bybit.com/v5/trade",
75 }
76}
77
78#[cfg(test)]
83mod tests {
84 use rstest::rstest;
85
86 use super::*;
87
88 #[rstest]
89 fn resolves_public_urls() {
90 assert_eq!(
91 bybit_ws_public_url(BybitProductType::Linear, BybitEnvironment::Mainnet),
92 "wss://stream.bybit.com/v5/public/linear"
93 );
94 assert_eq!(
95 bybit_ws_public_url(BybitProductType::Option, BybitEnvironment::Demo),
96 "wss://stream-demo.bybit.com/v5/public/option"
97 );
98 assert_eq!(
99 bybit_ws_public_url(BybitProductType::Inverse, BybitEnvironment::Testnet),
100 "wss://stream-testnet.bybit.com/v5/public/inverse"
101 );
102 }
103
104 #[rstest]
105 fn resolves_private_urls() {
106 assert_eq!(
107 bybit_ws_private_url(BybitEnvironment::Mainnet),
108 "wss://stream.bybit.com/v5/private"
109 );
110 assert_eq!(
111 bybit_ws_private_url(BybitEnvironment::Demo),
112 "wss://stream.bybit.com/v5/private"
113 );
114 assert_eq!(
115 bybit_ws_private_url(BybitEnvironment::Testnet),
116 "wss://stream-testnet.bybit.com/v5/private"
117 );
118 }
119
120 #[rstest]
121 fn resolves_trade_urls() {
122 assert_eq!(
123 bybit_ws_trade_url(BybitEnvironment::Mainnet),
124 "wss://stream.bybit.com/v5/trade"
125 );
126 assert_eq!(
127 bybit_ws_trade_url(BybitEnvironment::Demo),
128 "wss://stream.bybit.com/v5/trade"
129 );
130 assert_eq!(
131 bybit_ws_trade_url(BybitEnvironment::Testnet),
132 "wss://stream-testnet.bybit.com/v5/trade"
133 );
134 }
135
136 #[rstest]
137 fn resolves_http_urls() {
138 assert_eq!(
139 bybit_http_base_url(BybitEnvironment::Mainnet),
140 "https://api.bybit.com"
141 );
142 assert_eq!(
143 bybit_http_base_url(BybitEnvironment::Demo),
144 "https://api-demo.bybit.com"
145 );
146 assert_eq!(
147 bybit_http_base_url(BybitEnvironment::Testnet),
148 "https://api-testnet.bybit.com"
149 );
150 }
151}