models-dev
A smart Rust client library for the models.dev API with intelligent caching capabilities.
๐ Overview
models-dev is a high-performance Rust client for the models.dev API that provides comprehensive information about AI model providers and their available models. The library features intelligent caching with ETag-based conditional requests, ensuring optimal performance for repeated API calls while always serving fresh data when available.
Key Features
- Smart Conditional Requests: Uses ETag-based HTTP conditional requests to minimize bandwidth usage
- Transparent Caching: Automatic cache management with the same simple API interface
- Performance Optimized: Significant speed improvements for repeated requests (up to 10x faster)
- Comprehensive Error Handling: Specific error types for better debugging and error recovery
- Clean Architecture: Well-structured codebase with clear separation of concerns
- Idiomatic Rust: Leverages Rust's type system and async/await for safe, concurrent operations
๐ฆ Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Feature Flags
The library supports different TLS backends:
# Default (native TLS)
= { = "0.1.0" }
# Rustls TLS
= { = "0.1.0", = ["rustls-tls"] }
# Native TLS
= { = "0.1.0", = ["native-tls"] }
Minimum Rust Version
This library requires Rust 1.70+.
๐ฏ Quick Start
Here's a simple example to get you started:
use ModelsDevClient;
async
๐ง Advanced Usage
Smart Caching
The library automatically handles caching with conditional HTTP requests. When you call fetch_providers(), it:
- Sends a HEAD request with ETag to check if data has changed
- If data hasn't changed, returns cached response immediately
- If data has changed, fetches fresh data and updates cache
use ModelsDevClient;
use Instant;
async
Cache Management
You can manually manage the cache:
use ModelsDevClient;
let client = new;
// Get cache information
let cache_info = client.cache_info;
println!;
// Clear cache (forces fresh API request)
client.clear_cache?;
// Fetch fresh data
let response = client.fetch_providers.await?;
Custom Configuration
Create a client with custom settings:
use ModelsDevClient;
use Duration;
// Custom API base URL
let client = with_base_url;
// The client uses a 30-second timeout by default
println!;
Error Handling
The library provides comprehensive error handling:
use ;
async
๐ API Reference
ModelsDevClient
The main client struct for interacting with the models.dev API.
Methods
new() -> Self
Creates a new client with default settings.
with_base_url(api_base_url: impl Into<String>) -> Self
Creates a client with a custom API base URL.
fetch_providers() -> Result<ModelsDevResponse, ModelsDevError>
Fetches provider information with smart caching.
clear_cache() -> Result<(), ModelsDevError>
Clears the cache metadata.
cache_info() -> CacheInfo
Returns information about the current cache state.
api_base_url() -> &str
Returns the API base URL.
timeout() -> Duration
Returns the request timeout.
Data Structures
ModelsDevResponse
Top-level response containing provider information.
Provider
Information about an AI model provider.
Model
Information about a specific AI model.
Error Types
ModelsDevError
Comprehensive error enum for all possible failures:
HttpError(reqwest::Error)- HTTP request failuresJsonError(serde_json::Error)- JSON parsing errorsApiError(String)- API error responsesTimeout- Network timeoutInvalidUrl(String)- Invalid URL configurationCacheError(String)- Cache operation failures
๐ Examples
The library includes comprehensive examples:
Basic Usage
Demonstrates basic client usage and caching benefits.
Smart Caching
Shows advanced caching functionality and performance comparisons.
Integration
Demonstrates integration patterns and error handling.
๐งช Testing
Run the test suite:
# Run all tests
# Run unit tests only
# Run integration tests
# Run with output
Test Coverage
The library includes:
- 12 unit tests covering core functionality
- 4 doc tests for API examples
- Integration tests for real API scenarios
- All tests are passing with comprehensive coverage
๐ค Contributing
We welcome contributions! Togather input, make a new issue to discuss the feature or fix you plan to working on. Please follow these guidelines:
Development Setup
- Fork the repository
- Clone your fork:
git clone https://github.com/your-username/models-dev - Create a feature branch:
git checkout -b feature-name - Install development dependencies:
cargo build - Run tests:
cargo test
Code Style Guidelines
- Follow the project's cognitive load reduction philosophy
- Use specific error types with
thiserror - Prefer
Result<T, ModelsDevError>over generic errors - Use
?operator for early returns, avoid nested conditionals - Leverage serde derive macros for API data structures
- Keep modules cohesive, use
pub(crate)for implementation details - Use descriptive variable names to reduce cognitive load
- Prefer async/await with tokio for network operations
Pull Request Process
- Ensure all tests pass:
cargo test - Format code:
cargo fmt - Run clippy:
cargo clippy -- -D warnings - Update documentation if needed
- Write clear commit messages following conventional commit format
- Submit a pull request with a clear description of changes
Commit Message Format
Follow the conventional commit format:
type(scope): description
# Examples
feat(client): add custom timeout configuration
fix(caching): resolve etag comparison issue
docs(readme): update installation instructions
test(examples): add integration test coverage
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Changelog
Version 0.1.0 (Current)
Initial Release
- โ Smart conditional HTTP requests using ETag-based caching
- โ Transparent caching with automatic cache management
- โ Performance optimization for repeated requests (up to 10x speedup)
- โ Comprehensive error handling with specific error types
- โ Clean, idiomatic Rust API design
- โ
Cache management utilities (
clear_cache(),cache_info()) - โ Comprehensive examples and documentation
- โ Full test coverage (12 unit tests + 4 doc tests)
- โ Support for multiple TLS backends
- โ Zero-cost abstractions leveraging Rust's type system
Key Milestones Achieved
- Architecture: Clean separation between client logic, data types, and error handling
- Performance: Smart caching reduces API calls and improves response times
- Reliability: Comprehensive error handling and testing
- Usability: Simple API interface with advanced features available when needed
- Documentation: Comprehensive README with examples and API reference
๐ฏ Design Philosophy
Cognitive load is what matters. This library is designed with the following principles:
- Simple Interface: The same
fetch_providers()method works for both fresh and cached data - Type Safety: Leverage Rust's type system to prevent entire classes of bugs
- Performance: Smart caching happens automatically, no manual coordination required
- Error Clarity: Specific error types guide developers to solutions
- Zero-Cost Abstractions: Caching and optimizations don't add runtime overhead when not needed
The library reduces cognitive load by:
- Providing a single method for all provider fetching needs
- Automatically handling cache invalidation and updates
- Using descriptive types that prevent common mistakes
- Offering clear error messages that guide debugging
Built with โค๏ธ for the Rust community
Rust's type system should reduce cognitive load, not increase it. If you're fighting the compiler, redesign.