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
use crate::{Client, JsonValue, Result};
use serde::de::DeserializeOwned;
pub enum Interval {
Min1 = 1,
Min5 = 5,
Min15 = 15,
Min30 = 30,
Hour1 = 60,
Hour4 = 240,
Day1 = 1_440,
Day7 = 10_080,
Day15 = 21_600,
}
#[must_use = "Does nothing until you send or execute it"]
pub struct GetOhlcDataRequest {
client: Client,
pair: String,
interval: Option<Interval>,
}
impl GetOhlcDataRequest {
pub fn interval(self, interval: Interval) -> Self {
Self {
interval: Some(interval),
..self
}
}
pub async fn execute<T: DeserializeOwned>(self) -> Result<T> {
let mut url = format!("/0/public/OHLC?pair={}", self.pair);
if let Some(interval) = self.interval {
url.push_str(&format!("&interval={}", interval as u32))
}
self.client.send_public(&url).await
}
pub async fn send(self) -> Result<GetOhlcDataResponse> {
self.execute().await
}
}
pub type GetOhlcDataResponse = JsonValue;
impl Client {
pub fn get_ohlc_data(&self, pair: impl Into<String>) -> GetOhlcDataRequest {
GetOhlcDataRequest {
client: self.clone(),
pair: pair.into(),
interval: None,
}
}
}
#[cfg(test)]
mod tests {
use crate::{Client, Interval, PairName};
#[test]
fn get_ohlc_data() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let client = Client::default();
let pair = PairName::from("XBT", "USD");
let resp = client
.get_ohlc_data(&pair)
.interval(Interval::Day1)
.send()
.await;
match resp {
Ok(resp) => println!("{:?}", resp),
Err(error) => eprintln!("{:?}", error),
}
});
}
}