8
8
//! # Examples
9
9
//!
10
10
//! ```rust
11
- //! use influxdb::client::InfluxDbClient ;
11
+ //! use influxdb::Client ;
12
12
//!
13
- //! let client = InfluxDbClient ::new("http://localhost:8086", "test");
13
+ //! let client = Client ::new("http://localhost:8086", "test");
14
14
//!
15
15
//! assert_eq!(client.database_name(), "test");
16
16
//! ```
17
17
18
18
use futures:: { Future , Stream } ;
19
- use reqwest:: r#async:: { Client , Decoder } ;
19
+ use reqwest:: r#async:: { Client as ReqwestClient , Decoder } ;
20
20
use reqwest:: { StatusCode , Url } ;
21
21
22
22
use std:: mem;
23
23
24
- use crate :: error:: InfluxDbError ;
25
- use crate :: query:: { InfluxDbQuery , InfluxDbQueryTypes } ;
24
+ use crate :: query:: QueryTypes ;
25
+ use crate :: Error ;
26
+ use crate :: Query ;
26
27
27
28
#[ derive( Clone , Debug ) ]
28
29
/// Internal Authentication representation
29
- pub ( crate ) struct InfluxDbAuthentication {
30
+ pub ( crate ) struct Authentication {
30
31
pub username : String ,
31
32
pub password : String ,
32
33
}
33
34
34
35
#[ derive( Clone , Debug ) ]
35
36
/// Internal Representation of a Client
36
- pub struct InfluxDbClient {
37
+ pub struct Client {
37
38
url : String ,
38
39
database : String ,
39
- auth : Option < InfluxDbAuthentication > ,
40
+ auth : Option < Authentication > ,
40
41
}
41
42
42
- impl Into < Vec < ( String , String ) > > for InfluxDbClient {
43
+ impl Into < Vec < ( String , String ) > > for Client {
43
44
fn into ( self ) -> Vec < ( String , String ) > {
44
45
let mut vec: Vec < ( String , String ) > = Vec :: new ( ) ;
45
46
vec. push ( ( "db" . to_string ( ) , self . database ) ) ;
@@ -51,7 +52,7 @@ impl Into<Vec<(String, String)>> for InfluxDbClient {
51
52
}
52
53
}
53
54
54
- impl < ' a > Into < Vec < ( String , String ) > > for & ' a InfluxDbClient {
55
+ impl < ' a > Into < Vec < ( String , String ) > > for & ' a Client {
55
56
fn into ( self ) -> Vec < ( String , String ) > {
56
57
let mut vec: Vec < ( String , String ) > = Vec :: new ( ) ;
57
58
vec. push ( ( "db" . to_string ( ) , self . database . to_owned ( ) ) ) ;
@@ -63,8 +64,8 @@ impl<'a> Into<Vec<(String, String)>> for &'a InfluxDbClient {
63
64
}
64
65
}
65
66
66
- impl InfluxDbClient {
67
- /// Instantiates a new [`InfluxDbClient `](crate::client::InfluxDbClient )
67
+ impl Client {
68
+ /// Instantiates a new [`Client `](crate::Client )
68
69
///
69
70
/// # Arguments
70
71
///
@@ -74,23 +75,23 @@ impl InfluxDbClient {
74
75
/// # Examples
75
76
///
76
77
/// ```rust
77
- /// use influxdb::client::InfluxDbClient ;
78
+ /// use influxdb::Client ;
78
79
///
79
- /// let _client = InfluxDbClient ::new("http://localhost:8086", "test");
80
+ /// let _client = Client ::new("http://localhost:8086", "test");
80
81
/// ```
81
82
pub fn new < S1 , S2 > ( url : S1 , database : S2 ) -> Self
82
83
where
83
84
S1 : ToString ,
84
85
S2 : ToString ,
85
86
{
86
- InfluxDbClient {
87
+ Client {
87
88
url : url. to_string ( ) ,
88
89
database : database. to_string ( ) ,
89
90
auth : None ,
90
91
}
91
92
}
92
93
93
- /// Add authentication/authorization information to [`InfluxDbClient `](crate::client::InfluxDbClient )
94
+ /// Add authentication/authorization information to [`Client `](crate::Client )
94
95
///
95
96
/// # Arguments
96
97
///
@@ -100,16 +101,16 @@ impl InfluxDbClient {
100
101
/// # Examples
101
102
///
102
103
/// ```rust
103
- /// use influxdb::client::InfluxDbClient ;
104
+ /// use influxdb::Client ;
104
105
///
105
- /// let _client = InfluxDbClient ::new("http://localhost:9086", "test").with_auth("admin", "password");
106
+ /// let _client = Client ::new("http://localhost:9086", "test").with_auth("admin", "password");
106
107
/// ```
107
108
pub fn with_auth < S1 , S2 > ( mut self , username : S1 , password : S2 ) -> Self
108
109
where
109
110
S1 : ToString ,
110
111
S2 : ToString ,
111
112
{
112
- self . auth = Some ( InfluxDbAuthentication {
113
+ self . auth = Some ( Authentication {
113
114
username : username. to_string ( ) ,
114
115
password : password. to_string ( ) ,
115
116
} ) ;
@@ -129,8 +130,8 @@ impl InfluxDbClient {
129
130
/// Pings the InfluxDB Server
130
131
///
131
132
/// Returns a tuple of build type and version number
132
- pub fn ping ( & self ) -> impl Future < Item = ( String , String ) , Error = InfluxDbError > {
133
- Client :: new ( )
133
+ pub fn ping ( & self ) -> impl Future < Item = ( String , String ) , Error = Error > {
134
+ ReqwestClient :: new ( )
134
135
. get ( format ! ( "{}/ping" , self . url) . as_str ( ) )
135
136
. send ( )
136
137
. map ( |res| {
@@ -149,117 +150,116 @@ impl InfluxDbClient {
149
150
150
151
( String :: from ( build) , String :: from ( version) )
151
152
} )
152
- . map_err ( |err| InfluxDbError :: ProtocolError {
153
+ . map_err ( |err| Error :: ProtocolError {
153
154
error : format ! ( "{}" , err) ,
154
155
} )
155
156
}
156
157
157
- /// Sends a [`InfluxDbReadQuery `](crate::query::read_query::InfluxDbReadQuery ) or [`InfluxDbWriteQuery `](crate::query::write_query::InfluxDbWriteQuery ) to the InfluxDB Server.
158
+ /// Sends a [`ReadQuery `](crate::ReadQuery ) or [`WriteQuery `](crate::WriteQuery ) to the InfluxDB Server.
158
159
///
159
160
/// A version capable of parsing the returned string is available under the [serde_integration](crate::integrations::serde_integration)
160
161
///
161
162
/// # Arguments
162
163
///
163
- /// * `q`: Query of type [`InfluxDbReadQuery `](crate::query::read_query::InfluxDbReadQuery ) or [`InfluxDbWriteQuery `](crate::query::write_query::InfluxDbWriteQuery )
164
+ /// * `q`: Query of type [`ReadQuery `](crate::ReadQuery ) or [`WriteQuery `](crate::WriteQuery )
164
165
///
165
166
/// # Examples
166
167
///
167
168
/// ```rust
168
- /// use influxdb::client::InfluxDbClient;
169
- /// use influxdb::query::{InfluxDbQuery, Timestamp};
169
+ /// use influxdb::{Client, Query, Timestamp};
170
170
///
171
- /// let client = InfluxDbClient ::new("http://localhost:8086", "test");
171
+ /// let client = Client ::new("http://localhost:8086", "test");
172
172
/// let _future = client.query(
173
- /// &InfluxDbQuery ::write_query(Timestamp::NOW, "weather")
173
+ /// &Query ::write_query(Timestamp::NOW, "weather")
174
174
/// .add_field("temperature", 82)
175
175
/// );
176
176
/// ```
177
177
/// # Errors
178
178
///
179
179
/// If the function can not finish the query,
180
- /// a [`InfluxDbError `] variant will be returned.
180
+ /// a [`Error `] variant will be returned.
181
181
///
182
- /// [`InfluxDbError `]: enum.InfluxDbError .html
183
- pub fn query < ' q , Q > ( & self , q : & ' q Q ) -> Box < dyn Future < Item = String , Error = InfluxDbError > >
182
+ /// [`Error `]: enum.Error .html
183
+ pub fn query < ' q , Q > ( & self , q : & ' q Q ) -> Box < dyn Future < Item = String , Error = Error > >
184
184
where
185
- Q : InfluxDbQuery ,
186
- & ' q Q : Into < InfluxDbQueryTypes < ' q > > ,
185
+ Q : Query ,
186
+ & ' q Q : Into < QueryTypes < ' q > > ,
187
187
{
188
188
use futures:: future;
189
189
190
190
let query = match q. build ( ) {
191
191
Err ( err) => {
192
- let error = InfluxDbError :: InvalidQueryError {
192
+ let error = Error :: InvalidQueryError {
193
193
error : format ! ( "{}" , err) ,
194
194
} ;
195
- return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
195
+ return Box :: new ( future:: err :: < String , Error > ( error) ) ;
196
196
}
197
197
Ok ( query) => query,
198
198
} ;
199
199
200
200
let basic_parameters: Vec < ( String , String ) > = self . into ( ) ;
201
201
202
202
let client = match q. into ( ) {
203
- InfluxDbQueryTypes :: Read ( _) => {
203
+ QueryTypes :: Read ( _) => {
204
204
let read_query = query. get ( ) ;
205
205
let mut url = match Url :: parse_with_params (
206
206
format ! ( "{url}/query" , url = self . database_url( ) ) . as_str ( ) ,
207
207
basic_parameters,
208
208
) {
209
209
Ok ( url) => url,
210
210
Err ( err) => {
211
- let error = InfluxDbError :: UrlConstructionError {
211
+ let error = Error :: UrlConstructionError {
212
212
error : format ! ( "{}" , err) ,
213
213
} ;
214
- return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
214
+ return Box :: new ( future:: err :: < String , Error > ( error) ) ;
215
215
}
216
216
} ;
217
217
url. query_pairs_mut ( ) . append_pair ( "q" , & read_query. clone ( ) ) ;
218
218
219
219
if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
220
- Client :: new ( ) . get ( url)
220
+ ReqwestClient :: new ( ) . get ( url)
221
221
} else {
222
- Client :: new ( ) . post ( url)
222
+ ReqwestClient :: new ( ) . post ( url)
223
223
}
224
224
}
225
- InfluxDbQueryTypes :: Write ( write_query) => {
225
+ QueryTypes :: Write ( write_query) => {
226
226
let mut url = match Url :: parse_with_params (
227
227
format ! ( "{url}/write" , url = self . database_url( ) ) . as_str ( ) ,
228
228
basic_parameters,
229
229
) {
230
230
Ok ( url) => url,
231
231
Err ( err) => {
232
- let error = InfluxDbError :: InvalidQueryError {
232
+ let error = Error :: InvalidQueryError {
233
233
error : format ! ( "{}" , err) ,
234
234
} ;
235
- return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
235
+ return Box :: new ( future:: err :: < String , Error > ( error) ) ;
236
236
}
237
237
} ;
238
238
url. query_pairs_mut ( )
239
239
. append_pair ( "precision" , & write_query. get_precision ( ) ) ;
240
- Client :: new ( ) . post ( url) . body ( query. get ( ) )
240
+ ReqwestClient :: new ( ) . post ( url) . body ( query. get ( ) )
241
241
}
242
242
} ;
243
243
Box :: new (
244
244
client
245
245
. send ( )
246
- . map_err ( |err| InfluxDbError :: ConnectionError { error : err } )
246
+ . map_err ( |err| Error :: ConnectionError { error : err } )
247
247
. and_then (
248
- |res| -> future:: FutureResult < reqwest:: r#async:: Response , InfluxDbError > {
248
+ |res| -> future:: FutureResult < reqwest:: r#async:: Response , Error > {
249
249
match res. status ( ) {
250
250
StatusCode :: UNAUTHORIZED => {
251
- futures:: future:: err ( InfluxDbError :: AuthorizationError )
251
+ futures:: future:: err ( Error :: AuthorizationError )
252
252
}
253
253
StatusCode :: FORBIDDEN => {
254
- futures:: future:: err ( InfluxDbError :: AuthenticationError )
254
+ futures:: future:: err ( Error :: AuthenticationError )
255
255
}
256
256
_ => futures:: future:: ok ( res) ,
257
257
}
258
258
} ,
259
259
)
260
260
. and_then ( |mut res| {
261
261
let body = mem:: replace ( res. body_mut ( ) , Decoder :: empty ( ) ) ;
262
- body. concat2 ( ) . map_err ( |err| InfluxDbError :: ProtocolError {
262
+ body. concat2 ( ) . map_err ( |err| Error :: ProtocolError {
263
263
error : format ! ( "{}" , err) ,
264
264
} )
265
265
} )
@@ -269,15 +269,15 @@ impl InfluxDbClient {
269
269
270
270
// todo: improve error parsing without serde
271
271
if s. contains ( "\" error\" " ) {
272
- return futures:: future:: err ( InfluxDbError :: DatabaseError {
272
+ return futures:: future:: err ( Error :: DatabaseError {
273
273
error : format ! ( "influxdb error: \" {}\" " , s) ,
274
274
} ) ;
275
275
}
276
276
277
277
return futures:: future:: ok ( s) ;
278
278
}
279
279
280
- futures:: future:: err ( InfluxDbError :: DeserializationError {
280
+ futures:: future:: err ( Error :: DeserializationError {
281
281
error : "response could not be converted to UTF-8" . to_string ( ) ,
282
282
} )
283
283
} ) ,
@@ -287,17 +287,17 @@ impl InfluxDbClient {
287
287
288
288
#[ cfg( test) ]
289
289
mod tests {
290
- use crate :: client :: InfluxDbClient ;
290
+ use crate :: Client ;
291
291
292
292
#[ test]
293
293
fn test_fn_database ( ) {
294
- let client = InfluxDbClient :: new ( "http://localhost:8068" , "database" ) ;
294
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
295
295
assert_eq ! ( "database" , client. database_name( ) ) ;
296
296
}
297
297
298
298
#[ test]
299
299
fn test_with_auth ( ) {
300
- let client = InfluxDbClient :: new ( "http://localhost:8068" , "database" ) ;
300
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
301
301
assert_eq ! ( client. url, "http://localhost:8068" ) ;
302
302
assert_eq ! ( client. database, "database" ) ;
303
303
assert ! ( client. auth. is_none( ) ) ;
@@ -310,16 +310,16 @@ mod tests {
310
310
311
311
#[ test]
312
312
fn test_into_impl ( ) {
313
- let client = InfluxDbClient :: new ( "http://localhost:8068" , "database" ) ;
313
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
314
314
assert ! ( client. auth. is_none( ) ) ;
315
315
let basic_parameters: Vec < ( String , String ) > = client. into ( ) ;
316
316
assert_eq ! (
317
317
vec![ ( "db" . to_string( ) , "database" . to_string( ) ) ] ,
318
318
basic_parameters
319
319
) ;
320
320
321
- let with_auth = InfluxDbClient :: new ( "http://localhost:8068" , "database" )
322
- . with_auth ( "username" , "password" ) ;
321
+ let with_auth =
322
+ Client :: new ( "http://localhost:8068" , "database" ) . with_auth ( "username" , "password" ) ;
323
323
let basic_parameters_with_auth: Vec < ( String , String ) > = with_auth. into ( ) ;
324
324
assert_eq ! (
325
325
vec![
@@ -330,16 +330,16 @@ mod tests {
330
330
basic_parameters_with_auth
331
331
) ;
332
332
333
- let client = InfluxDbClient :: new ( "http://localhost:8068" , "database" ) ;
333
+ let client = Client :: new ( "http://localhost:8068" , "database" ) ;
334
334
assert ! ( client. auth. is_none( ) ) ;
335
335
let basic_parameters: Vec < ( String , String ) > = ( & client) . into ( ) ;
336
336
assert_eq ! (
337
337
vec![ ( "db" . to_string( ) , "database" . to_string( ) ) ] ,
338
338
basic_parameters
339
339
) ;
340
340
341
- let with_auth = InfluxDbClient :: new ( "http://localhost:8068" , "database" )
342
- . with_auth ( "username" , "password" ) ;
341
+ let with_auth =
342
+ Client :: new ( "http://localhost:8068" , "database" ) . with_auth ( "username" , "password" ) ;
343
343
let basic_parameters_with_auth: Vec < ( String , String ) > = ( & with_auth) . into ( ) ;
344
344
assert_eq ! (
345
345
vec![
0 commit comments