use polyoxide_core::{HttpClient, Request, RequestError};
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
use crate::error::DataApiError;
#[derive(Clone)]
pub struct Health {
pub(crate) http_client: HttpClient,
}
impl Health {
pub async fn check(&self) -> Result<HealthResponse, DataApiError> {
Request::<HealthResponse, DataApiError>::new(self.http_client.clone(), "/")
.send()
.await
}
pub async fn ping(&self) -> Result<Duration, DataApiError> {
let _permit = self.http_client.acquire_concurrency().await;
self.http_client.acquire_rate_limit("/", None).await;
let start = Instant::now();
let response = self
.http_client
.client
.get(self.http_client.base_url.clone())
.send()
.await?;
let latency = start.elapsed();
if !response.status().is_success() {
return Err(DataApiError::from_response(response).await);
}
Ok(latency)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
pub data: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_health_response() {
let json = r#"{"data": "OK"}"#;
let health: HealthResponse = serde_json::from_str(json).unwrap();
assert_eq!(health.data, "OK");
}
#[test]
fn health_response_roundtrip() {
let original = HealthResponse {
data: "OK".to_string(),
};
let json = serde_json::to_string(&original).unwrap();
let deserialized: HealthResponse = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.data, original.data);
}
}