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
use serde_json;
use serde_json::Value;
use coinnect::Credentials;
use exchange::Exchange;
use helpers;
use error::*;
use std::collections::HashMap;
use std::str::FromStr;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct BitstampCreds {
exchange: Exchange,
name: String,
data: HashMap<String, String>,
}
impl BitstampCreds {
pub fn new(name: &str, api_key: &str, api_secret: &str, customer_id: &str) -> Self {
let mut creds = BitstampCreds {
data: HashMap::new(),
exchange: Exchange::Bitstamp,
name: if name.is_empty() {
"BitstampClient".to_string()
} else {
name.to_string()
},
};
creds
.data
.insert("api_key".to_string(), api_key.to_string());
creds
.data
.insert("api_secret".to_string(), api_secret.to_string());
creds
.data
.insert("customer_id".to_string(), customer_id.to_string());
creds
}
pub fn new_from_file(name: &str, path: PathBuf) -> Result<Self> {
let mut f = File::open(&path)?;
let mut buffer = String::new();
f.read_to_string(&mut buffer)?;
let data: Value = serde_json::from_str(&buffer)?;
let json_obj = data.as_object()
.ok_or_else(|| ErrorKind::BadParse)?
.get(name)
.ok_or_else(|| ErrorKind::MissingField(name.to_string()))?;
let api_key = helpers::get_json_string(json_obj, "api_key")?;
let api_secret = helpers::get_json_string(json_obj, "api_secret")?;
let customer_id = helpers::get_json_string(json_obj, "customer_id")?;
let exchange = {
let exchange_str = helpers::get_json_string(json_obj, "exchange")?;
Exchange::from_str(exchange_str)
.chain_err(|| ErrorKind::InvalidFieldValue("exchange".to_string()))?
};
if exchange != Exchange::Bitstamp {
return Err(ErrorKind::InvalidConfigType(Exchange::Bitstamp, exchange).into());
}
Ok(BitstampCreds::new(name, api_key, api_secret, customer_id))
}
}
impl Credentials for BitstampCreds {
fn get(&self, key: &str) -> Option<String> {
if let Some(res) = self.data.get(key) {
Some(res.clone())
} else {
None
}
}
fn name(&self) -> String {
self.name.clone()
}
fn exchange(&self) -> Exchange {
self.exchange
}
}