nautilus_interactive_brokers/python/
gateway.rs1#[cfg(feature = "gateway")]
19use nautilus_common::live::get_runtime;
20#[cfg(feature = "gateway")]
21use nautilus_core::python::to_pyruntime_err;
22#[cfg(feature = "gateway")]
23use pyo3::prelude::*;
24
25#[cfg(feature = "gateway")]
26use crate::config::DockerizedIBGatewayConfig;
27#[cfg(feature = "gateway")]
28use crate::gateway::dockerized::DockerizedIBGateway;
29
30#[cfg(feature = "gateway")]
31#[pymethods]
32impl DockerizedIBGateway {
33 #[new]
34 fn py_new(config: DockerizedIBGatewayConfig) -> PyResult<Self> {
35 Self::new(config).map_err(|e| to_pyruntime_err(format!("{e}")))
36 }
37
38 fn __repr__(&self) -> String {
39 format!(
40 "DockerizedIBGateway(container_name={}, host={}, port={})",
41 self.container_name(),
42 self.host(),
43 self.port()
44 )
45 }
46
47 #[getter("container_name")]
49 fn py_container_name(&self) -> String {
50 self.container_name().to_string()
51 }
52
53 #[getter("host")]
55 fn py_host(&self) -> String {
56 self.host().to_string()
57 }
58
59 #[getter("port")]
61 fn py_port(&self) -> u16 {
62 self.port()
63 }
64
65 #[pyo3(name = "start")]
71 fn py_start<'py>(&self, py: Python<'py>, wait: Option<u64>) -> PyResult<Bound<'py, PyAny>> {
72 let mut gateway = self.clone();
73 pyo3_async_runtimes::tokio::future_into_py(py, async move {
74 gateway
75 .start(wait)
76 .await
77 .map_err(|e| to_pyruntime_err(format!("{e}")))
78 })
79 }
80
81 #[pyo3(name = "start_blocking")]
82 fn py_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
83 let mut gateway = self.clone();
84 get_runtime()
85 .block_on(async move { gateway.start(wait).await })
86 .map_err(|e| to_pyruntime_err(format!("{e}")))
87 }
88
89 #[pyo3(name = "safe_start")]
95 fn py_safe_start<'py>(
96 &self,
97 py: Python<'py>,
98 wait: Option<u64>,
99 ) -> PyResult<Bound<'py, PyAny>> {
100 let mut gateway = self.clone();
101 pyo3_async_runtimes::tokio::future_into_py(py, async move {
102 gateway
103 .safe_start(wait)
104 .await
105 .map_err(|e| to_pyruntime_err(format!("{e}")))
106 })
107 }
108
109 #[pyo3(name = "safe_start_blocking")]
110 fn py_safe_start_blocking(&self, wait: Option<u64>) -> PyResult<()> {
111 let mut gateway = self.clone();
112 get_runtime()
113 .block_on(async move { gateway.safe_start(wait).await })
114 .map_err(|e| to_pyruntime_err(format!("{e}")))
115 }
116
117 #[pyo3(name = "stop")]
119 fn py_stop<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
120 let gateway = self.clone();
121 pyo3_async_runtimes::tokio::future_into_py(py, async move {
122 gateway
123 .stop()
124 .await
125 .map_err(|e| to_pyruntime_err(format!("{e}")))
126 })
127 }
128
129 #[pyo3(name = "stop_blocking")]
130 fn py_stop_blocking(&self) -> PyResult<()> {
131 let gateway = self.clone();
132 get_runtime()
133 .block_on(async move { gateway.stop().await })
134 .map_err(|e| to_pyruntime_err(format!("{e}")))
135 }
136
137 #[pyo3(name = "container_status")]
139 fn py_container_status<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
140 let gateway = self.clone();
141 pyo3_async_runtimes::tokio::future_into_py(py, async move {
142 gateway
143 .container_status()
144 .await
145 .map_err(|e| to_pyruntime_err(format!("{e}")))
146 })
147 }
148}