@@ -19,31 +19,28 @@ use futures_util::TryFutureExt;
19
19
use reqwest:: { Client as HttpClient , RequestBuilder , Response as HttpResponse } ;
20
20
use std:: collections:: { BTreeMap , HashMap } ;
21
21
use std:: fmt:: { self , Debug , Formatter } ;
22
- use std:: marker:: PhantomData ;
23
22
use std:: sync:: Arc ;
24
23
25
24
use crate :: query:: QueryType ;
26
25
use crate :: Error ;
27
26
use crate :: Query ;
28
27
29
- /// Marker type for InfluxDB Version 1
30
- #[ derive( Clone ) ]
31
- pub struct InfluxVersion1 ;
32
- /// Marker type for InfluxDB Version 2
33
- #[ derive( Clone ) ]
34
- pub struct InfluxVersion2 ;
35
- /// Marker type for InfluxDB Version 3
36
- #[ derive( Clone ) ]
37
- pub struct InfluxVersion3 ;
28
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
29
+ #[ non_exhaustive]
30
+ pub enum InfluxDbVersion {
31
+ V1 ,
32
+ V2 ,
33
+ V3 ,
34
+ }
38
35
39
36
#[ derive( Clone ) ]
40
37
/// Internal Representation of a Client
41
- pub struct Client < V , H = reqwest:: Client > {
38
+ pub struct Client < H = reqwest:: Client > {
42
39
pub ( crate ) url : Arc < String > ,
43
40
pub ( crate ) parameters : Arc < HashMap < & ' static str , String > > ,
44
41
pub ( crate ) token : Option < String > ,
45
42
pub ( crate ) client : H ,
46
- _version : PhantomData < V > ,
43
+ pub ( crate ) version : InfluxDbVersion ,
47
44
}
48
45
49
46
struct RedactPassword < ' a > ( & ' a HashMap < & ' static str , String > ) ;
@@ -62,17 +59,18 @@ impl<'a> Debug for RedactPassword<'a> {
62
59
}
63
60
}
64
61
65
- impl < V , H > Debug for Client < V , H > {
62
+ impl < H > Debug for Client < H > {
66
63
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
67
64
f. debug_struct ( "Client" )
68
65
. field ( "url" , & self . url )
69
66
. field ( "parameters" , & RedactPassword ( & self . parameters ) )
67
+ . field ( "version" , & self . version )
70
68
. finish_non_exhaustive ( )
71
69
}
72
70
}
73
71
74
- impl < V > Client < V , reqwest:: Client > {
75
- /// Instantiates a new [`Client`](crate::Client)
72
+ impl Client < reqwest:: Client > {
73
+ /// Instantiates a new [`Client`](crate::Client) for InfluxDB v1.
76
74
///
77
75
/// # Arguments
78
76
///
@@ -94,12 +92,78 @@ impl<V> Client<V, reqwest::Client> {
94
92
{
95
93
let mut parameters = HashMap :: < & str , String > :: new ( ) ;
96
94
parameters. insert ( "db" , database. into ( ) ) ;
97
- Client {
95
+ Self {
98
96
url : Arc :: new ( url. into ( ) ) ,
99
97
parameters : Arc :: new ( parameters) ,
100
98
client : HttpClient :: new ( ) ,
101
99
token : None ,
102
- _version : PhantomData ,
100
+ version : InfluxDbVersion :: V1 ,
101
+ }
102
+ }
103
+
104
+ /// Instantiates a new [`Client`](crate::Client) for InfluxDB v2.
105
+ ///
106
+ /// # Arguments
107
+ ///
108
+ /// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
109
+ /// * `token`: The InfluxDB v2 authentication token.
110
+ /// * `bucket`: The InfluxDB v2 bucket.
111
+ ///
112
+ /// # Examples
113
+ ///
114
+ /// ```rust
115
+ /// use influxdb::Client;
116
+ ///
117
+ /// let _client = Client::v2("http://localhost:8086", "some-token", "my-bucket");
118
+ /// ```
119
+ #[ must_use = "Creating a client is pointless unless you use it" ]
120
+ pub fn v2 < S1 , S2 , S3 > ( url : S1 , token : S2 , bucket : S3 ) -> Self
121
+ where
122
+ S1 : Into < String > ,
123
+ S2 : Into < String > ,
124
+ S3 : Into < String > ,
125
+ {
126
+ let mut parameters = HashMap :: < & str , String > :: new ( ) ;
127
+ parameters. insert ( "bucket" , bucket. into ( ) ) ;
128
+ Self {
129
+ url : Arc :: new ( url. into ( ) ) ,
130
+ parameters : Arc :: new ( parameters) ,
131
+ client : HttpClient :: new ( ) ,
132
+ token : Some ( token. into ( ) ) ,
133
+ version : InfluxDbVersion :: V2 ,
134
+ }
135
+ }
136
+
137
+ /// Instantiates a new [`Client`](crate::Client) for InfluxDB v3.
138
+ ///
139
+ /// # Arguments
140
+ ///
141
+ /// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
142
+ /// * `token`: The InfluxDB v3 authentication token.
143
+ /// * `database`: The InfluxDB v3 database.
144
+ ///
145
+ /// # Examples
146
+ ///
147
+ /// ```rust
148
+ /// use influxdb::Client;
149
+ ///
150
+ /// let _client = Client::v3("http://localhost:8086", "some-token", "my-database");
151
+ /// ```
152
+ #[ must_use = "Creating a client is pointless unless you use it" ]
153
+ pub fn v3 < S1 , S2 , S3 > ( url : S1 , token : S2 , database : S3 ) -> Self
154
+ where
155
+ S1 : Into < String > ,
156
+ S2 : Into < String > ,
157
+ S3 : Into < String > ,
158
+ {
159
+ let mut parameters = HashMap :: < & str , String > :: new ( ) ;
160
+ parameters. insert ( "db" , database. into ( ) ) ;
161
+ Self {
162
+ url : Arc :: new ( url. into ( ) ) ,
163
+ parameters : Arc :: new ( parameters) ,
164
+ client : HttpClient :: new ( ) ,
165
+ token : Some ( token. into ( ) ) ,
166
+ version : InfluxDbVersion :: V3 ,
103
167
}
104
168
}
105
169
@@ -302,16 +366,12 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {
302
366
303
367
#[ cfg( test) ]
304
368
mod tests {
305
-
306
- use crate :: client:: InfluxVersion1 ;
307
-
308
- use super :: Client ;
369
+ use super :: { Client , InfluxDbVersion } ;
309
370
use indoc:: indoc;
310
371
311
372
#[ test]
312
373
fn test_client_debug_redacted_password ( ) {
313
- let client: Client < InfluxVersion1 > =
314
- Client :: new ( "https://localhost:8086" , "db" ) . with_auth ( "user" , "pass" ) ;
374
+ let client = Client :: new ( "https://localhost:8086" , "db" ) . with_auth ( "user" , "pass" ) ;
315
375
let actual = format ! ( "{client:#?}" ) ;
316
376
let expected = indoc ! { r#"
317
377
Client {
@@ -321,6 +381,7 @@ mod tests {
321
381
"p": "<redacted>",
322
382
"u": "user",
323
383
},
384
+ version: V1,
324
385
..
325
386
}
326
387
"# } ;
@@ -329,14 +390,14 @@ mod tests {
329
390
330
391
#[ test]
331
392
fn test_fn_database ( ) {
332
- let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
393
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
333
394
assert_eq ! ( client. database_name( ) , "database" ) ;
334
395
assert_eq ! ( client. database_url( ) , "http://localhost:8068" ) ;
335
396
}
336
397
337
398
#[ test]
338
399
fn test_with_auth ( ) {
339
- let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
400
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
340
401
assert_eq ! ( client. parameters. len( ) , 1 ) ;
341
402
assert_eq ! ( client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
342
403
@@ -346,10 +407,23 @@ mod tests {
346
407
assert_eq ! ( with_auth. parameters. get( "u" ) . unwrap( ) , "username" ) ;
347
408
assert_eq ! ( with_auth. parameters. get( "p" ) . unwrap( ) , "password" ) ;
348
409
349
- let client: Client < InfluxVersion1 > = Client :: new ( "http://localhost:8068" , "database" ) ;
410
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
350
411
let with_auth = client. with_token ( "token" ) ;
351
412
assert_eq ! ( with_auth. parameters. len( ) , 1 ) ;
352
413
assert_eq ! ( with_auth. parameters. get( "db" ) . unwrap( ) , "database" ) ;
353
414
assert_eq ! ( with_auth. token. unwrap( ) , "token" ) ;
354
415
}
416
+
417
+ #[ test]
418
+ fn test_v2_and_v3_clients ( ) {
419
+ let v2_client = Client :: v2 ( "http://localhost:8086" , "token" , "bucket" ) ;
420
+ assert_eq ! ( v2_client. version, InfluxDbVersion :: V2 ) ;
421
+ assert_eq ! ( v2_client. token. unwrap( ) , "token" ) ;
422
+ assert_eq ! ( v2_client. parameters. get( "bucket" ) . unwrap( ) , "bucket" ) ;
423
+
424
+ let v3_client = Client :: v3 ( "http://localhost:8086" , "token" , "database" ) ;
425
+ assert_eq ! ( v3_client. version, InfluxDbVersion :: V3 ) ;
426
+ assert_eq ! ( v3_client. token. unwrap( ) , "token" ) ;
427
+ assert_eq ! ( v3_client. parameters. get( "db" ) . unwrap( ) , "database" ) ;
428
+ }
355
429
}
0 commit comments