@@ -22,11 +22,7 @@ use reqwest::{StatusCode, Url};
22
22
use std:: mem;
23
23
24
24
use crate :: error:: InfluxDbError ;
25
- use crate :: query:: read_query:: InfluxDbReadQuery ;
26
- use crate :: query:: write_query:: InfluxDbWriteQuery ;
27
- use crate :: query:: InfluxDbQuery ;
28
-
29
- use std:: any:: Any ;
25
+ use crate :: query:: { InfluxDbQuery , InfluxDbQueryTypes } ;
30
26
31
27
#[ derive( Clone , Debug ) ]
32
28
/// Internal Authentication representation
@@ -184,9 +180,10 @@ impl InfluxDbClient {
184
180
/// a [`InfluxDbError`] variant will be returned.
185
181
///
186
182
/// [`InfluxDbError`]: enum.InfluxDbError.html
187
- pub fn query < Q > ( & self , q : & Q ) -> Box < dyn Future < Item = String , Error = InfluxDbError > >
183
+ pub fn query < ' q , Q > ( & self , q : & ' q Q ) -> Box < dyn Future < Item = String , Error = InfluxDbError > >
188
184
where
189
- Q : Any + InfluxDbQuery ,
185
+ Q : InfluxDbQuery ,
186
+ & ' q Q : Into < InfluxDbQueryTypes < ' q > > ,
190
187
{
191
188
use futures:: future;
192
189
@@ -200,49 +197,48 @@ impl InfluxDbClient {
200
197
Ok ( query) => query,
201
198
} ;
202
199
203
- let any_value = q as & dyn Any ;
204
200
let basic_parameters: Vec < ( String , String ) > = self . into ( ) ;
205
201
206
- let client = if let Some ( _) = any_value. downcast_ref :: < InfluxDbReadQuery > ( ) {
207
- let read_query = query. get ( ) ;
202
+ let client = match q. into ( ) {
203
+ InfluxDbQueryTypes :: Read ( _) => {
204
+ let read_query = query. get ( ) ;
205
+ let mut url = match Url :: parse_with_params (
206
+ format ! ( "{url}/query" , url = self . database_url( ) ) . as_str ( ) ,
207
+ basic_parameters,
208
+ ) {
209
+ Ok ( url) => url,
210
+ Err ( err) => {
211
+ let error = InfluxDbError :: UrlConstructionError {
212
+ error : format ! ( "{}" , err) ,
213
+ } ;
214
+ return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
215
+ }
216
+ } ;
217
+ url. query_pairs_mut ( ) . append_pair ( "q" , & read_query. clone ( ) ) ;
208
218
209
- let mut url = match Url :: parse_with_params (
210
- format ! ( "{url}/query" , url = self . database_url( ) ) . as_str ( ) ,
211
- basic_parameters,
212
- ) {
213
- Ok ( url) => url,
214
- Err ( err) => {
215
- let error = InfluxDbError :: UrlConstructionError {
216
- error : format ! ( "{}" , err) ,
217
- } ;
218
- return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
219
+ if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
220
+ Client :: new ( ) . get ( url)
221
+ } else {
222
+ Client :: new ( ) . post ( url)
219
223
}
220
- } ;
221
- url. query_pairs_mut ( ) . append_pair ( "q" , & read_query. clone ( ) ) ;
222
-
223
- if read_query. contains ( "SELECT" ) || read_query. contains ( "SHOW" ) {
224
- Client :: new ( ) . get ( url)
225
- } else {
226
- Client :: new ( ) . post ( url)
227
224
}
228
- } else if let Some ( write_query) = any_value. downcast_ref :: < InfluxDbWriteQuery > ( ) {
229
- let mut url = match Url :: parse_with_params (
230
- format ! ( "{url}/write" , url = self . database_url( ) ) . as_str ( ) ,
231
- basic_parameters,
232
- ) {
233
- Ok ( url) => url,
234
- Err ( err) => {
235
- let error = InfluxDbError :: InvalidQueryError {
236
- error : format ! ( "{}" , err) ,
237
- } ;
238
- return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
239
- }
240
- } ;
241
- url. query_pairs_mut ( )
242
- . append_pair ( "precision" , & write_query. get_precision ( ) ) ;
243
- Client :: new ( ) . post ( url) . body ( query. get ( ) )
244
- } else {
245
- unreachable ! ( )
225
+ InfluxDbQueryTypes :: Write ( write_query) => {
226
+ let mut url = match Url :: parse_with_params (
227
+ format ! ( "{url}/write" , url = self . database_url( ) ) . as_str ( ) ,
228
+ basic_parameters,
229
+ ) {
230
+ Ok ( url) => url,
231
+ Err ( err) => {
232
+ let error = InfluxDbError :: InvalidQueryError {
233
+ error : format ! ( "{}" , err) ,
234
+ } ;
235
+ return Box :: new ( future:: err :: < String , InfluxDbError > ( error) ) ;
236
+ }
237
+ } ;
238
+ url. query_pairs_mut ( )
239
+ . append_pair ( "precision" , & write_query. get_precision ( ) ) ;
240
+ Client :: new ( ) . post ( url) . body ( query. get ( ) )
241
+ }
246
242
} ;
247
243
Box :: new (
248
244
client
0 commit comments