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
use crate::api::Requests;
use crate::error::Error;
use crate::util::build_reqwest_client;
use reqwest::Url;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::Debug;
static PAPER_ENDPOINT: &str = "https://paper-trading.lemon.markets/v1";
static MONEY_ENDPOINT: &str = "https://trading.lemon.markets/v1";
#[derive(Debug)]
pub struct TradingClient {
pub api_key: String,
pub base_url: Url,
pub(crate) client: reqwest::blocking::Client,
}
impl Requests for TradingClient {
fn get<T: DeserializeOwned + Debug>(&self, path: &str) -> Result<T, Error> {
let url = format!("{}/{}", self.base_url, path);
let r = self.client.get(&url).send()?;
let json = self.response_handler(r)?;
Ok(json)
}
fn get_with_query<T: DeserializeOwned + Debug, Q: IntoIterator + Serialize>(
&self,
path: &str,
query: Q,
) -> Result<T, Error> {
let url = format!("{}/{}", self.base_url, path);
let r = self.client.get(&url).query(&query).send()?;
let json = self.response_handler(r)?;
Ok(json)
}
fn post<T: DeserializeOwned, B: Serialize>(&self, path: &str, body: B) -> Result<T, Error> {
let url = format!("{}/{}", self.base_url, path);
let body_string = serde_json::to_string(&body)?;
let r = self.client.post(&url).body(body_string).send()?;
let json = self.response_handler(r)?;
Ok(json)
}
fn delete<T: DeserializeOwned>(&self, path: &str, path_param: &str) -> Result<T, Error> {
let url = format!("{}/{}/{}", self.base_url, path, path_param);
let r = self.client.delete(&url).send()?;
let json = self.response_handler::<T>(r)?;
Ok(json)
}
}
impl TradingClient {
pub fn new(api_key: String, _endpoint: &str) -> Self {
let base_url = Url::parse(PAPER_ENDPOINT).unwrap();
let client = build_reqwest_client(&api_key);
Self {
api_key,
base_url,
client,
}
}
pub fn paper_client(api_key: &str) -> Self {
TradingClient::new(api_key.to_string(), PAPER_ENDPOINT)
}
pub fn live_client(api_key: String) -> Self {
TradingClient::new(api_key, MONEY_ENDPOINT)
}
}