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
Aakash Hemadri 2021-03-16 18:36:20 +05:30 committed by GitHub
parent 51304fd138
commit 9c112b9872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -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(())
}

View File

@ -302,3 +302,5 @@ cpu,host=server01,region=us-west usage=0.87
Ok(())
}
}
mod ready;

View File

@ -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(())
}
}