nautilus_tardis/python/
http.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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
16use nautilus_core::{
17    UnixNanos,
18    python::{IntoPyObjectNautilusExt, enums::parse_enum, to_pyruntime_err},
19};
20use nautilus_model::python::instruments::instrument_any_to_pyobject;
21use pyo3::prelude::*;
22
23use crate::{
24    enums::TardisExchange,
25    http::{TardisHttpClient, query::InstrumentFilterBuilder},
26};
27
28#[pymethods]
29impl TardisHttpClient {
30    #[new]
31    #[pyo3(signature = (api_key=None, base_url=None, timeout_secs=None, normalize_symbols=true))]
32    fn py_new(
33        api_key: Option<&str>,
34        base_url: Option<&str>,
35        timeout_secs: Option<u64>,
36        normalize_symbols: bool,
37    ) -> PyResult<Self> {
38        Self::new(api_key, base_url, timeout_secs, normalize_symbols).map_err(to_pyruntime_err)
39    }
40
41    #[getter]
42    #[pyo3(name = "api_key")]
43    fn py_api_key(&self) -> Option<&str> {
44        self.credential().map(|c| c.api_key())
45    }
46
47    #[getter]
48    #[pyo3(name = "api_key_masked")]
49    fn py_api_key_masked(&self) -> Option<String> {
50        self.credential().map(|c| c.api_key_masked())
51    }
52
53    #[allow(clippy::too_many_arguments)]
54    #[pyo3(name = "instruments")]
55    #[pyo3(signature = (exchange, symbol=None, base_currency=None, quote_currency=None, instrument_type=None, contract_type=None, active=None, start=None, end=None, available_offset=None, effective=None, ts_init=None))]
56    fn py_instruments<'py>(
57        &self,
58        exchange: String,
59        symbol: Option<String>,
60        base_currency: Option<Vec<String>>,
61        quote_currency: Option<Vec<String>>,
62        instrument_type: Option<Vec<String>>,
63        contract_type: Option<Vec<String>>,
64        active: Option<bool>,
65        start: Option<u64>,
66        end: Option<u64>,
67        available_offset: Option<u64>,
68        effective: Option<u64>,
69        ts_init: Option<u64>,
70        py: Python<'py>,
71    ) -> PyResult<Bound<'py, PyAny>> {
72        let exchange: TardisExchange = parse_enum(&exchange, stringify!(exchange))?;
73
74        let filter = InstrumentFilterBuilder::default()
75            .base_currency(base_currency)
76            .quote_currency(quote_currency)
77            .instrument_type(instrument_type)
78            .contract_type(contract_type)
79            .active(active)
80            // NOTE: The Tardis instruments metadata API does not function correctly when using
81            // the `availableSince` and `availableTo` params.
82            // .available_since(start.map(|x| DateTime::from_timestamp_nanos(x as i64)))
83            // .available_to(end.map(|x| DateTime::from_timestamp_nanos(x as i64)))
84            .build()
85            .unwrap(); // SAFETY: Safe since all fields are Option
86
87        let self_clone = self.clone();
88
89        pyo3_async_runtimes::tokio::future_into_py(py, async move {
90            let instruments = self_clone
91                .instruments(
92                    exchange,
93                    symbol.as_deref(),
94                    Some(&filter),
95                    start.map(UnixNanos::from),
96                    end.map(UnixNanos::from),
97                    available_offset.map(UnixNanos::from),
98                    effective.map(UnixNanos::from),
99                    ts_init.map(UnixNanos::from),
100                )
101                .await
102                .map_err(to_pyruntime_err)?;
103
104            Python::attach(|py| {
105                let mut py_instruments = Vec::new();
106                for inst in instruments {
107                    py_instruments.push(instrument_any_to_pyobject(py, inst)?);
108                }
109                Ok(py_instruments.into_py_any_unwrap(py))
110            })
111        })
112    }
113}