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:
| 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 |
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
UniRateErrorand turns it into an HTTP response via Actix’sResponseError. - Convert
Params - Query params for
GET /convert. - Convert
Response - Body of a successful
GET /convert. - Currencies
Response - Body of a successful
GET /currencies. - Rate
Params - 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. - VatRate
Body - 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
Scopeholding the UniRate routes underprefix.