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//! ## Feature Flags
18//!
19//! This crate supports multiple feature flags to enable platform-specific functionality:
20//!
21//! - **`native`** (default): Enables native platform support with tokio, file I/O, and CSV parsing
22//! - Includes: `tokio`, `sha2`, `csv` dependencies
23//! - Best for: Desktop applications, servers, CLI tools
24//!
25//! - **`wasm`**: Enables WebAssembly support with browser APIs and CSV parsing
26//! - Includes: `wasm-bindgen`, `web-sys`, `js-sys`, `gloo-utils`, `csv-core` dependencies
27//! - Features: Browser-compatible CSV parsing using csv-core
28//! - Best for: Browser applications, web workers
29//!
30//! - **`debug`**: Enables additional logging and debugging features
31//! - Includes: Enhanced `log` output
32//! - Best for: Development and troubleshooting
33//!
34//! ## Quick Start
35//!
36//! Add to your `Cargo.toml`:
37//!
38//! ```toml
39//! # For native applications (default)
40//! [dependencies]
41//! kiteconnect-async-wasm = "0.1.0"
42//!
43//! # For WASM/browser applications
44//! [dependencies]
45//! kiteconnect-async-wasm = { version = "0.1.0", default-features = false, features = ["wasm"] }
46//!
47//! # For development with debugging
48//! [dependencies]
49//! kiteconnect-async-wasm = { version = "0.1.0", features = ["native", "debug"] }
50//! ```
51//!
52//! ## Basic Usage
53//!
54//! ```rust,no_run
55//! use kiteconnect_async_wasm::connect::KiteConnect;
56//! use serde_json::Value as JsonValue;
57//!
58//! #[tokio::main]
59//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
60//! // Initialize KiteConnect client
61//! let mut kiteconnect = KiteConnect::new("<YOUR-API-KEY>", "");
62//!
63//! // Step 1: Get login URL
64//! let login_url = kiteconnect.login_url();
65//! println!("Login URL: {}", login_url);
66//!
67//! // Step 2: After user login, generate session with request token
68//! let session_response = kiteconnect
69//! .generate_session("<REQUEST-TOKEN>", "<API-SECRET>")
70//! .await?;
71//! println!("Session: {:?}", session_response);
72//!
73//! // Step 3: Use the API (access token is automatically set)
74//! let holdings: JsonValue = kiteconnect.holdings().await?;
75//! println!("Holdings: {:?}", holdings);
76//!
77//! Ok(())
78//! }
79//! ```
80//!
81//! ## Available APIs
82//!
83//! The library provides access to all KiteConnect REST APIs:
84//!
85//! ### Authentication
86//! - `login_url()` - Generate login URL
87//! - `generate_session()` - Create session with request token
88//! - `invalidate_session()` - Logout user
89//!
90//! ### Portfolio
91//! - `holdings()` - Get user holdings
92//! - `positions()` - Get user positions
93//! - `margins()` - Get account margins
94//!
95//! ### Orders
96//! - `orders()` - Get all orders
97//! - `order_trades()` - Get trades for specific order
98//! - `trades()` - Get all trades
99//!
100//! ### Market Data
101//! - `instruments()` - Get instrument list
102//! - `trigger_range()` - Get trigger range for instruments
103//!
104//! ### Mutual Funds
105//! - `mf_orders()` - Get mutual fund orders
106//! - `mf_instruments()` - Get mutual fund instruments
107//!
108//! ## Error Handling
109//!
110//! The library uses `anyhow::Result` for comprehensive error handling:
111//!
112//! ```rust,no_run
113//! # use kiteconnect_async_wasm::connect::KiteConnect;
114//! # #[tokio::main]
115//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
116//! # let kiteconnect = KiteConnect::new("", "");
117//! match kiteconnect.holdings().await {
118//! Ok(holdings) => println!("Holdings: {:?}", holdings),
119//! Err(e) => eprintln!("Error fetching holdings: {}", e),
120//! }
121//! # Ok(())
122//! # }
123//! ```
124//!
125//! ## Platform-Specific Features
126//!
127//! ### Native (Tokio)
128//! - Full CSV parsing for instruments
129//! - Complete async/await support
130//! - High-performance HTTP client
131//!
132//! ### WASM (Browser)
133//! - All APIs supported with full CSV parsing
134//! - CSV parsing using csv-core for browser compatibility
135//! - Returns structured JSON data (same as native)
136//! - Compatible with web frameworks
137//!
138//! ## Examples
139//!
140//! See the `examples/` directory for comprehensive usage examples:
141//! - `connect_sample.rs` - Basic API usage
142//! - `async_connect_example.rs` - Advanced async patterns
143//!
144//! ## Thread Safety
145//!
146//! The `KiteConnect` struct is `Clone + Send + Sync`, making it safe to use across
147//! multiple threads and async tasks. The underlying HTTP client uses connection
148//! pooling for optimal performance.
149//!
150//! ```rust,no_run
151//! # use kiteconnect_async_wasm::connect::KiteConnect;
152//! # #[tokio::main]
153//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
154//! let kiteconnect = KiteConnect::new("<API-KEY>", "<ACCESS-TOKEN>");
155//!
156//! // Clone for use in different tasks
157//! let kc1 = kiteconnect.clone();
158//! let kc2 = kiteconnect.clone();
159//!
160//! // Use in concurrent tasks
161//! let (holdings, positions) = tokio::try_join!(
162//! kc1.holdings(),
163//! kc2.positions()
164//! )?;
165//! # Ok(())
166//! # }
167//! ```
168//!
169#[cfg(test)]
170extern crate mockito;
171
172pub mod connect;
173pub mod models;