1616//! ```
1717
1818use futures:: prelude:: * ;
19- use reqwest :: { self , Client as ReqwestClient , StatusCode } ;
19+ use surf :: { self , Client as SurfClient , StatusCode } ;
2020
2121use crate :: query:: QueryTypes ;
2222use crate :: Error ;
2323use crate :: Query ;
24+ use std:: collections:: HashMap ;
2425use std:: sync:: Arc ;
2526
2627#[ derive( Clone , Debug ) ]
2728/// Internal Representation of a Client
2829pub struct Client {
2930 pub ( crate ) url : Arc < String > ,
30- pub ( crate ) parameters : Arc < Vec < ( & ' static str , String ) > > ,
31- pub ( crate ) client : ReqwestClient ,
31+ pub ( crate ) parameters : Arc < HashMap < & ' static str , String > > ,
32+ pub ( crate ) client : SurfClient ,
3233}
3334
3435impl Client {
@@ -51,10 +52,12 @@ impl Client {
5152 S1 : Into < String > ,
5253 S2 : Into < String > ,
5354 {
55+ let mut parameters = HashMap :: < & str , String > :: new ( ) ;
56+ parameters. insert ( "db" , database. into ( ) ) ;
5457 Client {
5558 url : Arc :: new ( url. into ( ) ) ,
56- parameters : Arc :: new ( vec ! [ ( "db" , database . into ( ) ) ] ) ,
57- client : ReqwestClient :: new ( ) ,
59+ parameters : Arc :: new ( parameters ) ,
60+ client : SurfClient :: new ( ) ,
5861 }
5962 }
6063
@@ -78,16 +81,16 @@ impl Client {
7881 S2 : Into < String > ,
7982 {
8083 let mut with_auth = self . parameters . as_ref ( ) . clone ( ) ;
81- with_auth. push ( ( "u" , username. into ( ) ) ) ;
82- with_auth. push ( ( "p" , password. into ( ) ) ) ;
84+ with_auth. insert ( "u" , username. into ( ) ) ;
85+ with_auth. insert ( "p" , password. into ( ) ) ;
8386 self . parameters = Arc :: new ( with_auth) ;
8487 self
8588 }
8689
8790 /// Returns the name of the database the client is using
8891 pub fn database_name ( & self ) -> & str {
8992 // safe to unwrap: we always set the database name in `Self::new`
90- & self . parameters . first ( ) . unwrap ( ) . 1
93+ self . parameters . get ( "db" ) . unwrap ( )
9194 }
9295
9396 /// Returns the URL of the InfluxDB installation the client is using
@@ -109,18 +112,8 @@ impl Client {
109112 error : format ! ( "{}" , err) ,
110113 } ) ?;
111114
112- let build = res
113- . headers ( )
114- . get ( "X-Influxdb-Build" )
115- . unwrap ( )
116- . to_str ( )
117- . unwrap ( ) ;
118- let version = res
119- . headers ( )
120- . get ( "X-Influxdb-Version" )
121- . unwrap ( )
122- . to_str ( )
123- . unwrap ( ) ;
115+ let build = res. header ( "X-Influxdb-Build" ) . unwrap ( ) . as_str ( ) ;
116+ let version = res. header ( "X-Influxdb-Version" ) . unwrap ( ) . as_str ( ) ;
124117
125118 Ok ( ( build. to_owned ( ) , version. to_owned ( ) ) )
126119 }
@@ -140,7 +133,7 @@ impl Client {
140133 /// use influxdb::InfluxDbWriteable;
141134 /// use std::time::{SystemTime, UNIX_EPOCH};
142135 ///
143- /// # #[tokio ::main]
136+ /// # #[async_std ::main]
144137 /// # async fn main() -> Result<(), influxdb::Error> {
145138 /// let start = SystemTime::now();
146139 /// let since_the_epoch = start
@@ -169,60 +162,55 @@ impl Client {
169162 & ' q Q : Into < QueryTypes < ' q > > ,
170163 {
171164 let query = q. build ( ) . map_err ( |err| Error :: InvalidQueryError {
172- error : format ! ( "{}" , err) ,
165+ error : err. to_string ( ) ,
173166 } ) ?;
174167
175168 let request_builder = match q. into ( ) {
176169 QueryTypes :: Read ( _) => {
177170 let read_query = query. get ( ) ;
178171 let url = & format ! ( "{}/query" , & self . url) ;
179- let query = [ ( "q" , & read_query) ] ;
172+ let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
173+ parameters. insert ( "q" , read_query. clone ( ) ) ;
180174
181175 if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
182- self . client
183- . get ( url)
184- . query ( self . parameters . as_ref ( ) )
185- . query ( & query)
176+ self . client . get ( url) . query ( & parameters)
186177 } else {
187- self . client
188- . post ( url)
189- . query ( self . parameters . as_ref ( ) )
190- . query ( & query)
178+ self . client . post ( url) . query ( & parameters)
191179 }
192180 }
193181 QueryTypes :: Write ( write_query) => {
194182 let url = & format ! ( "{}/write" , & self . url) ;
195- let precision = [ ( "precision" , write_query. get_precision ( ) ) ] ;
183+ let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
184+ parameters. insert ( "precision" , write_query. get_precision ( ) ) ;
196185
197- self . client
198- . post ( url)
199- . query ( self . parameters . as_ref ( ) )
200- . query ( & precision)
201- . body ( query. get ( ) )
186+ self . client . post ( url) . body ( query. get ( ) ) . query ( & parameters)
202187 }
203- } ;
204-
205- let request = request_builder
206- . build ( )
207- . map_err ( |err| Error :: UrlConstructionError {
208- error : format ! ( "{}" , & err) ,
209- } ) ?;
188+ }
189+ . map_err ( |err| Error :: UrlConstructionError {
190+ error : err. to_string ( ) ,
191+ } ) ?;
210192
211- let res = self
193+ let request = request_builder. build ( ) ;
194+ let mut res = self
212195 . client
213- . execute ( request)
214- . map_err ( |err| Error :: ConnectionError { error : err } )
196+ . send ( request)
197+ . map_err ( |err| Error :: ConnectionError {
198+ error : err. to_string ( ) ,
199+ } )
215200 . await ?;
216201
217202 match res. status ( ) {
218- StatusCode :: UNAUTHORIZED => return Err ( Error :: AuthorizationError ) ,
219- StatusCode :: FORBIDDEN => return Err ( Error :: AuthenticationError ) ,
203+ StatusCode :: Unauthorized => return Err ( Error :: AuthorizationError ) ,
204+ StatusCode :: Forbidden => return Err ( Error :: AuthenticationError ) ,
220205 _ => { }
221206 }
222207
223- let s = res. text ( ) . await . map_err ( |_| Error :: DeserializationError {
224- error : "response could not be converted to UTF-8" . to_string ( ) ,
225- } ) ?;
208+ let s = res
209+ . body_string ( )
210+ . await
211+ . map_err ( |_| Error :: DeserializationError {
212+ error : "response could not be converted to UTF-8" . to_string ( ) ,
213+ } ) ?;
226214
227215 // todo: improve error parsing without serde
228216 if s. contains ( "\" error\" " ) {
@@ -249,16 +237,13 @@ mod tests {
249237 #[ test]
250238 fn test_with_auth ( ) {
251239 let client = Client :: new ( "http://localhost:8068" , "database" ) ;
252- assert_eq ! ( vec![ ( "db" , "database" . to_string( ) ) ] , * client. parameters) ;
240+ assert_eq ! ( client. parameters. len( ) , 1 ) ;
241+ assert_eq ! ( client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
253242
254243 let with_auth = client. with_auth ( "username" , "password" ) ;
255- assert_eq ! (
256- vec![
257- ( "db" , "database" . to_string( ) ) ,
258- ( "u" , "username" . to_string( ) ) ,
259- ( "p" , "password" . to_string( ) )
260- ] ,
261- * with_auth. parameters
262- ) ;
244+ assert_eq ! ( with_auth. parameters. len( ) , 3 ) ;
245+ assert_eq ! ( with_auth. parameters. get( "db" ) . unwrap( ) , "database" ) ;
246+ assert_eq ! ( with_auth. parameters. get( "u" ) . unwrap( ) , "username" ) ;
247+ assert_eq ! ( with_auth. parameters. get( "p" ) . unwrap( ) , "password" ) ;
263248 }
264249}
0 commit comments