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
use reqwest::Url;
use {Client, API_URL, ApiResult};
use error::*;
use types::*;
impl Client {
pub fn get_markets(&self) -> Result<Box<[MarketInfo]>> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getmarkets").unwrap();
}
let mut response = self.inner.get(URL.clone())?.send()?;
let result: ApiResult<Box<[MarketInfo]>> = response.json()?;
result.into_result()
}
pub fn get_currencies(&self) -> Result<Box<[CurrencyInfo]>> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getcurrencies").unwrap();
}
let mut response = self.inner.get(URL.clone())?.send()?;
let result: ApiResult<Box<[CurrencyInfo]>> = response.json()?;
result.into_result()
}
pub fn get_ticker<S: AsRef<str>>(&self, market: S) -> Result<TickerInfo> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getticker").unwrap();
}
let mut url = URL.clone();
let _ = url.query_pairs_mut().append_pair("market", market.as_ref());
let mut response = self.inner.get(url)?.send()?;
let result: ApiResult<TickerInfo> = response.json()?;
result.into_result()
}
pub fn get_market_summaries(&self) -> Result<Box<[MarketSummary]>> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getmarketsummaries").unwrap();
}
let mut response = self.inner.get(URL.clone())?.send()?;
let result: ApiResult<Box<[MarketSummary]>> = response.json()?;
result.into_result()
}
pub fn get_market_summary<S: AsRef<str>>(&self, market: S) -> Result<MarketSummary> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getmarketsummary").unwrap();
}
let mut url = URL.clone();
let _ = url.query_pairs_mut().append_pair("market", market.as_ref());
let mut response = self.inner.get(url.clone())?.send()?;
let result: ApiResult<Vec<MarketSummary>> = response.json()?;
result.into_result().and_then(|arr| {
arr.into_iter().next().ok_or_else(|| {
ErrorKind::Api("api response did not contain the market summary".to_owned()).into()
})
})
}
pub fn get_order_book<S: AsRef<str>>(
&self,
market: S,
order_type: OrderBookType,
depth: u8,
) -> Result<OrderBook> {
assert!(depth <= 50, "order book depth must be between 0 and 50");
lazy_static! {
static ref URL: Url = API_URL.join("public/getorderbook").unwrap();
}
let mut url = URL.clone();
let _ = url.query_pairs_mut()
.append_pair("market", market.as_ref())
.append_pair("type", order_type.as_str())
.append_pair("depth", &format!("{}", depth));
let mut response = self.inner.get(url.clone())?.send()?;
match order_type {
OrderBookType::Buy => {
let result: ApiResult<Box<[Order]>> = response.json()?;
Ok(OrderBook::new(result.into_result()?, Vec::new()))
}
OrderBookType::Sell => {
let result: ApiResult<Box<[Order]>> = response.json()?;
Ok(OrderBook::new(Vec::new(), result.into_result()?))
}
OrderBookType::Both => {
let result: ApiResult<OrderBook> = response.json()?;
result.into_result()
}
}
}
pub fn get_market_history<S: AsRef<str>>(&self, market: S) -> Result<MarketSummary> {
lazy_static! {
static ref URL: Url = API_URL.join("public/getmarketsummary").unwrap();
}
let mut url = URL.clone();
let _ = url.query_pairs_mut().append_pair("market", market.as_ref());
let mut response = self.inner.get(url.clone())?.send()?;
let result: ApiResult<Vec<MarketSummary>> = response.json()?;
result.into_result().and_then(|arr| {
arr.into_iter().next().ok_or_else(|| {
ErrorKind::Api("api response did not contain the market summary".to_owned()).into()
})
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OrderBookType {
Buy,
Sell,
Both,
}
impl OrderBookType {
fn as_str(&self) -> &str {
match *self {
OrderBookType::Buy => "buy",
OrderBookType::Sell => "sell",
OrderBookType::Both => "both",
}
}
}