-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathlib.rs
170 lines (151 loc) · 5.29 KB
/
lib.rs
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
167
168
169
170
extern crate aw_models;
extern crate chrono;
extern crate gethostname;
extern crate reqwest;
extern crate serde_json;
use std::collections::HashMap;
use std::vec::Vec;
use chrono::{DateTime, Utc};
use serde_json::Map;
pub use aw_models::{Bucket, BucketMetadata, Event};
pub struct AwClient {
client: reqwest::blocking::Client,
pub baseurl: String,
pub name: String,
pub hostname: String,
}
impl std::fmt::Debug for AwClient {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "AwClient(baseurl={:?})", self.baseurl)
}
}
impl AwClient {
pub fn new(ip: &str, port: &str, name: &str) -> AwClient {
let baseurl = format!("http://{}:{}", ip, port);
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(120))
.build()
.unwrap();
let hostname = gethostname::gethostname().into_string().unwrap();
AwClient {
client,
baseurl,
name: name.to_string(),
hostname,
}
}
pub fn get_bucket(&self, bucketname: &str) -> Result<Bucket, reqwest::Error> {
let url = format!("{}/api/0/buckets/{}", self.baseurl, bucketname);
let bucket = self.client.get(&url).send()?.error_for_status()?.json()?;
Ok(bucket)
}
pub fn get_buckets(&self) -> Result<HashMap<String, Bucket>, reqwest::Error> {
let url = format!("{}/api/0/buckets/", self.baseurl);
self.client.get(&url).send()?.json()
}
pub fn create_bucket(&self, bucket: &Bucket) -> Result<(), reqwest::Error> {
let url = format!("{}/api/0/buckets/{}", self.baseurl, bucket.id);
self.client.post(&url).json(bucket).send()?;
Ok(())
}
pub fn create_bucket_simple(
&self,
bucketname: &str,
buckettype: &str,
) -> Result<Bucket, reqwest::Error> {
let bucket = Bucket {
bid: None,
id: format!("{}_{}", bucketname, self.hostname),
client: self.name.clone(),
_type: buckettype.to_string(),
hostname: self.hostname.clone(),
data: Map::default(),
metadata: BucketMetadata::default(),
events: None,
created: None,
last_updated: None,
};
self.create_bucket(&bucket)?;
Ok(bucket)
}
pub fn delete_bucket(&self, bucketname: &str) -> Result<(), reqwest::Error> {
let url = format!("{}/api/0/buckets/{}", self.baseurl, bucketname);
self.client.delete(&url).send()?;
Ok(())
}
pub fn get_events(
&self,
bucketname: &str,
start: Option<DateTime<Utc>>,
stop: Option<DateTime<Utc>>,
limit: Option<u64>,
) -> Result<Vec<Event>, reqwest::Error> {
let mut url = reqwest::Url::parse(
format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname).as_str(),
)
.unwrap();
// Must be a better way to build URLs
if let Some(s) = start {
url.query_pairs_mut()
.append_pair("start", s.to_rfc3339().as_str());
};
if let Some(s) = stop {
url.query_pairs_mut()
.append_pair("end", s.to_rfc3339().as_str());
};
if let Some(s) = limit {
url.query_pairs_mut()
.append_pair("limit", s.to_string().as_str());
};
self.client.get(url).send()?.json()
}
pub fn insert_event(&self, bucketname: &str, event: &Event) -> Result<(), reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname);
let eventlist = vec![event.clone()];
self.client.post(&url).json(&eventlist).send()?;
Ok(())
}
pub fn insert_events(
&self,
bucketname: &str,
events: Vec<Event>,
) -> Result<(), reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname);
self.client.post(&url).json(&events).send()?;
Ok(())
}
pub fn heartbeat(
&self,
bucketname: &str,
event: &Event,
pulsetime: f64,
) -> Result<(), reqwest::Error> {
let url = format!(
"{}/api/0/buckets/{}/heartbeat?pulsetime={}",
self.baseurl, bucketname, pulsetime
);
self.client.post(&url).json(&event).send()?;
Ok(())
}
pub fn delete_event(&self, bucketname: &str, event_id: i64) -> Result<(), reqwest::Error> {
let url = format!(
"{}/api/0/buckets/{}/events/{}",
self.baseurl, bucketname, event_id
);
self.client.delete(&url).send()?;
Ok(())
}
pub fn get_event_count(&self, bucketname: &str) -> Result<i64, reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events/count", self.baseurl, bucketname);
let res = self.client.get(&url).send()?.error_for_status()?.text()?;
let count: i64 = match res.trim().parse() {
Ok(count) => count,
Err(err) => panic!("could not parse get_event_count response: {:?}", err),
};
Ok(count)
}
pub fn get_info(&self) -> Result<aw_models::Info, reqwest::Error> {
let url = format!("{}/api/0/info", self.baseurl);
self.client.get(&url).send()?.json()
}
}