@@ -20,14 +20,14 @@ use futures::prelude::*;
20
20
use crate :: query:: QueryType ;
21
21
use crate :: Error ;
22
22
use crate :: Query ;
23
- use std:: collections:: HashMap ;
24
23
use std:: sync:: Arc ;
25
24
26
25
#[ derive( Clone , Debug ) ]
27
26
/// Internal Representation of a Client
28
27
pub struct Client {
29
28
pub ( crate ) url : Arc < String > ,
30
- pub ( crate ) parameters : Arc < HashMap < & ' static str , String > > ,
29
+ pub ( crate ) database : ( & ' static str , String ) ,
30
+ pub ( crate ) credentials : Option < ( [ ( & ' static str , String ) ; 2 ] ) > ,
31
31
pub ( crate ) client : reqwest:: Client ,
32
32
}
33
33
@@ -51,12 +51,11 @@ impl Client {
51
51
S1 : Into < String > ,
52
52
S2 : Into < String > ,
53
53
{
54
- let mut parameters = HashMap :: < & str , String > :: new ( ) ;
55
- parameters. insert ( "db" , database. into ( ) ) ;
56
54
Client {
57
55
url : Arc :: new ( url. into ( ) ) ,
58
- parameters : Arc :: new ( parameters) ,
59
56
client : reqwest:: Client :: new ( ) ,
57
+ database : ( "db" , database. into ( ) ) ,
58
+ credentials : None ,
60
59
}
61
60
}
62
61
@@ -79,17 +78,14 @@ impl Client {
79
78
S1 : Into < String > ,
80
79
S2 : Into < String > ,
81
80
{
82
- let mut with_auth = self . parameters . as_ref ( ) . clone ( ) ;
83
- with_auth. insert ( "u" , username. into ( ) ) ;
84
- with_auth. insert ( "p" , password. into ( ) ) ;
85
- self . parameters = Arc :: new ( with_auth) ;
81
+ self . credentials = Some ( [ ( "u" , username. into ( ) ) , ( "p" , password. into ( ) ) ] ) ;
86
82
self
87
83
}
88
84
89
85
/// Returns the name of the database the client is using
90
86
pub fn database_name ( & self ) -> & str {
91
87
// safe to unwrap: we always set the database name in `Self::new`
92
- self . parameters . get ( "db" ) . unwrap ( )
88
+ & self . database . 1
93
89
}
94
90
95
91
/// Returns the URL of the InfluxDB installation the client is using
@@ -183,29 +179,22 @@ impl Client {
183
179
QueryType :: ReadQuery => {
184
180
let read_query = query. get ( ) ;
185
181
let url = & format ! ( "{}/query" , & self . url) ;
186
- let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
187
- parameters. insert ( "q" , read_query. clone ( ) ) ;
188
182
189
183
if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
190
- self . client
191
- . request ( reqwest:: Method :: GET , url)
192
- . query ( & parameters)
184
+ self . build_db_request ( reqwest:: Method :: GET , url)
185
+ . query ( & [ ( "q" , read_query) ] )
193
186
. build ( )
194
187
} else {
195
- self . client
196
- . request ( reqwest:: Method :: POST , url)
197
- . query ( & parameters)
188
+ self . build_db_request ( reqwest:: Method :: POST , url)
189
+ . query ( & [ ( "q" , read_query) ] )
198
190
. build ( )
199
191
}
200
192
}
201
193
QueryType :: WriteQuery ( precision) => {
202
194
let url = & format ! ( "{}/write" , & self . url) ;
203
- let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
204
- parameters. insert ( "precision" , precision) ;
205
- self . client
206
- . request ( reqwest:: Method :: POST , url)
195
+ self . build_db_request ( reqwest:: Method :: POST , url)
207
196
. body ( query. get ( ) )
208
- . query ( & parameters )
197
+ . query ( & [ ( "precision" , precision ) ] )
209
198
. build ( )
210
199
}
211
200
}
@@ -243,6 +232,16 @@ impl Client {
243
232
244
233
Ok ( s)
245
234
}
235
+
236
+ pub fn build_db_request ( & self , method : reqwest:: Method , url : & str ) -> reqwest:: RequestBuilder {
237
+ let mut req = self . client . request ( method, url) . query ( & [ & self . database ] ) ;
238
+
239
+ if let Some ( credentials) = & self . credentials {
240
+ req = req. query ( & credentials) ;
241
+ }
242
+
243
+ req
244
+ }
246
245
}
247
246
248
247
#[ cfg( test) ]
@@ -259,13 +258,12 @@ mod tests {
259
258
#[ test]
260
259
fn test_with_auth ( ) {
261
260
let client = Client :: new ( "http://localhost:8068" , "database" ) ;
262
- assert_eq ! ( client. parameters. len( ) , 1 ) ;
263
- assert_eq ! ( client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
261
+ assert_eq ! ( client. database, ( "db" , "database" . to_string( ) ) ) ;
264
262
265
263
let with_auth = client. with_auth ( "username" , "password" ) ;
266
- assert_eq ! ( with_auth . parameters . len ( ) , 3 ) ;
267
- assert_eq ! ( with_auth. parameters . get ( "db" ) . unwrap ( ) , "database" ) ;
268
- assert_eq ! ( with_auth . parameters . get ( "u" ) . unwrap ( ) , "username" ) ;
269
- assert_eq ! ( with_auth . parameters . get ( "p" ) . unwrap ( ) , "password" ) ;
264
+ assert_eq ! (
265
+ with_auth. credentials ,
266
+ Some ( [ ( "u" , "username" . to_string ( ) ) , ( "p" , "password" . to_string ( ) ) , ] )
267
+ ) ;
270
268
}
271
269
}
0 commit comments