Skip to main content

nautilus_kraken/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 builders for Kraken HTTP and WebSocket endpoints.
17
18use super::{
19    consts::{
20        KRAKEN_FUTURES_DEMO_HTTP_URL, KRAKEN_FUTURES_DEMO_WS_URL, KRAKEN_FUTURES_HTTP_URL,
21        KRAKEN_FUTURES_WS_URL, KRAKEN_SPOT_HTTP_URL, KRAKEN_SPOT_WS_PRIVATE_URL,
22        KRAKEN_SPOT_WS_PUBLIC_URL,
23    },
24    enums::{KrakenEnvironment, KrakenProductType},
25};
26
27/// Returns the HTTP base URL for the given product type and environment.
28pub fn get_kraken_http_base_url(
29    product_type: KrakenProductType,
30    environment: KrakenEnvironment,
31) -> &'static str {
32    match (product_type, environment) {
33        (KrakenProductType::Spot, KrakenEnvironment::Live) => KRAKEN_SPOT_HTTP_URL,
34        (KrakenProductType::Spot, KrakenEnvironment::Demo) => {
35            panic!("Kraken Spot does not support the demo environment")
36        }
37        (KrakenProductType::Futures, KrakenEnvironment::Live) => KRAKEN_FUTURES_HTTP_URL,
38        (KrakenProductType::Futures, KrakenEnvironment::Demo) => KRAKEN_FUTURES_DEMO_HTTP_URL,
39    }
40}
41
42/// Returns the public WebSocket URL for the given product type and environment.
43pub fn get_kraken_ws_public_url(
44    product_type: KrakenProductType,
45    environment: KrakenEnvironment,
46) -> &'static str {
47    match (product_type, environment) {
48        (KrakenProductType::Spot, KrakenEnvironment::Live) => KRAKEN_SPOT_WS_PUBLIC_URL,
49        (KrakenProductType::Spot, KrakenEnvironment::Demo) => {
50            panic!("Kraken Spot does not support the demo environment")
51        }
52        (KrakenProductType::Futures, KrakenEnvironment::Live) => KRAKEN_FUTURES_WS_URL,
53        (KrakenProductType::Futures, KrakenEnvironment::Demo) => KRAKEN_FUTURES_DEMO_WS_URL,
54    }
55}
56
57/// Returns the private WebSocket URL for the given product type and environment.
58pub fn get_kraken_ws_private_url(
59    product_type: KrakenProductType,
60    environment: KrakenEnvironment,
61) -> &'static str {
62    match (product_type, environment) {
63        (KrakenProductType::Spot, KrakenEnvironment::Live) => KRAKEN_SPOT_WS_PRIVATE_URL,
64        (KrakenProductType::Spot, KrakenEnvironment::Demo) => {
65            panic!("Kraken Spot does not support the demo environment")
66        }
67        (KrakenProductType::Futures, KrakenEnvironment::Live) => KRAKEN_FUTURES_WS_URL,
68        (KrakenProductType::Futures, KrakenEnvironment::Demo) => KRAKEN_FUTURES_DEMO_WS_URL,
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use rstest::rstest;
75
76    use super::*;
77
78    #[rstest]
79    fn test_spot_live_urls() {
80        assert_eq!(
81            get_kraken_http_base_url(KrakenProductType::Spot, KrakenEnvironment::Live),
82            KRAKEN_SPOT_HTTP_URL
83        );
84        assert_eq!(
85            get_kraken_ws_public_url(KrakenProductType::Spot, KrakenEnvironment::Live),
86            KRAKEN_SPOT_WS_PUBLIC_URL
87        );
88        assert_eq!(
89            get_kraken_ws_private_url(KrakenProductType::Spot, KrakenEnvironment::Live),
90            KRAKEN_SPOT_WS_PRIVATE_URL
91        );
92    }
93
94    #[rstest]
95    fn test_futures_demo_urls() {
96        assert_eq!(
97            get_kraken_http_base_url(KrakenProductType::Futures, KrakenEnvironment::Demo),
98            KRAKEN_FUTURES_DEMO_HTTP_URL
99        );
100        assert_eq!(
101            get_kraken_ws_public_url(KrakenProductType::Futures, KrakenEnvironment::Demo),
102            KRAKEN_FUTURES_DEMO_WS_URL
103        );
104        assert_eq!(
105            get_kraken_ws_private_url(KrakenProductType::Futures, KrakenEnvironment::Demo),
106            KRAKEN_FUTURES_DEMO_WS_URL
107        );
108    }
109
110    #[rstest]
111    #[should_panic(expected = "Kraken Spot does not support the demo environment")]
112    fn test_spot_demo_panics() {
113        get_kraken_http_base_url(KrakenProductType::Spot, KrakenEnvironment::Demo);
114    }
115}