Skip to main content

Crate unirate_actix

Crate unirate_actix 

Source
Expand description

§unirate-actix

Actix Web integration for the UniRate API — free, real-time currency exchange rates, conversion, and VAT rates.

This crate provides a configure function (and a scope factory) that register four GET handlers on top of the official unirate_api::Client:

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

The Client is pulled from application state (web::Data<Client>), so register one with App::app_data. UniRate errors are mapped to the matching HTTP status with a JSON error body via ApiError.

§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 Actix wiring. That keeps the client parity in one place and this crate small.

§Quick start

use actix_web::{web, App, HttpServer};
use unirate_actix::configure;
use unirate_api::Client;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let client = Client::new(std::env::var("UNIRATE_API_KEY").unwrap());
    let data = web::Data::new(client);

    HttpServer::new(move || {
        App::new()
            .app_data(data.clone())
            .configure(configure)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

§Mounting under a prefix

Use scope to nest the routes under a path prefix:

use actix_web::{web, App};
use unirate_actix::scope;
use unirate_api::Client;

let data = web::Data::new(Client::new("your-api-key"));
let app = App::new()
    .app_data(data)
    .service(scope("/currency"));
// now GET /currency/rate, /currency/convert, ...

Structs§

ApiError
Wraps a UniRateError and turns it into an HTTP response via Actix’s ResponseError.
ConvertParams
Query params for GET /convert.
ConvertResponse
Body of a successful GET /convert.
CurrenciesResponse
Body of a successful GET /currencies.
RateParams
Query params for GET /rate.
VatAll
Body of a successful GET /vat (all countries).
VatCountry
Body of a successful GET /vat?country=XX.
VatParams
Query params for GET /vat.
VatRateBody
VAT rate for a single country, as returned in a JSON response.

Functions§

configure
Register the UniRate routes on an Actix ServiceConfig.
scope
Build an Actix Scope holding the UniRate routes under prefix.