@@ -3,6 +3,9 @@ extern crate chrono;
33extern crate gethostname;
44extern crate reqwest;
55extern crate serde_json;
6+ extern crate tokio;
7+
8+ pub mod blocking;
69
710use std:: collections:: HashMap ;
811use std:: vec:: Vec ;
@@ -13,7 +16,7 @@ use serde_json::Map;
1316pub use aw_models:: { Bucket , BucketMetadata , Event } ;
1417
1518pub struct AwClient {
16- client : reqwest:: blocking :: Client ,
19+ client : reqwest:: Client ,
1720 pub baseurl : String ,
1821 pub name : String ,
1922 pub hostname : String ,
@@ -28,7 +31,7 @@ impl std::fmt::Debug for AwClient {
2831impl AwClient {
2932 pub fn new ( ip : & str , port : & str , name : & str ) -> AwClient {
3033 let baseurl = format ! ( "http://{ip}:{port}" ) ;
31- let client = reqwest:: blocking :: Client :: builder ( )
34+ let client = reqwest:: Client :: builder ( )
3235 . timeout ( std:: time:: Duration :: from_secs ( 120 ) )
3336 . build ( )
3437 . unwrap ( ) ;
@@ -41,24 +44,31 @@ impl AwClient {
4144 }
4245 }
4346
44- pub fn get_bucket ( & self , bucketname : & str ) -> Result < Bucket , reqwest:: Error > {
47+ pub async fn get_bucket ( & self , bucketname : & str ) -> Result < Bucket , reqwest:: Error > {
4548 let url = format ! ( "{}/api/0/buckets/{}" , self . baseurl, bucketname) ;
46- let bucket = self . client . get ( url) . send ( ) ?. error_for_status ( ) ?. json ( ) ?;
49+ let bucket = self
50+ . client
51+ . get ( url)
52+ . send ( )
53+ . await ?
54+ . error_for_status ( ) ?
55+ . json ( )
56+ . await ?;
4757 Ok ( bucket)
4858 }
4959
50- pub fn get_buckets ( & self ) -> Result < HashMap < String , Bucket > , reqwest:: Error > {
60+ pub async fn get_buckets ( & self ) -> Result < HashMap < String , Bucket > , reqwest:: Error > {
5161 let url = format ! ( "{}/api/0/buckets/" , self . baseurl) ;
52- self . client . get ( url) . send ( ) ?. json ( )
62+ self . client . get ( url) . send ( ) . await ?. json ( ) . await
5363 }
5464
55- pub fn create_bucket ( & self , bucket : & Bucket ) -> Result < ( ) , reqwest:: Error > {
65+ pub async fn create_bucket ( & self , bucket : & Bucket ) -> Result < ( ) , reqwest:: Error > {
5666 let url = format ! ( "{}/api/0/buckets/{}" , self . baseurl, bucket. id) ;
57- self . client . post ( url) . json ( bucket) . send ( ) ?;
67+ self . client . post ( url) . json ( bucket) . send ( ) . await ?;
5868 Ok ( ( ) )
5969 }
6070
61- pub fn create_bucket_simple (
71+ pub async fn create_bucket_simple (
6272 & self ,
6373 bucketname : & str ,
6474 buckettype : & str ,
@@ -75,16 +85,16 @@ impl AwClient {
7585 created : None ,
7686 last_updated : None ,
7787 } ;
78- self . create_bucket ( & bucket)
88+ self . create_bucket ( & bucket) . await
7989 }
8090
81- pub fn delete_bucket ( & self , bucketname : & str ) -> Result < ( ) , reqwest:: Error > {
91+ pub async fn delete_bucket ( & self , bucketname : & str ) -> Result < ( ) , reqwest:: Error > {
8292 let url = format ! ( "{}/api/0/buckets/{}" , self . baseurl, bucketname) ;
83- self . client . delete ( url) . send ( ) ?;
93+ self . client . delete ( url) . send ( ) . await ?;
8494 Ok ( ( ) )
8595 }
8696
87- pub fn get_events (
97+ pub async fn get_events (
8898 & self ,
8999 bucketname : & str ,
90100 start : Option < DateTime < Utc > > ,
@@ -109,27 +119,31 @@ impl AwClient {
109119 url. query_pairs_mut ( )
110120 . append_pair ( "limit" , s. to_string ( ) . as_str ( ) ) ;
111121 } ;
112- self . client . get ( url) . send ( ) ?. json ( )
122+ self . client . get ( url) . send ( ) . await ?. json ( ) . await
113123 }
114124
115- pub fn insert_event ( & self , bucketname : & str , event : & Event ) -> Result < ( ) , reqwest:: Error > {
125+ pub async fn insert_event (
126+ & self ,
127+ bucketname : & str ,
128+ event : & Event ,
129+ ) -> Result < ( ) , reqwest:: Error > {
116130 let url = format ! ( "{}/api/0/buckets/{}/events" , self . baseurl, bucketname) ;
117131 let eventlist = vec ! [ event. clone( ) ] ;
118- self . client . post ( url) . json ( & eventlist) . send ( ) ?;
132+ self . client . post ( url) . json ( & eventlist) . send ( ) . await ?;
119133 Ok ( ( ) )
120134 }
121135
122- pub fn insert_events (
136+ pub async fn insert_events (
123137 & self ,
124138 bucketname : & str ,
125139 events : Vec < Event > ,
126140 ) -> Result < ( ) , reqwest:: Error > {
127141 let url = format ! ( "{}/api/0/buckets/{}/events" , self . baseurl, bucketname) ;
128- self . client . post ( url) . json ( & events) . send ( ) ?;
142+ self . client . post ( url) . json ( & events) . send ( ) . await ?;
129143 Ok ( ( ) )
130144 }
131145
132- pub fn heartbeat (
146+ pub async fn heartbeat (
133147 & self ,
134148 bucketname : & str ,
135149 event : & Event ,
@@ -139,31 +153,42 @@ impl AwClient {
139153 "{}/api/0/buckets/{}/heartbeat?pulsetime={}" ,
140154 self . baseurl, bucketname, pulsetime
141155 ) ;
142- self . client . post ( url) . json ( & event) . send ( ) ?;
156+ self . client . post ( url) . json ( & event) . send ( ) . await ?;
143157 Ok ( ( ) )
144158 }
145159
146- pub fn delete_event ( & self , bucketname : & str , event_id : i64 ) -> Result < ( ) , reqwest:: Error > {
160+ pub async fn delete_event (
161+ & self ,
162+ bucketname : & str ,
163+ event_id : i64 ,
164+ ) -> Result < ( ) , reqwest:: Error > {
147165 let url = format ! (
148166 "{}/api/0/buckets/{}/events/{}" ,
149167 self . baseurl, bucketname, event_id
150168 ) ;
151- self . client . delete ( url) . send ( ) ?;
169+ self . client . delete ( url) . send ( ) . await ?;
152170 Ok ( ( ) )
153171 }
154172
155- pub fn get_event_count ( & self , bucketname : & str ) -> Result < i64 , reqwest:: Error > {
173+ pub async fn get_event_count ( & self , bucketname : & str ) -> Result < i64 , reqwest:: Error > {
156174 let url = format ! ( "{}/api/0/buckets/{}/events/count" , self . baseurl, bucketname) ;
157- let res = self . client . get ( url) . send ( ) ?. error_for_status ( ) ?. text ( ) ?;
175+ let res = self
176+ . client
177+ . get ( url)
178+ . send ( )
179+ . await ?
180+ . error_for_status ( ) ?
181+ . text ( )
182+ . await ?;
158183 let count: i64 = match res. trim ( ) . parse ( ) {
159184 Ok ( count) => count,
160185 Err ( err) => panic ! ( "could not parse get_event_count response: {err:?}" ) ,
161186 } ;
162187 Ok ( count)
163188 }
164189
165- pub fn get_info ( & self ) -> Result < aw_models:: Info , reqwest:: Error > {
190+ pub async fn get_info ( & self ) -> Result < aw_models:: Info , reqwest:: Error > {
166191 let url = format ! ( "{}/api/0/info" , self . baseurl) ;
167- self . client . get ( url) . send ( ) ?. json ( )
192+ self . client . get ( url) . send ( ) . await ?. json ( ) . await
168193 }
169194}
0 commit comments