🔧 Retrofit-rs
A type-safe, declarative HTTP client library for Rust, inspired by Java Retrofit.
✨ Features
- 🎯 Declarative API - Define your API with simple macros
- 🔒 Type-Safe - Compile-time type checking
- ⚡ Async/Await - Built on tokio for high performance
- 🔄 Interceptors - Powerful middleware system
- 📝 Auto Serialization - Automatic JSON handling with serde
- 🛡️ Memory Safe - Leveraging Rust's ownership system
🚀 Quick Start
Add Retrofit-rs to your Cargo.toml
:
[]
= "0.1"
= { = "1", = ["full"] }
= { = "1", = ["derive"] }
Define your API:
use ;
use Deserialize;
async
💡 Examples
Basic Usage
use ;
let client = builder
.base_url
.build?;
let request = client.get
.path_param;
let user: User = client.execute.await?;
Timeout Configuration
use Duration;
let client = builder
.base_url
.timeout // Overall timeout (default: 30s)
.connect_timeout // Connection timeout (default: 10s)
.build?;
With Interceptors
use ;
let client = builder
.base_url
.add_interceptor
.add_interceptor
.build?;
Declarative API with CRUD Operations
use ;
// Use the API
let client = builder
.base_url
.build?;
let api = with_client;
// Create
let book = api.create_book.await?;
// Read
let book = api.get_book.await?;
// Update
let updated = api.update_book.await?;
// Delete
api.delete_book.await?;
🎯 Run Examples
# Complete CRUD example with Book server and Retrofit-rs client
# GitHub API with type wrappers
🔧 Built-in Interceptors
Logging Interceptor
use LoggingInterceptor;
// Basic logging (method and URL)
.add_interceptor
// With headers
.add_interceptor
// Full logging (including body)
.add_interceptor
Auth Interceptor
use AuthInterceptor;
// Bearer token
.add_interceptor
// Basic auth
.add_interceptor
// API Key
.add_interceptor
Retry Interceptor
use RetryInterceptor;
use Duration;
.add_interceptor
🏗️ Architecture
retrofit-rs/
├── retrofit-rs # Core library
│ ├── Client # HTTP client
│ ├── Interceptors # Middleware system
│ └── Builder # Fluent API
│
└── retrofit-rs-macros # Procedural macros
├── #[api] # API definition
└── #[get/post] # HTTP methods
📈 Comparison
Feature | Java Retrofit | Retrofit-rs |
---|---|---|
API Style | Annotations | Macros ✨ |
Type Safety | Runtime | Compile-time ✅ |
Async | Callback/RxJava | async/await ✅ |
Performance | JVM overhead | Zero-cost ✅ |
Memory Safety | GC | Ownership ✅ |
🎓 Why "Retrofit-rs"?
Retrofit-rs brings the beloved Retrofit API design pattern to Rust, combining Java's proven declarative HTTP client approach with Rust's compile-time safety and zero-cost abstractions.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
Inspired by Square's Retrofit for Java.
Made with ❤️ and 🦀 by the Rust community