|
| 1 | +use futures::{Future, Stream}; |
| 2 | +use reqwest::r#async::{Client, Decoder}; |
| 3 | + |
| 4 | +use std::mem; |
| 5 | + |
| 6 | +use crate::error::InfluxDbError; |
| 7 | +use crate::query::{InfluxDbQuery, QueryType}; |
| 8 | + |
| 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 | +/// ``` |
| 25 | +pub struct InfluxDbClient { |
| 26 | + url: String, |
| 27 | + database: String, |
| 28 | + // auth: Option<InfluxDbAuthentication> |
| 29 | +} |
| 30 | + |
| 31 | +impl InfluxDbClient { |
| 32 | + /// Instantiates a new [`InfluxDbClient`] |
| 33 | + /// |
| 34 | + /// # Arguments |
| 35 | + /// |
| 36 | + /// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`). |
| 37 | + /// * `database`: The Database against which queries and writes will be run. |
| 38 | + /// |
| 39 | + /// # Examples |
| 40 | + /// |
| 41 | + /// ```rust |
| 42 | + /// use influxdb::client::InfluxDbClient; |
| 43 | + /// |
| 44 | + /// let _client = InfluxDbClient::new("http://localhost:8086", "test"); |
| 45 | + /// ``` |
| 46 | + pub fn new<S>(url: S, database: S) -> Self |
| 47 | + where |
| 48 | + S: Into<String>, |
| 49 | + { |
| 50 | + InfluxDbClient { |
| 51 | + url: url.into(), |
| 52 | + database: database.into(), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn database_name<'a>(&'a self) -> &'a str { |
| 57 | + &self.database |
| 58 | + } |
| 59 | + |
| 60 | + pub fn database_url<'a>(&'a self) -> &'a str { |
| 61 | + &self.url |
| 62 | + } |
| 63 | + |
| 64 | + pub fn ping(&self) -> impl Future<Item = (String, String), Error = InfluxDbError> { |
| 65 | + Client::new() |
| 66 | + .get(format!("{}/ping", self.url).as_str()) |
| 67 | + .send() |
| 68 | + .map(|res| { |
| 69 | + let build = res |
| 70 | + .headers() |
| 71 | + .get("X-Influxdb-Build") |
| 72 | + .unwrap() |
| 73 | + .to_str() |
| 74 | + .unwrap(); |
| 75 | + let version = res |
| 76 | + .headers() |
| 77 | + .get("X-Influxdb-Version") |
| 78 | + .unwrap() |
| 79 | + .to_str() |
| 80 | + .unwrap(); |
| 81 | + |
| 82 | + (String::from(build), String::from(version)) |
| 83 | + }) |
| 84 | + .map_err(|err| InfluxDbError::UnspecifiedError { |
| 85 | + error: format!("{}", err), |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + pub fn query<Q>(self, q: Q) -> Box<dyn Future<Item = String, Error = InfluxDbError>> |
| 90 | + where |
| 91 | + Q: InfluxDbQuery, |
| 92 | + { |
| 93 | + use futures::future; |
| 94 | + |
| 95 | + let query_type = q.get_type(); |
| 96 | + let endpoint = match query_type { |
| 97 | + QueryType::ReadQuery => "query", |
| 98 | + QueryType::WriteQuery => "write", |
| 99 | + }; |
| 100 | + |
| 101 | + let query = match q.build() { |
| 102 | + Err(err) => { |
| 103 | + let error = InfluxDbError::UnspecifiedError { |
| 104 | + error: format!("{}", err), |
| 105 | + }; |
| 106 | + return Box::new(future::err::<String, InfluxDbError>(error)); |
| 107 | + } |
| 108 | + Ok(query) => query, |
| 109 | + }; |
| 110 | + |
| 111 | + let query_str = query.get(); |
| 112 | + let url_params = match query_type { |
| 113 | + QueryType::ReadQuery => format!("&q={}", query_str), |
| 114 | + QueryType::WriteQuery => String::from(""), |
| 115 | + }; |
| 116 | + |
| 117 | + let client = match query_type { |
| 118 | + QueryType::ReadQuery => Client::new().get( |
| 119 | + format!( |
| 120 | + "{url}/{endpoint}?db={db}{url_params}", |
| 121 | + url = self.url, |
| 122 | + endpoint = endpoint, |
| 123 | + db = self.database, |
| 124 | + url_params = url_params |
| 125 | + ) |
| 126 | + .as_str(), |
| 127 | + ), |
| 128 | + QueryType::WriteQuery => Client::new() |
| 129 | + .post( |
| 130 | + format!( |
| 131 | + "{url}/{endpoint}?db={db}", |
| 132 | + url = self.url, |
| 133 | + endpoint = endpoint, |
| 134 | + db = self.database, |
| 135 | + ) |
| 136 | + .as_str(), |
| 137 | + ) |
| 138 | + .body(query_str), |
| 139 | + }; |
| 140 | + |
| 141 | + Box::new( |
| 142 | + client |
| 143 | + .send() |
| 144 | + .and_then(|mut res| { |
| 145 | + let body = mem::replace(res.body_mut(), Decoder::empty()); |
| 146 | + body.concat2() |
| 147 | + }) |
| 148 | + .map_err(|err| InfluxDbError::UnspecifiedError { |
| 149 | + error: format!("{}", err), |
| 150 | + }) |
| 151 | + .and_then(|body| { |
| 152 | + if let Ok(utf8) = std::str::from_utf8(&body) { |
| 153 | + let mut s = String::new(); |
| 154 | + utf8.clone_into(&mut s); |
| 155 | + |
| 156 | + // todo: improve error parsing without serde |
| 157 | + if s.contains("\"error\"") { |
| 158 | + return futures::future::err(InfluxDbError::UnspecifiedError { |
| 159 | + error: format!("influxdb error: \"{}\"", s), |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + return futures::future::ok(s); |
| 164 | + } |
| 165 | + |
| 166 | + futures::future::err(InfluxDbError::UnspecifiedError { |
| 167 | + error: "some other error has happened here!".to_string(), |
| 168 | + }) |
| 169 | + }), |
| 170 | + ) |
| 171 | + } |
| 172 | +} |
0 commit comments