nautilus_tardis/python/
http.rs1use 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 .build()
85 .unwrap(); 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}