Skip to main content

nautilus_tardis/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//! Tardis base URL constants and resolution helpers.
17
18use super::consts::TARDIS_MACHINE_WS_URL;
19
20/// Default Tardis REST API base URL.
21pub const TARDIS_HTTP_BASE_URL: &str = "https://api.tardis.dev/v1";
22
23/// Resolves the Tardis Machine WebSocket base URL from an explicit value or the
24/// `TARDIS_MACHINE_WS_URL` environment variable.
25///
26/// # Errors
27///
28/// Returns an error if neither `url` nor the environment variable is set.
29pub fn resolve_ws_base_url(url: Option<&str>) -> anyhow::Result<String> {
30    url.map(ToString::to_string)
31        .or_else(|| std::env::var(TARDIS_MACHINE_WS_URL).ok())
32        .ok_or_else(|| {
33            anyhow::anyhow!(
34                "Tardis Machine `base_url` must be provided or \
35                 set in the '{TARDIS_MACHINE_WS_URL}' environment variable"
36            )
37        })
38}
39
40#[cfg(test)]
41mod tests {
42    use rstest::rstest;
43
44    use super::*;
45
46    #[rstest]
47    fn test_resolve_ws_base_url_with_explicit_value() {
48        let result = resolve_ws_base_url(Some("ws://localhost:8001")).unwrap();
49        assert_eq!(result, "ws://localhost:8001");
50    }
51
52    #[rstest]
53    fn test_resolve_ws_base_url_prefers_explicit_value() {
54        let result = resolve_ws_base_url(Some("ws://custom:9999")).unwrap();
55        assert_eq!(result, "ws://custom:9999");
56    }
57}