2525//! let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
2626//! let client = InfluxDbClient::new("http://localhost:8086", "test");
2727//! let query = InfluxDbQuery::raw_read_query("SELECT temperature FROM /weather_[a-z]*$/ WHERE time > now() - 1m ORDER BY DESC");
28- //! let _result = rt.block_on(client.json_query::<WeatherWithoutCityName, _ >(query))
28+ //! let _result = rt.block_on(client.json_query::<WeatherWithoutCityName>(query))
2929//! .map(|it| {
3030//! it.map(|series_vec| {
3131//! series_vec
3636//! }).collect::<Vec<Weather>>()
3737//! })
3838//! });
39+ //! ```
3940
4041use crate :: client:: InfluxDbClient ;
4142
@@ -49,7 +50,11 @@ use serde::Deserialize;
4950use serde_json;
5051
5152use crate :: error:: InfluxDbError ;
52- use crate :: query:: { InfluxDbQuery , QueryType } ;
53+
54+ use crate :: query:: read_query:: InfluxDbReadQuery ;
55+ use crate :: query:: InfluxDbQuery ;
56+
57+ use url:: form_urlencoded;
5358
5459#[ derive( Deserialize ) ]
5560#[ doc( hidden) ]
@@ -77,55 +82,41 @@ pub struct InfluxDbSeries<T> {
7782}
7883
7984impl InfluxDbClient {
80- pub fn json_query < T : ' static , Q > (
85+ pub fn json_query < T : ' static > (
8186 & self ,
82- q : Q ,
87+ q : InfluxDbReadQuery ,
8388 ) -> Box < dyn Future < Item = Option < Vec < InfluxDbSeries < T > > > , Error = InfluxDbError > >
8489 where
85- Q : InfluxDbQuery ,
8690 T : DeserializeOwned ,
8791 {
8892 use futures:: future;
8993
90- let q_type = q. get_type ( ) ;
91- let query = match q. build ( ) {
92- Err ( err) => {
94+ let query = q. build ( ) . unwrap ( ) ;
95+
96+ let client = {
97+ let read_query = query. get ( ) ;
98+ let encoded: String = form_urlencoded:: Serializer :: new ( String :: new ( ) )
99+ . append_pair ( "db" , self . database_name ( ) )
100+ . append_pair ( "q" , & read_query)
101+ . finish ( ) ;
102+ let http_query_string = format ! (
103+ "{url}/query?{encoded}" ,
104+ url = self . database_url( ) ,
105+ encoded = encoded
106+ ) ;
107+
108+ if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
109+ Client :: new ( ) . get ( http_query_string. as_str ( ) )
110+ } else {
93111 let error = InfluxDbError :: InvalidQueryError {
94- error : format ! ( "{}" , err) ,
112+ error : String :: from (
113+ "Only SELECT and SHOW queries supported with JSON deserialization" ,
114+ ) ,
95115 } ;
96116 return Box :: new (
97117 future:: err :: < Option < Vec < InfluxDbSeries < T > > > , InfluxDbError > ( error) ,
98118 ) ;
99119 }
100- Ok ( query) => query,
101- } ;
102-
103- let client = match q_type {
104- QueryType :: ReadQuery => {
105- let read_query = query. get ( ) ;
106- let http_query_string = format ! (
107- "{url}/query?db={db}&q={read_query}" ,
108- url = self . database_url( ) ,
109- db = self . database_name( ) ,
110- read_query = read_query,
111- ) ;
112-
113- if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
114- Client :: new ( ) . get ( http_query_string. as_str ( ) )
115- } else {
116- Client :: new ( ) . post ( http_query_string. as_str ( ) )
117- }
118- }
119- QueryType :: WriteQuery => Client :: new ( )
120- . post (
121- format ! (
122- "{url}/write?db={db}" ,
123- url = self . database_url( ) ,
124- db = self . database_name( ) ,
125- )
126- . as_str ( ) ,
127- )
128- . body ( query. get ( ) ) ,
129120 } ;
130121
131122 Box :: new (
0 commit comments