feat: Add support for ready API to influxdb_client (#999)
Get the readiness of an instance at startup Signed-off-by: Aakash Hemadri <aakashhemadri123@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>pull/24376/head
parent
51304fd138
commit
9c112b9872
|
@ -0,0 +1,11 @@
|
|||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let influx_url = "some-url";
|
||||
let token = "some-token";
|
||||
|
||||
let client = influxdb2_client::Client::new(influx_url, token);
|
||||
|
||||
println!("{:?}", client.ready().await?);
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -302,3 +302,5 @@ cpu,host=server01,region=us-west usage=0.87
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod ready;
|
||||
|
|
|
@ -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<bool, RequestError> {
|
||||
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<dyn std::error::Error>;
|
||||
type Result<T = (), E = Error> = std::result::Result<T, E>;
|
||||
|
||||
#[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(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue