-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathblocking.rs
82 lines (72 loc) · 2.33 KB
/
blocking.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
use std::future::Future;
use std::{collections::HashMap, error::Error};
use chrono::{DateTime, Utc};
use aw_models::{Bucket, Event};
use super::AwClient as AsyncAwClient;
pub struct AwClient {
client: AsyncAwClient,
pub baseurl: reqwest::Url,
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.client.baseurl)
}
}
fn block_on<F: Future>(f: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("build shell runtime")
.block_on(f)
}
macro_rules! proxy_method
{
($name:tt, $ret:ty, $($v:ident: $t:ty),*) => {
pub fn $name(&self, $($v: $t),*) -> Result<$ret, reqwest::Error>
{ block_on(self.client.$name($($v),*)) }
};
}
impl AwClient {
pub fn new(host: &str, port: u16, name: &str) -> Result<AwClient, Box<dyn Error>> {
let async_client = AsyncAwClient::new(host, port, name)?;
Ok(AwClient {
baseurl: async_client.baseurl.clone(),
name: async_client.name.clone(),
hostname: async_client.hostname.clone(),
client: async_client,
})
}
proxy_method!(get_bucket, Bucket, bucketname: &str);
proxy_method!(get_buckets, HashMap<String, Bucket>,);
proxy_method!(create_bucket, (), bucket: &Bucket);
proxy_method!(create_bucket_simple, (), bucketname: &str, buckettype: &str);
proxy_method!(delete_bucket, (), bucketname: &str);
proxy_method!(
get_events,
Vec<Event>,
bucketname: &str,
start: Option<DateTime<Utc>>,
stop: Option<DateTime<Utc>>,
limit: Option<u64>
);
proxy_method!(
query,
Vec<serde_json::Value>,
query: &str,
timeperiods: Vec<(DateTime<Utc>, DateTime<Utc>)>
);
proxy_method!(insert_event, (), bucketname: &str, event: &Event);
proxy_method!(insert_events, (), bucketname: &str, events: Vec<Event>);
proxy_method!(
heartbeat,
(),
bucketname: &str,
event: &Event,
pulsetime: f64
);
proxy_method!(delete_event, (), bucketname: &str, event_id: i64);
proxy_method!(get_event_count, i64, bucketname: &str);
proxy_method!(get_info, aw_models::Info,);
}