@@ -30,8 +30,6 @@ pub struct Client {
30
30
pub ( crate ) url : Arc < String > ,
31
31
pub ( crate ) parameters : Arc < HashMap < & ' static str , String > > ,
32
32
pub ( crate ) client : SurfClient ,
33
- pub ( crate ) username : Option < String > ,
34
- pub ( crate ) password : Option < String > ,
35
33
pub ( crate ) token : Option < String > ,
36
34
}
37
35
@@ -61,8 +59,6 @@ impl Client {
61
59
url : Arc :: new ( url. into ( ) ) ,
62
60
parameters : Arc :: new ( parameters) ,
63
61
client : SurfClient :: new ( ) ,
64
- username : None ,
65
- password : None ,
66
62
token : None ,
67
63
}
68
64
}
@@ -86,8 +82,10 @@ impl Client {
86
82
S1 : Into < String > ,
87
83
S2 : Into < String > ,
88
84
{
89
- self . username = Some ( username. into ( ) ) ;
90
- self . password = Some ( password. into ( ) ) ;
85
+ let mut with_auth = self . parameters . as_ref ( ) . clone ( ) ;
86
+ with_auth. insert ( "u" , username. into ( ) ) ;
87
+ with_auth. insert ( "p" , password. into ( ) ) ;
88
+ self . parameters = Arc :: new ( with_auth) ;
91
89
self
92
90
}
93
91
@@ -181,11 +179,11 @@ impl Client {
181
179
error : err. to_string ( ) ,
182
180
} ) ?;
183
181
182
+ let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
184
183
let request_builder = match q. get_type ( ) {
185
184
QueryType :: ReadQuery => {
186
185
let read_query = query. get ( ) ;
187
186
let url = & format ! ( "{}/query" , & self . url) ;
188
- let mut parameters = self . parameters . as_ref ( ) . clone ( ) ;
189
187
parameters. insert ( "q" , read_query. clone ( ) ) ;
190
188
191
189
if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
@@ -206,7 +204,7 @@ impl Client {
206
204
error : err. to_string ( ) ,
207
205
} ) ?;
208
206
209
- let request = self . try_authed ( request_builder) . build ( ) ;
207
+ let request = self . auth_if_needed ( request_builder) . build ( ) ;
210
208
let mut res = self
211
209
. client
212
210
. send ( request)
@@ -238,10 +236,8 @@ impl Client {
238
236
Ok ( s)
239
237
}
240
238
241
- fn try_authed ( & self , rb : RequestBuilder ) -> RequestBuilder {
242
- if let ( Some ( ref username) , Some ( ref password) ) = ( & self . username , & self . password ) {
243
- rb. header ( "Authorization" , format ! ( "Basic {}:{}" , username, password) )
244
- } else if let Some ( ref token) = self . token {
239
+ fn auth_if_needed ( & self , rb : RequestBuilder ) -> RequestBuilder {
240
+ if let Some ( ref token) = self . token {
245
241
rb. header ( "Authorization" , format ! ( "Token {}" , token) )
246
242
} else {
247
243
rb
@@ -263,14 +259,14 @@ mod tests {
263
259
#[ test]
264
260
fn test_with_auth ( ) {
265
261
let client = Client :: new ( "http://localhost:8068" , "database" ) ;
266
- assert_eq ! ( client. parameters. len( ) , 1 ) ;
262
+ assert_eq ! ( client. parameters. len( ) , 3 ) ;
267
263
assert_eq ! ( client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
268
264
269
265
let with_auth = client. with_auth ( "username" , "password" ) ;
270
- assert_eq ! ( with_auth. parameters. len( ) , 1 ) ;
266
+ assert_eq ! ( with_auth. parameters. len( ) , 3 ) ;
271
267
assert_eq ! ( with_auth. parameters. get( "db" ) . unwrap( ) , "database" ) ;
272
- assert_eq ! ( with_auth. username . unwrap( ) , "username" ) ;
273
- assert_eq ! ( with_auth. password . unwrap( ) , "password" ) ;
268
+ assert_eq ! ( with_auth. parameters . get ( "u" ) . unwrap( ) , "username" ) ;
269
+ assert_eq ! ( with_auth. parameters . get ( "p" ) . unwrap( ) , "password" ) ;
274
270
275
271
let client = Client :: new ( "http://localhost:8068" , "database" ) ;
276
272
let with_auth = client. with_token ( "token" ) ;
0 commit comments