Coder Social home page Coder Social logo

currency-layer-rs's Introduction

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
    );
}

currency-layer-rs's People

Contributors

ryanbluth avatar agentydragon avatar

Stargazers

Heni avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

agentydragon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.