Skip to main content

fly402_client/
lib.rs

1//! # 402fly Client
2//!
3//! HTTP client library for the 402fly payment protocol with automatic payment handling.
4//!
5//! This library provides two client implementations:
6//! - **X402Client**: Explicit control over payment flow
7//! - **X402AutoClient**: Automatic payment handling
8//!
9//! ## Features
10//!
11//! - Automatic detection of 402 Payment Required responses
12//! - Seamless payment creation and transaction broadcasting
13//! - Configurable payment limits and retry behavior
14//! - Support for GET and POST requests
15//!
16//! ## Example: Explicit Client
17//!
18//! ```rust,no_run
19//! use fly402_client::X402Client;
20//! use solana_sdk::signature::Keypair;
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//!     let keypair = Keypair::new(); // Load your keypair
25//!     let client = X402Client::new(keypair, None);
26//!
27//!     // Make request
28//!     let response = client.get("https://api.example.com/premium-data").await?;
29//!
30//!     // Check if payment is required
31//!     if client.is_payment_required(&response) {
32//!         // Parse payment request
33//!         let payment_request = client.parse_payment_request(response).await?;
34//!
35//!         // Create payment
36//!         let authorization = client.create_payment(&payment_request).await?;
37//!
38//!         // Retry with payment
39//!         let response = client.get_with_auth(
40//!             "https://api.example.com/premium-data",
41//!             &authorization
42//!         ).await?;
43//!
44//!         println!("Got data: {:?}", response.text().await?);
45//!     }
46//!
47//!     Ok(())
48//! }
49//! ```
50//!
51//! ## Example: Auto Client
52//!
53//! ```rust,no_run
54//! use fly402_client::{X402AutoClient, AutoClientOptions};
55//! use solana_sdk::signature::Keypair;
56//!
57//! #[tokio::main]
58//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
59//!     let keypair = Keypair::new(); // Load your keypair
60//!
61//!     // Configure options
62//!     let options = AutoClientOptions {
63//!         max_payment_amount: "5.0".to_string(),
64//!         auto_retry: true,
65//!         max_retries: 3,
66//!     };
67//!
68//!     let client = X402AutoClient::new(keypair, None, Some(options));
69//!
70//!     // Make request - payment is handled automatically!
71//!     let response = client.get("https://api.example.com/premium-data").await?;
72//!     println!("Got data: {:?}", response.text().await?);
73//!
74//!     Ok(())
75//! }
76//! ```
77
78pub mod auto_client;
79pub mod client;
80
81// Re-export commonly used types
82pub use auto_client::{AutoClientOptions, X402AutoClient};
83pub use client::X402Client;
84
85// Re-export core types for convenience
86pub use fly402_core::{
87    PaymentAuthorization, PaymentRequest, SolanaPaymentProcessor, X402Error, X402Result,
88};
89
90/// Library version
91pub const VERSION: &str = env!("CARGO_PKG_VERSION");
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_version() {
99        assert!(!VERSION.is_empty());
100    }
101}