nautilus_network/python/mod.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
16//! Python bindings from `pyo3`.
17
18// We need to allow `unexpected_cfgs` because the PyO3 macros internally check for
19// the `gil-refs` feature. We don’t define or enable `gil-refs` ourselves (due to a
20// memory leak), so the compiler raises an error about an unknown cfg feature.
21// This attribute prevents those errors without actually enabling `gil-refs`.
22#![allow(unexpected_cfgs)]
23
24pub mod http;
25pub mod socket;
26pub mod websocket;
27
28use pyo3::{PyTypeCheck, prelude::*};
29
30use crate::python::{
31 http::{HttpError, HttpTimeoutError},
32 websocket::WebSocketClientError,
33};
34
35/// Loaded as nautilus_pyo3.network
36///
37/// # Errors
38///
39/// Returns a `PyErr` if registering any module components fails.
40#[pymodule]
41pub fn network(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
42 m.add_class::<crate::http::HttpClient>()?;
43 m.add_class::<crate::http::HttpMethod>()?;
44 m.add_class::<crate::http::HttpResponse>()?;
45 m.add_class::<crate::ratelimiter::quota::Quota>()?;
46 m.add_class::<crate::websocket::WebSocketClient>()?;
47 m.add_class::<crate::websocket::WebSocketConfig>()?;
48 m.add_class::<crate::socket::SocketClient>()?;
49 m.add_class::<crate::socket::SocketConfig>()?;
50
51 // Add error classes
52 m.add(
53 <WebSocketClientError as PyTypeCheck>::NAME,
54 m.py().get_type::<WebSocketClientError>(),
55 )?;
56 m.add(
57 <HttpError as PyTypeCheck>::NAME,
58 m.py().get_type::<HttpError>(),
59 )?;
60 m.add(
61 <HttpTimeoutError as PyTypeCheck>::NAME,
62 m.py().get_type::<HttpTimeoutError>(),
63 )?;
64
65 Ok(())
66}