currency_layer

Crates.iocurrency_layer
lib.rscurrency_layer
version0.2.0
sourcesrc
created_at2018-07-29 18:23:04.609146
updated_at2022-10-27 12:31:42.550224
descriptionClient for free currency layer APIs
homepage
repositoryhttps://github.com/RyanBluth/currency-layer-rs
max_upload_size
id76499
size44,695
Ryan Bluth (RyanBluth)

documentation

README

currency-layer-rs is a client for CurrencyLayer's free APIs

Example Usage

Live Rates

See examples/live_rates.rs.

extern crate currency_layer;
extern crate tokio;

use currency_layer::Client;
use rusty_money::{iso, Money};

#[tokio::main]
async fn main() {
    let client = Client::new("API_KEY");
    let result = client
        .get_live_rates(
            // Currencies to get rates for
            vec!["GBP", "USD", "CAD"],
        )
        .await
        .unwrap();

    // The free API only returns rates in dollars.
    // See: https://currencylayer.com/documentation, section
    // "Source Currency Switching".
    let original_usd = Money::from_major(100, iso::USD);
    let exchange_rate_usd_to_gbp = result.quotes.get("GBP").unwrap();
    let converted_to_gbp = exchange_rate_usd_to_gbp
        .convert(original_usd.clone())
        .unwrap();
    // At 2021-02-16 02:12:06 UTC: $100 → £71.74
    println!(
        "At {}: {} → {}",
        result.timestamp, &original_usd, &converted_to_gbp
    );
}

Historical Rates

See examples/historical_rates.rs.

extern crate currency_layer;
extern crate tokio;

use currency_layer::Client;
use rusty_money::{iso, Money};

#[tokio::main]
async fn main() {
    let client = Client::new("API_KEY");
    let result = client
        .get_historical_rates(
            // Currencies to get rates for
            vec!["GBP", "USD", "CAD"],
            // Date as a tuple 3 in the format YYYY, MM, DD
            (2015, 2, 23),
        )
        .await
        .unwrap();

    // The free API only returns rates in dollars.
    // See: https://currencylayer.com/documentation, section
    // "Source Currency Switching".
    let original_usd = Money::from_major(100, iso::USD);
    let exchange_rate_usd_to_gbp = result.quotes.get("GBP").unwrap();
    let converted_to_gbp = exchange_rate_usd_to_gbp
        .convert(original_usd.clone())
        .unwrap();
    // At 2015-02-23 23:59:59 UTC: $100 → £64.70
    println!(
        "At {}: {} → {}",
        result.timestamp, &original_usd, &converted_to_gbp
    );
}
Commit count: 15

cargo fmt