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
#![cfg_attr(feature = "cargo-clippy", deny(clippy))]
#![forbid(anonymous_parameters)]
#![cfg_attr(feature = "cargo-clippy", warn(clippy_pedantic))]
#![deny(variant_size_differences, unused_results, unused_qualifications, unused_import_braces,
unsafe_code, trivial_numeric_casts, trivial_casts, missing_docs,
missing_debug_implementations, missing_copy_implementations, unused_extern_crates)]
#![cfg_attr(feature = "cargo-clippy", allow(similar_names))]
extern crate chrono;
#[macro_use]
extern crate failure;
extern crate hex;
extern crate hmac;
#[macro_use]
extern crate lazy_static;
extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate sha2;
mod private;
mod public;
pub mod types;
use chrono::Utc;
use failure::Error;
use reqwest::Url;
use reqwest::header::Headers;
use serde::Deserialize;
pub use public::OrderBookType;
lazy_static! {
static ref API_URL: Url = Url::parse("https://bittrex.com/api/v1.1/").unwrap();
}
#[derive(Debug)]
pub struct Client {
inner: reqwest::Client,
api_key: Option<String>,
api_secret: Option<String>,
}
impl Default for Client {
fn default() -> Self {
Self {
inner: reqwest::Client::new(),
api_key: None,
api_secret: None,
}
}
}
impl Client {
pub fn login<K: Into<String>, S: Into<String>>(&mut self, api_key: K, api_secret: S) {
self.api_key = Some(api_key.into());
self.api_secret = Some(api_secret.into());
}
fn append_login(&self, url: &mut Url) {
let api_key = self.api_key.as_ref().unwrap();
let nonce = Utc::now().timestamp();
let _ = url.query_pairs_mut()
.append_pair("apikey", api_key)
.append_pair("nonce", &format!("{}", nonce));
}
fn get_headers(&self, url: &Url) -> Result<Headers, Error> {
let mut headers = Headers::new();
let hash = self.hash_uri(url);
headers.set_raw("apisign", hash?);
Ok(headers)
}
fn hash_uri(&self, url: &Url) -> Result<String, Error> {
use hex::ToHex;
use hmac::{Hmac, Mac};
use sha2::Sha512;
let api_secret = self.api_secret
.as_ref()
.expect("the client was not logged in");
let mut hmac = Hmac::<Sha512>::new_varkey(api_secret.as_bytes())
.map_err(|_| format_err!("invalid key length"))?;
hmac.input(url.as_str().as_ref());
let mut res = String::new();
hmac.result().code().as_slice().write_hex(&mut res)?;
Ok(res)
}
}
#[derive(Debug, Clone, Deserialize)]
struct ApiResult<R> {
success: bool,
message: String,
result: Option<R>,
}
impl<R> ApiResult<R>
where
R: for<'r> Deserialize<'r>,
{
fn into_result(self) -> Result<R, Error> {
if self.success {
if let Some(r) = self.result {
Ok(r)
} else {
Err(format_err!("invalid result in success response"))
}
} else {
Err(format_err!("{}", self.message))
}
}
}