kiteconnect_async_wasm/lib.rs
1//! # kiteconnect-async-wasm
2//!
3//! A modern, async Rust implementation of the Zerodha KiteConnect API with WASM support.
4//! This library provides comprehensive access to KiteConnect's REST APIs for trading,
5//! portfolio management, and market data.
6//!
7//! ## Features
8//!
9//! - **๐ Async/Await**: Built with modern Rust async patterns using `tokio`
10//! - **๐ WASM Compatible**: Run in browsers with WebAssembly support
11//! - **๐ Cross-Platform**: Native (Linux, macOS, Windows) and Web targets
12//! - **๐ฆ Modern Dependencies**: Updated to latest Rust ecosystem libraries
13//! - **๐งช Well Tested**: Comprehensive test coverage with mocked responses
14//! - **โก High Performance**: Efficient HTTP client with connection pooling
15//! - **๐ก๏ธ Type Safe**: Leverages Rust's type system for safer API interactions
16//!
17//! ## Quick Start
18//!
19//! Add to your `Cargo.toml`:
20//!
21//! ```toml
22//! [dependencies]
23//! kiteconnect-async-wasm = { version = "0.1.0", features = ["native"] }
24//!
25//! # For WASM targets
26//! # kiteconnect-async-wasm = { version = "0.1.0", features = ["wasm"] }
27//! ```
28//!
29//! ## Basic Usage
30//!
31//! ```rust,no_run
32//! use kiteconnect_async_wasm::connect::KiteConnect;
33//! use serde_json::Value as JsonValue;
34//!
35//! #[tokio::main]
36//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
37//! // Initialize KiteConnect client
38//! let mut kiteconnect = KiteConnect::new("<YOUR-API-KEY>", "");
39//!
40//! // Step 1: Get login URL
41//! let login_url = kiteconnect.login_url();
42//! println!("Login URL: {}", login_url);
43//!
44//! // Step 2: After user login, generate session with request token
45//! let session_response = kiteconnect
46//! .generate_session("<REQUEST-TOKEN>", "<API-SECRET>")
47//! .await?;
48//! println!("Session: {:?}", session_response);
49//!
50//! // Step 3: Use the API (access token is automatically set)
51//! let holdings: JsonValue = kiteconnect.holdings().await?;
52//! println!("Holdings: {:?}", holdings);
53//!
54//! Ok(())
55//! }
56//! ```
57//!
58//! ## Available APIs
59//!
60//! The library provides access to all KiteConnect REST APIs:
61//!
62//! ### Authentication
63//! - `login_url()` - Generate login URL
64//! - `generate_session()` - Create session with request token
65//! - `invalidate_session()` - Logout user
66//!
67//! ### Portfolio
68//! - `holdings()` - Get user holdings
69//! - `positions()` - Get user positions
70//! - `margins()` - Get account margins
71//!
72//! ### Orders
73//! - `orders()` - Get all orders
74//! - `order_trades()` - Get trades for specific order
75//! - `trades()` - Get all trades
76//!
77//! ### Market Data
78//! - `instruments()` - Get instrument list
79//! - `trigger_range()` - Get trigger range for instruments
80//!
81//! ### Mutual Funds
82//! - `mf_orders()` - Get mutual fund orders
83//! - `mf_instruments()` - Get mutual fund instruments
84//!
85//! ## Error Handling
86//!
87//! The library uses `anyhow::Result` for comprehensive error handling:
88//!
89//! ```rust,no_run
90//! # use kiteconnect_async_wasm::connect::KiteConnect;
91//! # #[tokio::main]
92//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
93//! # let kiteconnect = KiteConnect::new("", "");
94//! match kiteconnect.holdings().await {
95//! Ok(holdings) => println!("Holdings: {:?}", holdings),
96//! Err(e) => eprintln!("Error fetching holdings: {}", e),
97//! }
98//! # Ok(())
99//! # }
100//! ```
101//!
102//! ## Platform-Specific Features
103//!
104//! ### Native (Tokio)
105//! - Full CSV parsing for instruments
106//! - Complete async/await support
107//! - High-performance HTTP client
108//!
109//! ### WASM (Browser)
110//! - All APIs supported
111//! - Raw CSV returned for client-side parsing
112//! - Compatible with web frameworks
113//!
114//! ## Examples
115//!
116//! See the `examples/` directory for comprehensive usage examples:
117//! - `connect_sample.rs` - Basic API usage
118//! - `async_connect_example.rs` - Advanced async patterns
119//!
120//! ## Thread Safety
121//!
122//! The `KiteConnect` struct is `Clone + Send + Sync`, making it safe to use across
123//! multiple threads and async tasks. The underlying HTTP client uses connection
124//! pooling for optimal performance.
125//!
126//! ```rust,no_run
127//! # use kiteconnect_async_wasm::connect::KiteConnect;
128//! # #[tokio::main]
129//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
130//! let kiteconnect = KiteConnect::new("<API-KEY>", "<ACCESS-TOKEN>");
131//!
132//! // Clone for use in different tasks
133//! let kc1 = kiteconnect.clone();
134//! let kc2 = kiteconnect.clone();
135//!
136//! // Use in concurrent tasks
137//! let (holdings, positions) = tokio::try_join!(
138//! kc1.holdings(),
139//! kc2.positions()
140//! )?;
141//! # Ok(())
142//! # }
143//! ```
144//!
145#[cfg(test)]
146extern crate mockito;
147
148pub mod connect;