influxdb2/api/
health.rs

1//! Health
2//!
3//! Get health of an InfluxDB instance
4
5use crate::models::HealthCheck;
6use crate::{Client, Http, RequestError, ReqwestProcessing};
7use reqwest::{Method, StatusCode};
8use snafu::ResultExt;
9
10impl Client {
11    /// Get health of an instance
12    pub async fn health(&self) -> Result<HealthCheck, RequestError> {
13        let health_url = self.url("/health");
14        let response = self
15            .request(Method::GET, &health_url)
16            .send()
17            .await
18            .context(ReqwestProcessing)?;
19
20        match response.status() {
21            StatusCode::OK => Ok(response
22                .json::<HealthCheck>()
23                .await
24                .context(ReqwestProcessing)?),
25            StatusCode::SERVICE_UNAVAILABLE => Ok(response
26                .json::<HealthCheck>()
27                .await
28                .context(ReqwestProcessing)?),
29            status => {
30                let text = response.text().await.context(ReqwestProcessing)?;
31                Http { status, text }.fail()?
32            }
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use mockito::mock;
41
42    #[tokio::test]
43    async fn health() {
44        let mock_server = mock("GET", "/health").create();
45
46        let client = Client::new(mockito::server_url(), "", "");
47
48        let _result = client.health().await;
49
50        mock_server.assert();
51    }
52}