betfair_rs/
lib.rs

1//! # betfair-rs
2//!
3//! A high-performance Rust library for interacting with the Betfair Exchange API,
4//! featuring real-time market data streaming, order management, and an interactive
5//! terminal dashboard for trading.
6//!
7//! ## Quick Start
8//!
9//! ```no_run
10//! use betfair_rs::{BetfairClient, Config};
11//! use betfair_rs::dto::market::{MarketFilter, ListMarketCatalogueRequest};
12//!
13//! # async fn example() -> anyhow::Result<()> {
14//! // Load configuration from config.toml
15//! let config = Config::new()?;
16//!
17//! // Create API client and login
18//! let mut client = BetfairClient::new(config);
19//! client.login().await?;
20//!
21//! // List available sports (event types)
22//! let sports = client.list_sports(None).await?;
23//!
24//! // Get markets for a specific event type
25//! let filter = MarketFilter {
26//!     event_type_ids: Some(vec!["1".to_string()]), // Soccer
27//!     ..Default::default()
28//! };
29//! let request = ListMarketCatalogueRequest {
30//!     filter,
31//!     market_projection: None,
32//!     sort: None,
33//!     max_results: Some(10),
34//!     locale: None,
35//! };
36//! let markets = client.list_market_catalogue(request).await?;
37//! # Ok(())
38//! # }
39//! ```
40//!
41//! ## Features
42//!
43//! - **REST API Client**: Complete implementation of Betfair's JSON-RPC API
44//! - **Real-time Streaming**: WebSocket streaming for live market data and orderbook updates
45//! - **Order Management**: Place, cancel, and monitor orders programmatically
46//! - **Rate Limiting**: Built-in rate limiting to respect API limits
47//! - **Retry Logic**: Automatic retry with exponential backoff for transient failures
48//! - **Terminal Dashboard**: Interactive TUI for real-time trading (binary included)
49//!
50//! ## Configuration
51//!
52//! Create a `config.toml` file in your project root:
53//!
54//! ```toml
55//! [betfair]
56//! username = "your_username"
57//! password = "your_password"
58//! api_key = "your_api_key"
59//! pem_path = "/path/to/certificate.pem"
60//! ```
61//!
62//! ## Example: Streaming Market Data
63//!
64//! ```no_run
65//! use betfair_rs::{BetfairClient, StreamingClient, Config};
66//! use std::time::Duration;
67//!
68//! # async fn example() -> anyhow::Result<()> {
69//! let config = Config::new()?;
70//! let mut api_client = BetfairClient::new(config.clone());
71//! api_client.login().await?;
72//!
73//! let session_token = api_client.get_session_token()
74//!     .ok_or_else(|| anyhow::anyhow!("No session token"))?;
75//!
76//! let mut streaming_client = StreamingClient::with_session_token(
77//!     config.betfair.api_key.clone(),
78//!     session_token
79//! );
80//!
81//! streaming_client.start().await?;
82//! streaming_client.subscribe_to_market("1.234567".to_string(), 10).await?;
83//!
84//! let orderbooks = streaming_client.get_orderbooks();
85//! # Ok(())
86//! # }
87//! ```
88
89use std::sync::Once;
90
91pub mod account;
92pub mod api_client;
93pub mod config;
94pub mod connection_state;
95pub mod dto;
96pub mod msg_model;
97pub mod order;
98pub mod order_cache;
99pub mod orderbook;
100mod public_data;
101mod rate_limiter;
102mod retry;
103mod streamer;
104pub mod streaming_client;
105pub mod unified_client;
106
107static CRYPTO_PROVIDER_INIT: Once = Once::new();
108
109pub(crate) fn ensure_crypto_provider() {
110    CRYPTO_PROVIDER_INIT.call_once(|| {
111        let _ = rustls::crypto::ring::default_provider().install_default();
112    });
113}
114
115pub use api_client::RestClient;
116pub use config::Config;
117pub use streaming_client::StreamingClient;
118pub use unified_client::BetfairClient;
119
120// Type aliases for backward compatibility and ease of use
121pub type BetfairApiClient = RestClient;
122pub type UnifiedBetfairClient = BetfairClient;