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
161
162
163
164
165
166
use core::panic;
use reqwest::{blocking::Client, Method, Result};
pub const R_OK: &str = "R_OK";
pub const R_TRUE: &str = "R_TRUE";
const R_PONG: &str = "PONG";
const R_INVALID_CREDENTIALS: &str = "INVALID_CREDENTIALS";
const R_AUTH_FAILED: &str = "AUTH_FAILED";
pub struct HyperClient {
address: String,
username: String,
password: String,
auth_enabled: bool,
token: String,
client: Option<Client>,
}
impl HyperClient {
pub fn new(address: String) -> HyperClient {
let mut hc = HyperClient {
address,
username: String::from(""),
password: String::from(""),
auth_enabled: false,
token: String::from(""),
client: None,
};
hc.connect();
hc
}
#[allow(dead_code)]
pub fn authenticate(&mut self, username: String, password: String) {
self.username = username;
self.password = password;
self.auth_enabled = true;
self.auth().expect("Authentication failed");
}
fn client(&self) -> Client {
match &self.client {
Some(c) => c.to_owned(),
None => Client::new(),
}
}
fn auth(&mut self) -> Result<String> {
let client = Client::new();
let resp = client
.post(&self.address)
.header("username", &self.username)
.header("password", &self.password)
.send()?
.text()?;
if resp == R_INVALID_CREDENTIALS {
panic!("Invalid credentials.");
}
self.token = resp.trim().to_string();
Ok(resp)
}
pub fn connect(&mut self) {
let r = self.http(Method::GET, &format!("ping"), "");
match r {
Err(e) => panic!("Unable to connect to the HyperDB server (E_001): {}", e),
Ok(r) => {
if r != R_PONG {
panic!("Unable to connect to the HyperDB server (E_002).")
}
println!("Connected to {}", self.address);
}
}
}
pub fn ping(&self) -> Result<String> {
self.client()
.get(String::from(&self.address) + "/ping")
.send()?
.text()
}
pub fn http(&mut self, method: Method, url: &String, body: &str) -> Result<String> {
let m = method.clone();
let mut r = self
.client()
.request(method, format!("{}/{}", &self.address, url))
.header("Auth", &self.token)
.body(body.to_string())
.send()?
.text()?;
if r == R_AUTH_FAILED && self.auth_enabled {
self.auth()?;
r = self
.client()
.request(m, format!("{}/{}", &self.address, url))
.header("Auth", &self.token)
.body(body.to_string())
.send()?
.text()?;
}
Ok(r)
}
pub fn version(&mut self) -> Result<String> {
self.http(Method::GET, &format!(""), "")
}
pub fn has(&mut self, key: &str) -> Result<String> {
self.http(Method::GET, &format!("has/{}", key), "")
}
pub fn get(&mut self, key: &str) -> Result<String> {
self.http(Method::GET, &format!("data/{}", key), "")
}
pub fn set(&mut self, key: &str, value: &str) -> Result<String> {
self.http(Method::POST, &format!("data/{}", key), value)
}
pub fn delete(&mut self, key: &str) -> Result<String> {
self.http(Method::DELETE, &format!("data/{}", key), "")
}
pub fn all(&mut self) -> Result<String> {
self.http(Method::GET, &format!("data"), "")
}
pub fn clear(&mut self) -> Result<String> {
self.http(Method::DELETE, &format!("data"), "")
}
pub fn empty(&mut self) -> Result<String> {
self.http(Method::GET, &format!("empty"), "")
}
pub fn save(&mut self) -> Result<String> {
self.http(Method::POST, &format!("save"), "")
}
pub fn reload(&mut self) -> Result<String> {
self.http(Method::POST, &format!("reload"), "")
}
pub fn reset(&mut self) -> Result<String> {
self.http(Method::DELETE, &format!("reset"), "")
}
}