Expand description
§unirate-rocket
Rocket integration for the UniRate API — free, real-time currency exchange rates, conversion, and VAT rates.
This crate exposes a ready-made set of routes via unirate_routes plus a
UniRateFairing that manages a single unirate_api::Client in Rocket’s
state. Together they add four GET routes:
| Route | Query params | Backing client call |
|---|---|---|
/rate | from, to | Client::get_rate / Client::get_all_rates |
/convert | from, to, amount | Client::convert |
/currencies | — | Client::get_supported_currencies |
/vat | country (optional) | Client::get_vat_rate / Client::get_vat_rates |
UniRate errors are mapped to the matching HTTP status with a JSON error body
via ApiError, which implements Rocket’s
Responder.
§Client sourcing
The UniRate HTTP client itself is not re-implemented here — this crate
depends on the published unirate-api
crate (unirate_api::Client) and only adds the Rocket wiring. That keeps the
client parity in one place and this crate small.
§Quick start
use rocket::launch;
use unirate_rocket::{unirate_routes, UniRateFairing};
#[launch]
fn rocket() -> _ {
let key = std::env::var("UNIRATE_API_KEY").expect("UNIRATE_API_KEY");
rocket::build()
.attach(UniRateFairing::new(key))
.mount("/", unirate_routes())
}The Client is managed by the UniRateFairing, so the handlers read it
from rocket::State<Client>. If you’d rather manage the client yourself,
skip the fairing and call rocket::Rocket::manage with a Client:
use rocket::routes;
use unirate_api::Client;
use unirate_rocket::unirate_routes;
let client = Client::new("your-api-key");
let rocket = rocket::build()
.manage(client)
.mount("/currency", unirate_routes());
// now GET /currency/rate, /currency/convert, ...Structs§
- ApiError
- Wraps a
UniRateErrorand turns it into an HTTP response via Rocket’sResponder. - Convert
Response - Body of a successful
GET /convert. - Currencies
Response - Body of a successful
GET /currencies. - UniRate
Fairing - A Rocket
Fairingthat manages a singleunirate_api::Clientin state. - VatAll
- Body of a successful
GET /vat(all countries). - VatCountry
- Body of a successful
GET /vat?country=XX. - VatRate
Body - VAT rate for a single country, as returned in a JSON response.
Functions§
- unirate_
routes - Return the UniRate routes for mounting on a Rocket instance.