1
+ //! Client which can read and write data from InfluxDB.
2
+ //!
3
+ //! # Arguments
4
+ //!
5
+ //! * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
6
+ //! * `database`: The Database against which queries and writes will be run.
7
+ //!
8
+ //! # Examples
9
+ //!
10
+ //! ```rust
11
+ //! use influxdb::client::InfluxDbClient;
12
+ //!
13
+ //! let client = InfluxDbClient::new("http://localhost:8086", "test");
14
+ //!
15
+ //! assert_eq!(client.database_name(), "test");
16
+ //! ```
17
+
1
18
use futures:: { Future , Stream } ;
2
19
use reqwest:: r#async:: { Client , Decoder } ;
3
20
@@ -6,30 +23,15 @@ use std::mem;
6
23
use crate :: error:: InfluxDbError ;
7
24
use crate :: query:: { InfluxDbQuery , QueryType } ;
8
25
9
- /// Client which can read and write data from InfluxDB.
10
- ///
11
- /// # Arguments
12
- ///
13
- /// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
14
- /// * `database`: The Database against which queries and writes will be run.
15
- ///
16
- /// # Examples
17
- ///
18
- /// ```rust
19
- /// use influxdb::client::InfluxDbClient;
20
- ///
21
- /// let client = InfluxDbClient::new("http://localhost:8086", "test");
22
- ///
23
- /// assert_eq!(client.database_name(), "test");
24
- /// ```
26
+ /// Internal Representation of a Client
25
27
pub struct InfluxDbClient {
26
28
url : String ,
27
29
database : String ,
28
30
// auth: Option<InfluxDbAuthentication>
29
31
}
30
32
31
33
impl InfluxDbClient {
32
- /// Instantiates a new [`InfluxDbClient`]
34
+ /// Instantiates a new [`InfluxDbClient`](crate::client::InfluxDbClient)
33
35
///
34
36
/// # Arguments
35
37
///
@@ -45,23 +47,28 @@ impl InfluxDbClient {
45
47
/// ```
46
48
pub fn new < S1 , S2 > ( url : S1 , database : S2 ) -> Self
47
49
where
48
- S1 : Into < String > ,
49
- S2 : Into < String > ,
50
+ S1 : ToString ,
51
+ S2 : ToString ,
50
52
{
51
53
InfluxDbClient {
52
- url : url. into ( ) ,
53
- database : database. into ( ) ,
54
+ url : url. to_string ( ) ,
55
+ database : database. to_string ( ) ,
54
56
}
55
57
}
56
58
57
- pub fn database_name < ' a > ( & ' a self ) -> & ' a str {
59
+ /// Returns the name of the database the client is using
60
+ pub fn database_name ( & self ) -> & str {
58
61
& self . database
59
62
}
60
63
61
- pub fn database_url < ' a > ( & ' a self ) -> & ' a str {
64
+ /// Returns the URL of the InfluxDB installation the client is using
65
+ pub fn database_url ( & self ) -> & str {
62
66
& self . url
63
67
}
64
68
69
+ /// Pings the InfluxDB Server
70
+ ///
71
+ /// Returns a tuple of build type and version number
65
72
pub fn ping ( & self ) -> impl Future < Item = ( String , String ) , Error = InfluxDbError > {
66
73
Client :: new ( )
67
74
. get ( format ! ( "{}/ping" , self . url) . as_str ( ) )
@@ -82,11 +89,31 @@ impl InfluxDbClient {
82
89
83
90
( String :: from ( build) , String :: from ( version) )
84
91
} )
85
- . map_err ( |err| InfluxDbError :: UnspecifiedError {
92
+ . map_err ( |err| InfluxDbError :: ProtocolError {
86
93
error : format ! ( "{}" , err) ,
87
94
} )
88
95
}
89
96
97
+ /// Sends a [`InfluxDbReadQuery`](crate::query::read_query::InfluxDbReadQuery) or [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) to the InfluxDB Server.InfluxDbError
98
+ ///
99
+ /// A version capable of parsing the returned string is available under the [serde_integration](crate::integrations::serde_integration)
100
+ ///
101
+ /// # Arguments
102
+ ///
103
+ /// * `q`: Query of type [`InfluxDbReadQuery`](crate::query::read_query::InfluxDbReadQuery) or [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery)
104
+ ///
105
+ /// # Examples
106
+ ///
107
+ /// ```rust
108
+ /// use influxdb::client::InfluxDbClient;
109
+ /// use influxdb::query::InfluxDbQuery;
110
+ ///
111
+ /// let client = InfluxDbClient::new("http://localhost:8086", "test");
112
+ /// let _future = client.query(
113
+ /// InfluxDbQuery::write_query("weather")
114
+ /// .add_field("temperature", 82)
115
+ /// );
116
+ /// ```
90
117
pub fn query < Q > ( & self , q : Q ) -> Box < dyn Future < Item = String , Error = InfluxDbError > >
91
118
where
92
119
Q : InfluxDbQuery ,
@@ -101,7 +128,7 @@ impl InfluxDbClient {
101
128
102
129
let query = match q. build ( ) {
103
130
Err ( err) => {
104
- let error = InfluxDbError :: UnspecifiedError {
131
+ let error = InfluxDbError :: InvalidQueryError {
105
132
error : format ! ( "{}" , err) ,
106
133
} ;
107
134
return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
@@ -146,7 +173,7 @@ impl InfluxDbClient {
146
173
let body = mem:: replace ( res. body_mut ( ) , Decoder :: empty ( ) ) ;
147
174
body. concat2 ( )
148
175
} )
149
- . map_err ( |err| InfluxDbError :: UnspecifiedError {
176
+ . map_err ( |err| InfluxDbError :: ProtocolError {
150
177
error : format ! ( "{}" , err) ,
151
178
} )
152
179
. and_then ( |body| {
@@ -155,16 +182,16 @@ impl InfluxDbClient {
155
182
156
183
// todo: improve error parsing without serde
157
184
if s. contains ( "\" error\" " ) {
158
- return futures:: future:: err ( InfluxDbError :: UnspecifiedError {
185
+ return futures:: future:: err ( InfluxDbError :: DatabaseError {
159
186
error : format ! ( "influxdb error: \" {}\" " , s) ,
160
187
} ) ;
161
188
}
162
189
163
190
return futures:: future:: ok ( s) ;
164
191
}
165
192
166
- futures:: future:: err ( InfluxDbError :: UnspecifiedError {
167
- error : "some other error has happened here! " . to_string ( ) ,
193
+ futures:: future:: err ( InfluxDbError :: DeserializationError {
194
+ error : "response could not be converted to UTF-8 " . to_string ( ) ,
168
195
} )
169
196
} ) ,
170
197
)
0 commit comments