1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
mod types;
use thiserror::Error;
use rusty_money::{iso, ExchangeRate};
use std::collections::HashMap;
pub use types::CurrencyRates;
use types::*;
#[derive(Debug, Error)]
pub enum CurrencyLayerError {
#[error("Invalid currency symbol: {}", symbol)]
InvalidCurrency {
symbol: String,
},
#[error(
"Currency Layer responded with an error: Code: {}. Message: {}",
code, message
)]
ServerError {
code: u16,
message: String,
},
#[error(transparent)]
MoneyError(#[from] rusty_money::MoneyError),
#[error(transparent)]
SerdeError(#[from] serde_json::Error),
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error(transparent)]
IoError(#[from] std::io::Error),
}
pub struct Client {
key: String,
http_client: reqwest::Client,
}
impl Client {
pub fn new(key: &str) -> Client {
Client {
key: String::from(key),
http_client: reqwest::Client::new(),
}
}
pub async fn get_live_rates(&self, currencies: Vec<&str>) -> Result<CurrencyRates<'_>, CurrencyLayerError> {
self.get_rates(currencies, None, "http://apilayer.net/api/live")
.await
}
pub async fn get_historical_rates(
&self,
currencies: Vec<&str>,
date: (u16, u16, u16),
) -> Result<CurrencyRates<'_>, CurrencyLayerError> {
self.get_rates(currencies, Some(date), "http://apilayer.net/api/historical")
.await
}
async fn get_rates(
&self,
currencies: Vec<&str>,
date: Option<(u16, u16, u16)>,
url: &str,
) -> Result<CurrencyRates<'_>, CurrencyLayerError> {
let mut query_items = vec![
("currencies", currencies.join(",")),
("format", "1".into()),
("access_key", self.key.clone()),
];
if let Some(d) = date {
query_items.push(("date", format!("{}-{:02}-{:02}", d.0, d.1, d.2)));
}
let response = self.http_client.get(url).query(&query_items).send().await?;
let body_buf = response.text().await?;
let success_guard: SuccessGuard = serde_json::from_str(&body_buf)?;
if !success_guard.success {
let result: ErrorResponse = serde_json::from_str(&body_buf)?;
return Err(CurrencyLayerError::ServerError {
code: result.error.code,
message: result.error.info,
}
.into());
}
let result: CurrencyRatesResponse = serde_json::from_str(&body_buf)?;
fn lookup(symbol: &str) -> Result<&'static iso::Currency, CurrencyLayerError> {
iso::find(symbol).ok_or(
CurrencyLayerError::InvalidCurrency {
symbol: symbol.to_string().clone(),
}
.into(),
)
}
let quotes: HashMap<String, ExchangeRate<iso::Currency>> = result
.quotes
.into_iter()
.map(|(symbol_pair, rate)| {
let (from_symbol, to_symbol) = symbol_pair.split_at(3);
(
(
from_symbol.to_string().clone(),
to_symbol.to_string().clone(),
),
rate,
)
})
.filter(|((from_symbol, to_symbol), _rate)| from_symbol != to_symbol)
.map(|((from_symbol, to_symbol), rate)| {
let from = lookup(&from_symbol)?;
let to = lookup(&to_symbol)?;
Ok((to_symbol, ExchangeRate::new(from, to, rate)?))
})
.collect::<Result<_, CurrencyLayerError>>()?;
Ok(CurrencyRates {
timestamp: result.timestamp,
quotes,
})
}
}