@@ -23,18 +23,30 @@ use std::fmt::{self, Debug, Formatter};
23
23
use std:: sync:: Arc ;
24
24
#[ cfg( feature = "surf" ) ]
25
25
use surf:: { Client as HttpClient , RequestBuilder , Response as HttpResponse } ;
26
+ use std:: marker:: PhantomData ;
26
27
27
28
use crate :: query:: QueryType ;
28
29
use crate :: Error ;
29
30
use crate :: Query ;
30
31
32
+ /// Marker type for InfluxDB Version 1
33
+ #[ derive( Clone ) ]
34
+ pub struct InfluxVersion1 ;
35
+ /// Marker type for InfluxDB Version 2
36
+ #[ derive( Clone ) ]
37
+ pub struct InfluxVersion2 ;
38
+ /// Marker type for InfluxDB Version 3
39
+ #[ derive( Clone ) ]
40
+ pub struct InfluxVersion3 ;
41
+
31
42
#[ derive( Clone ) ]
32
43
/// Internal Representation of a Client
33
- pub struct Client {
44
+ pub struct Client < V , H = reqwest :: Client > {
34
45
pub ( crate ) url : Arc < String > ,
35
46
pub ( crate ) parameters : Arc < HashMap < & ' static str , String > > ,
36
47
pub ( crate ) token : Option < String > ,
37
- pub ( crate ) client : HttpClient ,
48
+ pub ( crate ) client : H ,
49
+ _version : PhantomData < V > ,
38
50
}
39
51
40
52
struct RedactPassword < ' a > ( & ' a HashMap < & ' static str , String > ) ;
@@ -53,7 +65,7 @@ impl<'a> Debug for RedactPassword<'a> {
53
65
}
54
66
}
55
67
56
- impl Debug for Client {
68
+ impl < V , H > Debug for Client < V , H > {
57
69
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
58
70
f. debug_struct ( "Client" )
59
71
. field ( "url" , & self . url )
@@ -62,7 +74,7 @@ impl Debug for Client {
62
74
}
63
75
}
64
76
65
- impl Client {
77
+ impl < V > Client < V , reqwest :: Client > {
66
78
/// Instantiates a new [`Client`](crate::Client)
67
79
///
68
80
/// # Arguments
@@ -90,6 +102,7 @@ impl Client {
90
102
parameters : Arc :: new ( parameters) ,
91
103
client : HttpClient :: new ( ) ,
92
104
token : None ,
105
+ _version : PhantomData ,
93
106
}
94
107
}
95
108
@@ -308,12 +321,15 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {
308
321
309
322
#[ cfg( test) ]
310
323
mod tests {
324
+
325
+ use crate :: client:: InfluxVersion1 ;
326
+
311
327
use super :: Client ;
312
328
use indoc:: indoc;
313
329
314
330
#[ test]
315
331
fn test_client_debug_redacted_password ( ) {
316
- let client = Client :: new ( "https://localhost:8086" , "db" ) . with_auth ( "user" , "pass" ) ;
332
+ let client: Client < InfluxVersion1 > = Client :: new ( "https://localhost:8086" , "db" ) . with_auth ( "user" , "pass" ) ;
317
333
let actual = format ! ( "{client:#?}" ) ;
318
334
let expected = indoc ! { r#"
319
335
Client {
@@ -331,14 +347,14 @@ mod tests {
331
347
332
348
#[ test]
333
349
fn test_fn_database ( ) {
334
- let client = Client :: new ( "http://localhost:8068" , "database" ) ;
350
+ let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
335
351
assert_eq ! ( client. database_name( ) , "database" ) ;
336
352
assert_eq ! ( client. database_url( ) , "http://localhost:8068" ) ;
337
353
}
338
354
339
355
#[ test]
340
356
fn test_with_auth ( ) {
341
- let client = Client :: new ( "http://localhost:8068" , "database" ) ;
357
+ let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
342
358
assert_eq ! ( client. parameters. len( ) , 1 ) ;
343
359
assert_eq ! ( client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
344
360
@@ -348,7 +364,7 @@ mod tests {
348
364
assert_eq ! ( with_auth. parameters. get( "u" ) . unwrap( ) , "username" ) ;
349
365
assert_eq ! ( with_auth. parameters. get( "p" ) . unwrap( ) , "password" ) ;
350
366
351
- let client = Client :: new ( "http://localhost:8068" , "database" ) ;
367
+ let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
352
368
let with_auth = client. with_token ( "token" ) ;
353
369
assert_eq ! ( with_auth. parameters. len( ) , 1 ) ;
354
370
assert_eq ! ( with_auth. parameters. get( "db" ) . unwrap( ) , "database" ) ;
0 commit comments