Skip to main content

nautilus_okx/common/
urls.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! URL helpers and endpoint metadata for OKX services.
17
18const OKX_HTTP_URL: &str = "https://www.okx.com";
19const OKX_WS_PUBLIC_URL: &str = "wss://ws.okx.com:8443/ws/v5/public";
20const OKX_WS_PRIVATE_URL: &str = "wss://ws.okx.com:8443/ws/v5/private";
21const OKX_WS_BUSINESS_URL: &str = "wss://ws.okx.com:8443/ws/v5/business";
22const OKX_DEMO_WS_PUBLIC_URL: &str = "wss://wspap.okx.com:8443/ws/v5/public";
23const OKX_DEMO_WS_PRIVATE_URL: &str = "wss://wspap.okx.com:8443/ws/v5/private";
24const OKX_DEMO_WS_BUSINESS_URL: &str = "wss://wspap.okx.com:8443/ws/v5/business";
25
26/// OKX endpoint types for determining URL and authentication requirements.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[cfg_attr(feature = "python", pyo3::pyclass)]
29pub enum OKXEndpointType {
30    Public,
31    Private,
32    Business,
33}
34
35/// Checks if endpoint requires authentication.
36pub fn requires_authentication(endpoint_type: OKXEndpointType) -> bool {
37    matches!(
38        endpoint_type,
39        OKXEndpointType::Private | OKXEndpointType::Business
40    )
41}
42
43/// Returns the HTTP base URL.
44#[must_use]
45pub const fn get_http_base_url() -> &'static str {
46    OKX_HTTP_URL
47}
48
49/// Returns the WebSocket base URL for public data (market data).
50#[must_use]
51pub const fn get_ws_base_url_public(is_demo: bool) -> &'static str {
52    if is_demo {
53        OKX_DEMO_WS_PUBLIC_URL
54    } else {
55        OKX_WS_PUBLIC_URL
56    }
57}
58
59/// Returns the WebSocket base URL for private data (account/order management).
60#[must_use]
61pub const fn get_ws_base_url_private(is_demo: bool) -> &'static str {
62    if is_demo {
63        OKX_DEMO_WS_PRIVATE_URL
64    } else {
65        OKX_WS_PRIVATE_URL
66    }
67}
68
69/// Returns the WebSocket base URL for business data (bars/candlesticks).
70#[must_use]
71pub const fn get_ws_base_url_business(is_demo: bool) -> &'static str {
72    if is_demo {
73        OKX_DEMO_WS_BUSINESS_URL
74    } else {
75        OKX_WS_BUSINESS_URL
76    }
77}
78
79/// Returns WebSocket URL by endpoint type.
80#[must_use]
81pub const fn get_ws_url(endpoint_type: OKXEndpointType, is_demo: bool) -> &'static str {
82    match endpoint_type {
83        OKXEndpointType::Public => get_ws_base_url_public(is_demo),
84        OKXEndpointType::Private => get_ws_base_url_private(is_demo),
85        OKXEndpointType::Business => get_ws_base_url_business(is_demo),
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use rstest::rstest;
92
93    use super::*;
94
95    #[rstest]
96    fn test_endpoint_authentication() {
97        assert!(!requires_authentication(OKXEndpointType::Public));
98        assert!(requires_authentication(OKXEndpointType::Private));
99        assert!(requires_authentication(OKXEndpointType::Business));
100    }
101
102    #[rstest]
103    fn test_http_base_url() {
104        assert_eq!(get_http_base_url(), OKX_HTTP_URL);
105    }
106
107    #[rstest]
108    fn test_ws_urls_production() {
109        assert_eq!(get_ws_base_url_public(false), OKX_WS_PUBLIC_URL);
110        assert_eq!(get_ws_base_url_private(false), OKX_WS_PRIVATE_URL);
111        assert_eq!(get_ws_base_url_business(false), OKX_WS_BUSINESS_URL);
112    }
113
114    #[rstest]
115    fn test_ws_urls_demo() {
116        assert_eq!(get_ws_base_url_public(true), OKX_DEMO_WS_PUBLIC_URL);
117        assert_eq!(get_ws_base_url_private(true), OKX_DEMO_WS_PRIVATE_URL);
118        assert_eq!(get_ws_base_url_business(true), OKX_DEMO_WS_BUSINESS_URL);
119    }
120
121    #[rstest]
122    fn test_get_ws_url_by_type() {
123        assert_eq!(
124            get_ws_url(OKXEndpointType::Public, false),
125            get_ws_base_url_public(false)
126        );
127        assert_eq!(
128            get_ws_url(OKXEndpointType::Private, false),
129            get_ws_base_url_private(false)
130        );
131        assert_eq!(
132            get_ws_url(OKXEndpointType::Business, false),
133            get_ws_base_url_business(false)
134        );
135    }
136}