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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use hyper::client::{Client, HttpConnector};
use hyper;
use tokio_core::reactor::{Core, Handle, Timeout};
use futures::future::{Future, result};
use futures::Stream;
use serde_json;
use serde::de::{Deserialize, Deserializer};
use std::cell::RefCell;
use std::str;
use std::time::Duration;
use exchange::{Currency, Exchange, Rates};
use future::FixerioFuture;
use errors::{Error, ErrorKind};
/// Configuration for requests.
pub struct Config {
base: Currency,
symbols: Option<Vec<Currency>>,
date: Option<String>,
}
impl Config {
/// Create a new Config struct.
pub fn new(curr: Currency) -> Self {
Self {
base: curr,
symbols: None,
date: None,
}
}
/// Set the symbols (List of specific exchange rates for the base currency, will retrieve all if none as specified).
pub fn set_symbols(&mut self, symbols: Vec<Currency>) {
self.symbols = Some(symbols);
}
/// Set the date (The date for which to retrieve exchange rates from, will retrieve latest if a date is not specified).
pub fn set_date<T: Into<String>>(&mut self, date: T) {
self.date = Some(date.into());
}
}
fn get_url(conf: &Config) -> hyper::Uri {
let mut url = String::from("http://api.fixer.io/");
url.push_str(conf.date.as_ref().map(String::as_str).unwrap_or("latest"));
url.push_str("?base=");
url.push_str(conf.base.string());
if let Some(ref currencies) = conf.symbols {
let string_vec: Vec<&str> = currencies.iter().map(|x| x.string()).collect();
let sep_string = string_vec.join(",");
url.push_str("&symbols=");
url.push_str(&sep_string);
}
url.parse::<hyper::Uri>().unwrap()
}
/// Asynchronous API for sending requests
pub struct AsyncApi {
client: Client<HttpConnector>,
handle: Handle,
}
impl AsyncApi {
/// Create a new instance.
pub fn new(handle: &Handle) -> Self {
Self {
client: Client::new(handle),
handle: handle.clone(),
}
}
/// Perform request with timeout.
///
/// # Examples
///
/// ```rust,no_run
///
/// # extern crate fixerio;
/// # extern crate tokio_core;
/// use fixerio::{Config, Currency, AsyncApi};
/// use tokio_core::reactor::Core;
/// use std::time::Duration;
///
/// fn main() {
/// let mut core = Core::new().expect("Error creating core");
/// let handle = core.handle();
///
/// let api = AsyncApi::new(&handle);
///
/// let config = Config::new(Currency::USD);
///
/// let work = api.get_timeout(&config, Duration::from_secs(5));
///
/// let rates = core.run(work).expect("Error retrieving rates");
///
/// match rates {
/// Some(x) => println!("{:?}", x),
/// _ => println!("Request timed out")
/// };
/// }
/// ```
pub fn get_timeout(&self,
conf: &Config,
duration: Duration)
-> FixerioFuture<Option<Exchange>> {
let timeout_future = result(Timeout::new(duration, &self.handle))
.flatten()
.map_err(From::from)
.map(|_| None);
let get = self.get(conf).map(Some);
let future = timeout_future
.select(get)
.map(|(item, _next)| item)
.map_err(|(item, _next)| item);
FixerioFuture::new(Box::new(future))
}
/// Perform request.
///
/// # Examples
///
/// ```rust,no_run
///
/// # extern crate fixerio;
/// # extern crate tokio_core;
/// use fixerio::{Config, Currency, AsyncApi};
/// use tokio_core::reactor::Core;
/// use std::time::Duration;
///
/// fn main() {
/// let mut core = Core::new().expect("Error creating core");
/// let handle = core.handle();
///
/// // Get the latest exchange rates for USD
/// let api = AsyncApi::new(&handle);
///
/// let config = Config::new(Currency::USD);
/// let work = api.get(&config);
///
/// println!("{:?}", core.run(work).expect("Error retrieving rates"));
/// }
/// ```
pub fn get(&self, conf: &Config) -> FixerioFuture<Exchange> {
let url = get_url(conf);
let response = self.client
.get(url)
.and_then(|res| res.body().concat2())
.map_err(From::from);
let future = response.and_then(|bytes| {
result(serde_json::from_slice(&bytes)
.map_err(From::from)
.and_then(|value| match value {
Response::Success(exchange) => Ok(exchange),
Response::Error(err) => {
Err(ErrorKind::FixerioError { description: err }.into())
}
}))
});
FixerioFuture::new(Box::new(future))
}
}
/// Synchronous API for sending requests.
pub struct SyncApi {
core: RefCell<Core>,
api: AsyncApi,
}
impl SyncApi {
/// Create a new instance.
pub fn new() -> Result<Self, Error> {
let core = Core::new()?;
let handle = core.handle();
Ok(Self {
core: RefCell::new(core),
api: AsyncApi::new(&handle),
})
}
/// Perform request.
///
/// # Examples
///
/// ```rust,no_run
///
/// # extern crate fixerio;
/// use fixerio::{Config, Currency, SyncApi};
/// use std::time::Duration;
///
/// fn main() {
/// let api = SyncApi::new().expect("Error creating API");
///
/// let config = Config::new(Currency::USD);
///
/// let rates = api.get(&config).expect("Error retrieving rates");
///
/// println!("{:?}", rates);
/// }
/// ```
pub fn get(&self, conf: &Config) -> Result<Exchange, Error> {
let future = self.api.get(conf);
self.core.borrow_mut().run(future)
}
/// Perform request with timeout.
///
/// # Examples
///
/// ```rust,no_run
///
/// # extern crate fixerio;
/// use fixerio::{Config, Currency, SyncApi};
/// use std::time::Duration;
///
/// fn main() {
/// let api = SyncApi::new().expect("Error creating API");
///
/// let config = Config::new(Currency::USD);
///
/// let rates = api.get_timeout(&config, Duration::from_secs(5)).expect("Error retrieving rates");
///
/// match rates {
/// Some(x) => println!("{:?}", x),
/// _ => println!("Request timed out")
/// };
/// }
/// ```
pub fn get_timeout(&self,
conf: &Config,
duration: Duration)
-> Result<Option<Exchange>, Error> {
let future = self.api.get_timeout(conf, duration);
self.core.borrow_mut().run(future)
}
}
#[derive(Deserialize)]
struct RawResponse {
error: Option<String>,
base: Option<String>,
date: Option<String>,
rates: Option<Rates>,
}
enum Response {
Error(String),
Success(Exchange),
}
impl<'de> Deserialize<'de> for Response {
fn deserialize<D>(deserializer: D) -> Result<Response, D::Error>
where D: Deserializer<'de>
{
let raw: RawResponse = Deserialize::deserialize(deserializer)?;
if let Some(err) = raw.error {
Ok(Response::Error(err))
} else {
Ok(Response::Success(Exchange {
base: raw.base.unwrap(),
date: raw.date.unwrap(),
rates: raw.rates.unwrap(),
}))
}
}
}
#[cfg(test)]
mod tests {
use client::{Config, get_url};
use exchange::Currency;
#[test]
fn get_url_test() {
let tests = vec![("http://api.fixer.io/latest?base=AUD",
Config {
base: Currency::AUD,
symbols: None,
date: None,
}),
("http://api.fixer.io/latest?base=EUR",
Config {
base: Currency::EUR,
symbols: None,
date: None,
}),
("http://api.fixer.io/2000-01-03?base=EUR",
Config {
base: Currency::EUR,
symbols: None,
date: Some(String::from("2000-01-03")),
}),
("http://api.fixer.io/latest?base=EUR&symbols=AUD",
Config {
base: Currency::EUR,
symbols: Some(vec![Currency::AUD]),
date: None,
}),
("http://api.fixer.io/latest?base=EUR&symbols=AUD,USD",
Config {
base: Currency::EUR,
symbols: Some(vec![Currency::AUD, Currency::USD]),
date: None,
})];
for test in tests {
assert_eq!(test.0, get_url(&test.1));
}
}
}