use std::time::Duration;
use polyoxide_core::{decode_json, ApiError, HttpClient};
use serde::Deserialize;
use crate::usdm::{
error::UsdmError,
request::{route_builder, Routed},
types::ServerTime,
weight::Route,
};
#[derive(Debug, Clone)]
pub struct Health {
pub(crate) http: HttpClient,
}
#[derive(Deserialize)]
struct Empty {}
impl Health {
pub async fn ping(&self) -> Result<Duration, UsdmError> {
let route = Route::Ping;
let pong = self
.http
.health::<UsdmError>(route.path(), &[route.cost().into()])
.await?;
let text = pong.response.text().await?;
let _: Empty = decode_json(route.path(), &text).map_err(ApiError::from)?;
Ok(pong.round_trip)
}
pub fn time(&self) -> GetTime {
GetTime {
request: Routed::new(&self.http, Route::Time),
}
}
}
route_builder! {
GetTime => ServerTime
}