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+
118use futures:: { Future , Stream } ;
219use reqwest:: r#async:: { Client , Decoder } ;
320
@@ -6,30 +23,15 @@ use std::mem;
623use crate :: error:: InfluxDbError ;
724use crate :: query:: { InfluxDbQuery , QueryType } ;
825
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
2527pub struct InfluxDbClient {
2628 url : String ,
2729 database : String ,
2830 // auth: Option<InfluxDbAuthentication>
2931}
3032
3133impl InfluxDbClient {
32- /// Instantiates a new [`InfluxDbClient`]
34+ /// Instantiates a new [`InfluxDbClient`](crate::client::InfluxDbClient)
3335 ///
3436 /// # Arguments
3537 ///
@@ -45,23 +47,28 @@ impl InfluxDbClient {
4547 /// ```
4648 pub fn new < S1 , S2 > ( url : S1 , database : S2 ) -> Self
4749 where
48- S1 : Into < String > ,
49- S2 : Into < String > ,
50+ S1 : ToString ,
51+ S2 : ToString ,
5052 {
5153 InfluxDbClient {
52- url : url. into ( ) ,
53- database : database. into ( ) ,
54+ url : url. to_string ( ) ,
55+ database : database. to_string ( ) ,
5456 }
5557 }
5658
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 {
5861 & self . database
5962 }
6063
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 {
6266 & self . url
6367 }
6468
69+ /// Pings the InfluxDB Server
70+ ///
71+ /// Returns a tuple of build type and version number
6572 pub fn ping ( & self ) -> impl Future < Item = ( String , String ) , Error = InfluxDbError > {
6673 Client :: new ( )
6774 . get ( format ! ( "{}/ping" , self . url) . as_str ( ) )
@@ -82,11 +89,31 @@ impl InfluxDbClient {
8289
8390 ( String :: from ( build) , String :: from ( version) )
8491 } )
85- . map_err ( |err| InfluxDbError :: UnspecifiedError {
92+ . map_err ( |err| InfluxDbError :: ProtocolError {
8693 error : format ! ( "{}" , err) ,
8794 } )
8895 }
8996
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+ /// ```
90117 pub fn query < Q > ( & self , q : Q ) -> Box < dyn Future < Item = String , Error = InfluxDbError > >
91118 where
92119 Q : InfluxDbQuery ,
@@ -101,7 +128,7 @@ impl InfluxDbClient {
101128
102129 let query = match q. build ( ) {
103130 Err ( err) => {
104- let error = InfluxDbError :: UnspecifiedError {
131+ let error = InfluxDbError :: InvalidQueryError {
105132 error : format ! ( "{}" , err) ,
106133 } ;
107134 return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
@@ -146,7 +173,7 @@ impl InfluxDbClient {
146173 let body = mem:: replace ( res. body_mut ( ) , Decoder :: empty ( ) ) ;
147174 body. concat2 ( )
148175 } )
149- . map_err ( |err| InfluxDbError :: UnspecifiedError {
176+ . map_err ( |err| InfluxDbError :: ProtocolError {
150177 error : format ! ( "{}" , err) ,
151178 } )
152179 . and_then ( |body| {
@@ -155,16 +182,16 @@ impl InfluxDbClient {
155182
156183 // todo: improve error parsing without serde
157184 if s. contains ( "\" error\" " ) {
158- return futures:: future:: err ( InfluxDbError :: UnspecifiedError {
185+ return futures:: future:: err ( InfluxDbError :: DatabaseError {
159186 error : format ! ( "influxdb error: \" {}\" " , s) ,
160187 } ) ;
161188 }
162189
163190 return futures:: future:: ok ( s) ;
164191 }
165192
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 ( ) ,
168195 } )
169196 } ) ,
170197 )
0 commit comments