yf-common
Shared infrastructure library for Yahoo Finance data extraction tools.
Overview
yf-common provides battle-tested components for building reliable Yahoo Finance data extraction tools:
- HTTP Client - Builder pattern with configurable timeouts and connection pooling
- Rate Limiting - Token bucket algorithm to respect API limits (default: 5 req/min)
- Retry Logic - Exponential backoff with jitter for transient failures
- Authentication - Cookie/crumb mechanism for Yahoo Finance API
- Timestamp Utilities - Parse dates, convert to Unix timestamps
- Output Writers - JSON and CSV formatters
Installation
Add to your Cargo.toml:
[]
= "0.2"
# With authentication support (required for options data)
= { = "0.2", = ["auth"] }
# With CSV output support
= { = "0.2", = ["csv"] }
# All features
= { = "0.2", = ["auth", "csv"] }
Features
| Feature | Description | Default |
|---|---|---|
auth |
Cookie/crumb authentication for Yahoo Finance | No |
csv |
CSV output writer | No |
Quick Start
Basic HTTP Client
use ;
use Duration;
async
With Authentication (for Options Data)
use ;
async
Timestamp Utilities
use ;
// Parse date string to Unix timestamp
let ts = parse_date_to_timestamp?;
assert_eq!; // End of day UTC
// Convert timestamp back to date string
let date = timestamp_to_date;
assert_eq!;
// Get today's date
let today = today_str; // e.g., "2024-03-15"
Output Writers
use JsonWriter;
use Serialize;
let data = StockData ;
// Pretty JSON to stdout
stdout.pretty.write?;
// Compact JSON to file
file?.write?;
Module Reference
error - Error Types
use ;
// Error variants:
// - HttpError(reqwest::Error)
// - RateLimitExceeded
// - AuthenticationFailed(String)
// - InvalidResponse(String)
// - ParseError(String)
// - IoError(std::io::Error)
rate_limit - Rate Limiting
use ;
// Configure rate limiter
let config = new; // 10 requests per minute
let limiter = new;
// Check before making request
limiter.acquire.await; // Blocks if rate exceeded
retry - Retry Logic
use ;
let config = RetryConfig ;
// Retry a fallible async operation
let result = retry_with_backoff.await?;
client - HTTP Client
use ;
let client = new
.with_rate_limit // Requests per minute
.with_timeout
.with_retry
.with_user_agent
.build?;
// GET request
let body = client.get.await?;
// GET with query params
let body = client.get_with_params.await?;
auth - Authentication (feature: auth)
use ;
// For endpoints requiring authentication
let auth = new;
auth.authenticate.await?;
let crumb = auth.get_crumb?;
// For public endpoints
let no_auth = NoAuth;
time - Timestamp Utilities
use ;
output - Output Writers
use ; // CsvWriter requires "csv" feature
// JSON output
stdout.pretty.write?;
file?.write?;
// CSV output (with csv feature)
stdout.write_records?;
file?.with_headers.write_records?;
Architecture
yf-common/
├── Cargo.toml
├── README.md
└── src/
├── lib.rs # Public API exports
├── error.rs # YfCommonError enum
├── client.rs # YahooClient with builder pattern
├── auth.rs # CrumbAuth (feature-gated)
├── retry.rs # Exponential backoff
├── rate_limit.rs # Governor-based rate limiting
├── time.rs # Timestamp utilities
└── output.rs # JSON/CSV writers
Used By
- yf-quotes - Historical OHLCV stock data
- yf-options - Options chain with Greeks
MSRV
Minimum Supported Rust Version: 1.75
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions welcome! Please read the contributing guidelines first.