diff --git a/influxdb2_client/examples/ready.rs b/influxdb2_client/examples/ready.rs new file mode 100644 index 0000000000..07d69c4d88 --- /dev/null +++ b/influxdb2_client/examples/ready.rs @@ -0,0 +1,11 @@ +#[tokio::main] +async fn main() -> Result<(), Box> { + let influx_url = "some-url"; + let token = "some-token"; + + let client = influxdb2_client::Client::new(influx_url, token); + + println!("{:?}", client.ready().await?); + + Ok(()) +} diff --git a/influxdb2_client/src/lib.rs b/influxdb2_client/src/lib.rs index 52a1b60e6a..f332a0a154 100644 --- a/influxdb2_client/src/lib.rs +++ b/influxdb2_client/src/lib.rs @@ -302,3 +302,5 @@ cpu,host=server01,region=us-west usage=0.87 Ok(()) } } + +mod ready; diff --git a/influxdb2_client/src/ready.rs b/influxdb2_client/src/ready.rs new file mode 100644 index 0000000000..fae9ecce94 --- /dev/null +++ b/influxdb2_client/src/ready.rs @@ -0,0 +1,51 @@ +use reqwest::{Method, StatusCode}; +use snafu::ResultExt; + +use super::{Client, Http, RequestError, ReqwestProcessing}; + +impl Client { + /// Get the readiness of an instance at startup + pub async fn ready(&self) -> Result { + let ready_url = format!("{}/ready", self.url); + let response = self + .request(Method::GET, &ready_url) + .send() + .await + .context(ReqwestProcessing)?; + + match response.status() { + StatusCode::OK => Ok(true), + _ => { + let status = response.status(); + let text = response.text().await.context(ReqwestProcessing)?; + Http { status, text }.fail()? + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use mockito::mock; + + type Error = Box; + type Result = std::result::Result; + + #[tokio::test] + async fn ready() -> Result { + let token = "some-token"; + + let mock_server = mock("GET", "/ready") + .match_header("Authorization", format!("Token {}", token).as_str()) + .create(); + + let client = Client::new(&mockito::server_url(), token); + + let _result = client.ready().await; + + mock_server.assert(); + + Ok(()) + } +}