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
use super::Price;
use crate::{
config::HttpsConfig,
https_client::{HttpsClient, Query},
Currency, TradingPair,
};
use crate::{
error::{Error, ErrorKind},
prelude::*,
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
pub const API_HOST: &str = "quotation-api-cdn.dunamu.com";
pub struct DunamuSource {
https_client: HttpsClient,
}
impl DunamuSource {
#[allow(clippy::new_without_default)]
pub fn new(config: &HttpsConfig) -> Result<Self, Error> {
let https_client = HttpsClient::new(API_HOST, config)?;
Ok(Self { https_client })
}
pub async fn trading_pairs(&self, pair: &TradingPair) -> Result<Price, Error> {
if pair.0 != Currency::Krw && pair.1 != Currency::Krw {
fail!(ErrorKind::Currency, "trading pair must be with KRW");
}
let mut query = Query::new();
query.add("codes", format!("FRX.{}{}", pair.0, pair.1));
let api_response: Response = self
.https_client
.get_json("/v1/forex/recent", &query)
.await?;
let price: Decimal = api_response[0].base_price.to_string().parse()?;
Ok(Price::new(price)?)
}
}
pub type Response = Vec<ResponseElement>;
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponseElement {
code: String,
currency_code: String,
currency_name: String,
country: String,
name: String,
date: String,
time: String,
recurrence_count: i64,
base_price: f64,
opening_price: f64,
high_price: f64,
low_price: f64,
change: String,
change_price: f64,
cash_buying_price: f64,
cash_selling_price: f64,
tt_buying_price: f64,
tt_selling_price: f64,
tc_buying_price: Option<serde_json::Value>,
fc_selling_price: Option<serde_json::Value>,
exchange_commission: f64,
us_dollar_rate: f64,
#[serde(rename = "high52wPrice")]
high52_w_price: f64,
#[serde(rename = "high52wDate")]
high52_w_date: String,
#[serde(rename = "low52wPrice")]
low52_w_price: f64,
#[serde(rename = "low52wDate")]
low52_w_date: String,
currency_unit: i64,
provider: String,
timestamp: i64,
id: i64,
created_at: String,
modified_at: String,
change_rate: f64,
signed_change_price: f64,
signed_change_rate: f64,
}
#[cfg(test)]
mod tests {
use super::DunamuSource;
use std::future::Future;
fn block_on<F: Future>(future: F) -> F::Output {
tokio::runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap()
.block_on(future)
}
#[test]
#[ignore]
fn trading_pairs_ok() {
let pair = "KRW/USD".parse().unwrap();
let _response = block_on(
DunamuSource::new(&Default::default())
.unwrap()
.trading_pairs(&pair),
)
.unwrap();
}
#[test]
#[ignore]
fn trading_pairs_404() {
let pair = "N/A".parse().unwrap();
let _err = block_on(
DunamuSource::new(&Default::default())
.unwrap()
.trading_pairs(&pair),
)
.err()
.unwrap();
}
}