Skip to content

Commit 4f1d4f6

Browse files
authored
Prefix InfluxDb of type names are redundant (#27)
1 parent 75429fd commit 4f1d4f6

File tree

9 files changed

+261
-276
lines changed

9 files changed

+261
-276
lines changed

README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Pull requests are always welcome. See [Contributing](./CONTRIBUTING.md) and [Cod
4343
## Planned Features
4444

4545
- Read Query Builder instead of supplying raw queries
46-
- `#[derive(InfluxDbWritable)]`
46+
- `#[derive(InfluxDbReadable)]` and `#[derive(InfluxDbWriteable)]` proc macros
4747

4848
## Quickstart
4949

@@ -56,18 +56,16 @@ influxdb = "0.0.4"
5656
For an example with using Serde deserialization, please refer to [serde_integration](crate::integrations::serde_integration)
5757

5858
```rust
59-
use influxdb::query::{InfluxDbQuery, Timestamp};
60-
use influxdb::client::InfluxDbClient;
59+
use influxdb::{Client, Query, Timestamp};
6160
use tokio::runtime::current_thread::Runtime;
6261

63-
// Create a InfluxDbClient with URL `http://localhost:8086`
62+
// Create a Client with URL `http://localhost:8086`
6463
// and database name `test`
65-
let client = InfluxDbClient::new("http://localhost:8086", "test");
64+
let client = Client::new("http://localhost:8086", "test");
6665

67-
// Let's write something to InfluxDB. First we're creating a
68-
// InfluxDbWriteQuery to write some data.
66+
// Let's write something to InfluxDB. First we're creating a `WriteQuery` to write some data.
6967
// This creates a query which writes a new measurement into a series called `weather`
70-
let write_query = InfluxDbQuery::write_query(Timestamp::NOW, "weather")
68+
let write_query = Query::write_query(Timestamp::NOW, "weather")
7169
.add_field("temperature", 82);
7270

7371
// Since this library is async by default, we're going to need a Runtime,
@@ -81,7 +79,7 @@ let write_result = rt.block_on(client.query(&write_query));
8179
assert!(write_result.is_ok(), "Write result was not okay");
8280

8381
// Reading data is as simple as writing. First we need to create a query
84-
let read_query = InfluxDbQuery::raw_read_query("SELECT _ FROM weather");
82+
let read_query = Query::raw_read_query("SELECT _ FROM weather");
8583

8684
// Again, we're blocking until the request is done
8785
let read_result = rt.block_on(client.query(&read_query));

src/client/mod.rs

+60-60
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,39 @@
88
//! # Examples
99
//!
1010
//! ```rust
11-
//! use influxdb::client::InfluxDbClient;
11+
//! use influxdb::Client;
1212
//!
13-
//! let client = InfluxDbClient::new("http://localhost:8086", "test");
13+
//! let client = Client::new("http://localhost:8086", "test");
1414
//!
1515
//! assert_eq!(client.database_name(), "test");
1616
//! ```
1717
1818
use futures::{Future, Stream};
19-
use reqwest::r#async::{Client, Decoder};
19+
use reqwest::r#async::{Client as ReqwestClient, Decoder};
2020
use reqwest::{StatusCode, Url};
2121

2222
use std::mem;
2323

24-
use crate::error::InfluxDbError;
25-
use crate::query::{InfluxDbQuery, InfluxDbQueryTypes};
24+
use crate::query::QueryTypes;
25+
use crate::Error;
26+
use crate::Query;
2627

2728
#[derive(Clone, Debug)]
2829
/// Internal Authentication representation
29-
pub(crate) struct InfluxDbAuthentication {
30+
pub(crate) struct Authentication {
3031
pub username: String,
3132
pub password: String,
3233
}
3334

3435
#[derive(Clone, Debug)]
3536
/// Internal Representation of a Client
36-
pub struct InfluxDbClient {
37+
pub struct Client {
3738
url: String,
3839
database: String,
39-
auth: Option<InfluxDbAuthentication>,
40+
auth: Option<Authentication>,
4041
}
4142

42-
impl Into<Vec<(String, String)>> for InfluxDbClient {
43+
impl Into<Vec<(String, String)>> for Client {
4344
fn into(self) -> Vec<(String, String)> {
4445
let mut vec: Vec<(String, String)> = Vec::new();
4546
vec.push(("db".to_string(), self.database));
@@ -51,7 +52,7 @@ impl Into<Vec<(String, String)>> for InfluxDbClient {
5152
}
5253
}
5354

54-
impl<'a> Into<Vec<(String, String)>> for &'a InfluxDbClient {
55+
impl<'a> Into<Vec<(String, String)>> for &'a Client {
5556
fn into(self) -> Vec<(String, String)> {
5657
let mut vec: Vec<(String, String)> = Vec::new();
5758
vec.push(("db".to_string(), self.database.to_owned()));
@@ -63,8 +64,8 @@ impl<'a> Into<Vec<(String, String)>> for &'a InfluxDbClient {
6364
}
6465
}
6566

66-
impl InfluxDbClient {
67-
/// Instantiates a new [`InfluxDbClient`](crate::client::InfluxDbClient)
67+
impl Client {
68+
/// Instantiates a new [`Client`](crate::Client)
6869
///
6970
/// # Arguments
7071
///
@@ -74,23 +75,23 @@ impl InfluxDbClient {
7475
/// # Examples
7576
///
7677
/// ```rust
77-
/// use influxdb::client::InfluxDbClient;
78+
/// use influxdb::Client;
7879
///
79-
/// let _client = InfluxDbClient::new("http://localhost:8086", "test");
80+
/// let _client = Client::new("http://localhost:8086", "test");
8081
/// ```
8182
pub fn new<S1, S2>(url: S1, database: S2) -> Self
8283
where
8384
S1: ToString,
8485
S2: ToString,
8586
{
86-
InfluxDbClient {
87+
Client {
8788
url: url.to_string(),
8889
database: database.to_string(),
8990
auth: None,
9091
}
9192
}
9293

93-
/// Add authentication/authorization information to [`InfluxDbClient`](crate::client::InfluxDbClient)
94+
/// Add authentication/authorization information to [`Client`](crate::Client)
9495
///
9596
/// # Arguments
9697
///
@@ -100,16 +101,16 @@ impl InfluxDbClient {
100101
/// # Examples
101102
///
102103
/// ```rust
103-
/// use influxdb::client::InfluxDbClient;
104+
/// use influxdb::Client;
104105
///
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");
106107
/// ```
107108
pub fn with_auth<S1, S2>(mut self, username: S1, password: S2) -> Self
108109
where
109110
S1: ToString,
110111
S2: ToString,
111112
{
112-
self.auth = Some(InfluxDbAuthentication {
113+
self.auth = Some(Authentication {
113114
username: username.to_string(),
114115
password: password.to_string(),
115116
});
@@ -129,8 +130,8 @@ impl InfluxDbClient {
129130
/// Pings the InfluxDB Server
130131
///
131132
/// 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()
134135
.get(format!("{}/ping", self.url).as_str())
135136
.send()
136137
.map(|res| {
@@ -149,117 +150,116 @@ impl InfluxDbClient {
149150

150151
(String::from(build), String::from(version))
151152
})
152-
.map_err(|err| InfluxDbError::ProtocolError {
153+
.map_err(|err| Error::ProtocolError {
153154
error: format!("{}", err),
154155
})
155156
}
156157

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.
158159
///
159160
/// A version capable of parsing the returned string is available under the [serde_integration](crate::integrations::serde_integration)
160161
///
161162
/// # Arguments
162163
///
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)
164165
///
165166
/// # Examples
166167
///
167168
/// ```rust
168-
/// use influxdb::client::InfluxDbClient;
169-
/// use influxdb::query::{InfluxDbQuery, Timestamp};
169+
/// use influxdb::{Client, Query, Timestamp};
170170
///
171-
/// let client = InfluxDbClient::new("http://localhost:8086", "test");
171+
/// let client = Client::new("http://localhost:8086", "test");
172172
/// let _future = client.query(
173-
/// &InfluxDbQuery::write_query(Timestamp::NOW, "weather")
173+
/// &Query::write_query(Timestamp::NOW, "weather")
174174
/// .add_field("temperature", 82)
175175
/// );
176176
/// ```
177177
/// # Errors
178178
///
179179
/// If the function can not finish the query,
180-
/// a [`InfluxDbError`] variant will be returned.
180+
/// a [`Error`] variant will be returned.
181181
///
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>>
184184
where
185-
Q: InfluxDbQuery,
186-
&'q Q: Into<InfluxDbQueryTypes<'q>>,
185+
Q: Query,
186+
&'q Q: Into<QueryTypes<'q>>,
187187
{
188188
use futures::future;
189189

190190
let query = match q.build() {
191191
Err(err) => {
192-
let error = InfluxDbError::InvalidQueryError {
192+
let error = Error::InvalidQueryError {
193193
error: format!("{}", err),
194194
};
195-
return Box::new(future::err::<String, InfluxDbError>(error));
195+
return Box::new(future::err::<String, Error>(error));
196196
}
197197
Ok(query) => query,
198198
};
199199

200200
let basic_parameters: Vec<(String, String)> = self.into();
201201

202202
let client = match q.into() {
203-
InfluxDbQueryTypes::Read(_) => {
203+
QueryTypes::Read(_) => {
204204
let read_query = query.get();
205205
let mut url = match Url::parse_with_params(
206206
format!("{url}/query", url = self.database_url()).as_str(),
207207
basic_parameters,
208208
) {
209209
Ok(url) => url,
210210
Err(err) => {
211-
let error = InfluxDbError::UrlConstructionError {
211+
let error = Error::UrlConstructionError {
212212
error: format!("{}", err),
213213
};
214-
return Box::new(future::err::<String, InfluxDbError>(error));
214+
return Box::new(future::err::<String, Error>(error));
215215
}
216216
};
217217
url.query_pairs_mut().append_pair("q", &read_query.clone());
218218

219219
if read_query.contains("SELECT") || read_query.contains("SHOW") {
220-
Client::new().get(url)
220+
ReqwestClient::new().get(url)
221221
} else {
222-
Client::new().post(url)
222+
ReqwestClient::new().post(url)
223223
}
224224
}
225-
InfluxDbQueryTypes::Write(write_query) => {
225+
QueryTypes::Write(write_query) => {
226226
let mut url = match Url::parse_with_params(
227227
format!("{url}/write", url = self.database_url()).as_str(),
228228
basic_parameters,
229229
) {
230230
Ok(url) => url,
231231
Err(err) => {
232-
let error = InfluxDbError::InvalidQueryError {
232+
let error = Error::InvalidQueryError {
233233
error: format!("{}", err),
234234
};
235-
return Box::new(future::err::<String, InfluxDbError>(error));
235+
return Box::new(future::err::<String, Error>(error));
236236
}
237237
};
238238
url.query_pairs_mut()
239239
.append_pair("precision", &write_query.get_precision());
240-
Client::new().post(url).body(query.get())
240+
ReqwestClient::new().post(url).body(query.get())
241241
}
242242
};
243243
Box::new(
244244
client
245245
.send()
246-
.map_err(|err| InfluxDbError::ConnectionError { error: err })
246+
.map_err(|err| Error::ConnectionError { error: err })
247247
.and_then(
248-
|res| -> future::FutureResult<reqwest::r#async::Response, InfluxDbError> {
248+
|res| -> future::FutureResult<reqwest::r#async::Response, Error> {
249249
match res.status() {
250250
StatusCode::UNAUTHORIZED => {
251-
futures::future::err(InfluxDbError::AuthorizationError)
251+
futures::future::err(Error::AuthorizationError)
252252
}
253253
StatusCode::FORBIDDEN => {
254-
futures::future::err(InfluxDbError::AuthenticationError)
254+
futures::future::err(Error::AuthenticationError)
255255
}
256256
_ => futures::future::ok(res),
257257
}
258258
},
259259
)
260260
.and_then(|mut res| {
261261
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 {
263263
error: format!("{}", err),
264264
})
265265
})
@@ -269,15 +269,15 @@ impl InfluxDbClient {
269269

270270
// todo: improve error parsing without serde
271271
if s.contains("\"error\"") {
272-
return futures::future::err(InfluxDbError::DatabaseError {
272+
return futures::future::err(Error::DatabaseError {
273273
error: format!("influxdb error: \"{}\"", s),
274274
});
275275
}
276276

277277
return futures::future::ok(s);
278278
}
279279

280-
futures::future::err(InfluxDbError::DeserializationError {
280+
futures::future::err(Error::DeserializationError {
281281
error: "response could not be converted to UTF-8".to_string(),
282282
})
283283
}),
@@ -287,17 +287,17 @@ impl InfluxDbClient {
287287

288288
#[cfg(test)]
289289
mod tests {
290-
use crate::client::InfluxDbClient;
290+
use crate::Client;
291291

292292
#[test]
293293
fn test_fn_database() {
294-
let client = InfluxDbClient::new("http://localhost:8068", "database");
294+
let client = Client::new("http://localhost:8068", "database");
295295
assert_eq!("database", client.database_name());
296296
}
297297

298298
#[test]
299299
fn test_with_auth() {
300-
let client = InfluxDbClient::new("http://localhost:8068", "database");
300+
let client = Client::new("http://localhost:8068", "database");
301301
assert_eq!(client.url, "http://localhost:8068");
302302
assert_eq!(client.database, "database");
303303
assert!(client.auth.is_none());
@@ -310,16 +310,16 @@ mod tests {
310310

311311
#[test]
312312
fn test_into_impl() {
313-
let client = InfluxDbClient::new("http://localhost:8068", "database");
313+
let client = Client::new("http://localhost:8068", "database");
314314
assert!(client.auth.is_none());
315315
let basic_parameters: Vec<(String, String)> = client.into();
316316
assert_eq!(
317317
vec![("db".to_string(), "database".to_string())],
318318
basic_parameters
319319
);
320320

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");
323323
let basic_parameters_with_auth: Vec<(String, String)> = with_auth.into();
324324
assert_eq!(
325325
vec![
@@ -330,16 +330,16 @@ mod tests {
330330
basic_parameters_with_auth
331331
);
332332

333-
let client = InfluxDbClient::new("http://localhost:8068", "database");
333+
let client = Client::new("http://localhost:8068", "database");
334334
assert!(client.auth.is_none());
335335
let basic_parameters: Vec<(String, String)> = (&client).into();
336336
assert_eq!(
337337
vec![("db".to_string(), "database".to_string())],
338338
basic_parameters
339339
);
340340

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");
343343
let basic_parameters_with_auth: Vec<(String, String)> = (&with_auth).into();
344344
assert_eq!(
345345
vec![

0 commit comments

Comments
 (0)