Skip to main content

Crate unirate_rocket

Crate unirate_rocket 

Source
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:

RouteQuery paramsBacking client call
/ratefrom, toClient::get_rate / Client::get_all_rates
/convertfrom, to, amountClient::convert
/currencies—Client::get_supported_currencies
/vatcountry (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 UniRateError and turns it into an HTTP response via Rocket’s Responder.
ConvertResponse
Body of a successful GET /convert.
CurrenciesResponse
Body of a successful GET /currencies.
UniRateFairing
A Rocket Fairing that manages a single unirate_api::Client in state.
VatAll
Body of a successful GET /vat (all countries).
VatCountry
Body of a successful GET /vat?country=XX.
VatRateBody
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.