Skip to main content

hpx_transport/
lib.rs

1//! # hpx-transport
2//!
3//! Exchange SDK toolkit for cryptocurrency trading applications.
4//!
5//! This crate builds on `hpx` to provide exchange-specific functionality:
6//!
7//! - **Authentication**: API key, HMAC signing, and custom auth strategies
8//! - **WebSocket**: Single-task connection with `Connection`/`Handle`/`Stream` split API
9//! - **SSE** *(feature `sse`)*: Server-Sent Events transport with auto-reconnection,
10//!   protocol handlers, and `SseConnection`/`SseHandle`/`SseStream` split API
11//! - **Typed Responses**: Generic response wrapper with metadata
12//! - **Rate Limiting**: Token bucket rate limiter
13//! - **Metrics**: OpenTelemetry metrics integration
14//!
15//! ## Quick Start
16//!
17//! ```rust,no_run
18//! use hpx_transport::{
19//!     auth::ApiKeyAuth,
20//!     exchange::{ExchangeClient, RestClient, RestConfig},
21//! };
22//!
23//! #[tokio::main]
24//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
25//!     let config =
26//!         RestConfig::new("https://api.example.com").timeout(std::time::Duration::from_secs(30));
27//!
28//!     let auth = ApiKeyAuth::header("X-API-Key", "my-api-key");
29//!     let client = RestClient::new(config, auth)?;
30//!
31//!     // Use the client...
32//!     Ok(())
33//! }
34//! ```
35//!
36//! ## WebSocket Quick Start (Split API)
37//!
38//! ```rust,no_run
39//! use hpx_transport::websocket::{Connection, Event, WsConfig, handlers::GenericJsonHandler};
40//!
41//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
42//! let config = WsConfig::new("wss://api.exchange.com/ws");
43//! let handler = GenericJsonHandler::new();
44//!
45//! let connection = Connection::connect_stream(config, handler).await?;
46//! let (handle, mut stream) = connection.split();
47//!
48//! handle.subscribe("trades.BTC").await?;
49//! while let Some(event) = stream.next().await {
50//!     if let Event::Message(msg) = event {
51//!         println!("Control/unknown message: {:?}", msg.kind);
52//!     }
53//! }
54//! # Ok(())
55//! # }
56//! ```
57//!
58//! ## Rate Limiting Example
59//!
60//! ```rust
61//! use hpx_transport::rate_limit::RateLimiter;
62//!
63//! let limiter = RateLimiter::new();
64//! limiter.add_limit("orders", 10, 1.0)?; // 10 capacity, 1/sec refill
65//!
66//! if limiter.try_acquire("orders") {
67//!     println!("Request allowed");
68//! }
69//! # Ok::<(), hpx_transport::rate_limit::RateLimitError>(())
70//! ```
71
72pub mod auth;
73pub mod error;
74pub mod exchange;
75#[cfg(feature = "metrics")]
76#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
77pub mod metrics;
78pub mod rate_limit;
79#[cfg(any(feature = "sse", feature = "ws-yawc", feature = "ws-fastwebsockets"))]
80mod reconnect;
81#[cfg(feature = "sse")]
82pub mod sse;
83pub mod typed;
84#[cfg(any(feature = "ws-yawc", feature = "ws-fastwebsockets"))]
85pub mod websocket;
86
87// Re-export commonly used types
88pub use auth::{ApiKeyAuth, Authentication, BearerAuth, HmacAuth, NoAuth};
89pub use error::{TransportError, TransportResult};
90pub use exchange::{ExchangeClient, RestClient, RestConfig};
91pub use rate_limit::{RateLimitError, RateLimiter};
92pub use typed::{ApiError, TypedResponse};
93#[cfg(any(feature = "ws-yawc", feature = "ws-fastwebsockets"))]
94pub use websocket::{
95    // Core connection API
96    Connection,
97    ConnectionEpoch,
98    ConnectionHandle,
99    // Connection state
100    ConnectionState,
101    ConnectionStream,
102    Event,
103    MessageKind,
104    // Protocol abstraction
105    ProtocolHandler,
106    RequestId,
107    // Subscription guard
108    SubscriptionGuard,
109    Topic,
110    // Core client types
111    WsClient,
112    WsConfig,
113    WsMessage,
114};