Skip to main content

nautilus_coinbase/python/
config.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//! Python bindings for Coinbase configuration.
17
18use nautilus_core::string::secret::SecretString;
19use nautilus_model::{enums::AccountType, identifiers::AccountId};
20use nautilus_network::websocket::TransportBackend;
21use pyo3::pymethods;
22use rust_decimal::Decimal;
23
24use crate::{
25    common::enums::{CoinbaseEnvironment, CoinbaseMarginType},
26    config::{CoinbaseDataClientConfig, CoinbaseExecutionClientConfig},
27};
28
29#[pymethods]
30#[pyo3_stub_gen::derive::gen_stub_pymethods]
31impl CoinbaseDataClientConfig {
32    /// Configuration for the Coinbase live data client.
33    #[new]
34    #[pyo3(signature = (
35        api_key = None,
36        api_secret = None,
37        base_url_rest = None,
38        base_url_ws = None,
39        proxy_url = None,
40        environment = None,
41        http_timeout_secs = None,
42        ws_timeout_secs = None,
43        update_instruments_interval_mins = None,
44        derivatives_poll_interval_secs = None,
45        book_snapshot_timeout_secs = None,
46        transport_backend = None,
47    ))]
48    #[expect(clippy::too_many_arguments)]
49    fn py_new(
50        api_key: Option<String>,
51        api_secret: Option<String>,
52        base_url_rest: Option<String>,
53        base_url_ws: Option<String>,
54        proxy_url: Option<String>,
55        environment: Option<CoinbaseEnvironment>,
56        http_timeout_secs: Option<u64>,
57        ws_timeout_secs: Option<u64>,
58        update_instruments_interval_mins: Option<u64>,
59        derivatives_poll_interval_secs: Option<u64>,
60        book_snapshot_timeout_secs: Option<u64>,
61        transport_backend: Option<TransportBackend>,
62    ) -> Self {
63        let defaults = Self::default();
64        Self {
65            api_key: api_key.map(SecretString::from),
66            api_secret: api_secret.map(SecretString::from),
67            base_url_rest,
68            base_url_ws,
69            proxy_url: proxy_url.map(SecretString::from),
70            environment: environment.unwrap_or(defaults.environment),
71            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
72            ws_timeout_secs: ws_timeout_secs.unwrap_or(defaults.ws_timeout_secs),
73            update_instruments_interval_mins: update_instruments_interval_mins
74                .unwrap_or(defaults.update_instruments_interval_mins),
75            derivatives_poll_interval_secs: derivatives_poll_interval_secs
76                .unwrap_or(defaults.derivatives_poll_interval_secs),
77            book_snapshot_timeout_secs: book_snapshot_timeout_secs
78                .unwrap_or(defaults.book_snapshot_timeout_secs),
79            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
80        }
81    }
82
83    #[getter]
84    const fn has_proxy_url(&self) -> bool {
85        self.proxy_url.is_some()
86    }
87
88    fn __repr__(&self) -> String {
89        stringify!(CoinbaseDataClientConfig).to_string()
90    }
91}
92
93#[pymethods]
94#[pyo3_stub_gen::derive::gen_stub_pymethods]
95impl CoinbaseExecutionClientConfig {
96    /// Configuration for the Coinbase live execution client.
97    #[new]
98    #[pyo3(signature = (
99        account_id = None,
100        api_key = None,
101        api_secret = None,
102        base_url_rest = None,
103        base_url_ws = None,
104        proxy_url = None,
105        environment = None,
106        http_timeout_secs = None,
107        max_retries = None,
108        retry_delay_initial_ms = None,
109        retry_delay_max_ms = None,
110        account_type = None,
111        default_margin_type = None,
112        default_leverage = None,
113        retail_portfolio_id = None,
114        transport_backend = None,
115    ))]
116    #[expect(clippy::too_many_arguments)]
117    fn py_new(
118        account_id: Option<AccountId>,
119        api_key: Option<String>,
120        api_secret: Option<String>,
121        base_url_rest: Option<String>,
122        base_url_ws: Option<String>,
123        proxy_url: Option<String>,
124        environment: Option<CoinbaseEnvironment>,
125        http_timeout_secs: Option<u64>,
126        max_retries: Option<u32>,
127        retry_delay_initial_ms: Option<u64>,
128        retry_delay_max_ms: Option<u64>,
129        account_type: Option<AccountType>,
130        default_margin_type: Option<CoinbaseMarginType>,
131        default_leverage: Option<Decimal>,
132        retail_portfolio_id: Option<String>,
133        transport_backend: Option<TransportBackend>,
134    ) -> Self {
135        let defaults = Self::default();
136        Self {
137            account_id: account_id.unwrap_or(defaults.account_id),
138            api_key: api_key.map(SecretString::from),
139            api_secret: api_secret.map(SecretString::from),
140            base_url_rest,
141            base_url_ws,
142            proxy_url: proxy_url.map(SecretString::from),
143            environment: environment.unwrap_or(defaults.environment),
144            http_timeout_secs: http_timeout_secs.unwrap_or(defaults.http_timeout_secs),
145            max_retries: max_retries.unwrap_or(defaults.max_retries),
146            retry_delay_initial_ms: retry_delay_initial_ms
147                .unwrap_or(defaults.retry_delay_initial_ms),
148            retry_delay_max_ms: retry_delay_max_ms.unwrap_or(defaults.retry_delay_max_ms),
149            account_type: account_type.unwrap_or(defaults.account_type),
150            default_margin_type,
151            default_leverage,
152            retail_portfolio_id,
153            transport_backend: transport_backend.unwrap_or(defaults.transport_backend),
154        }
155    }
156
157    #[getter]
158    const fn has_proxy_url(&self) -> bool {
159        self.proxy_url.is_some()
160    }
161
162    fn __repr__(&self) -> String {
163        stringify!(CoinbaseExecutionClientConfig).to_string()
164    }
165}