polyoxide_data/api/
health.rs1use polyoxide_core::RequestError;
2use reqwest::Client;
3use serde::{Deserialize, Serialize};
4use std::time::{Duration, Instant};
5use url::Url;
6
7use crate::error::DataApiError;
8
9#[derive(Clone)]
11pub struct Health {
12 pub(crate) client: Client,
13 pub(crate) base_url: Url,
14}
15
16impl Health {
17 pub async fn check(&self) -> Result<HealthResponse, DataApiError> {
19 let response = self.client.get(self.base_url.clone()).send().await?;
20 let status = response.status();
21
22 if !status.is_success() {
23 return Err(DataApiError::from_response(response).await);
24 }
25
26 let health: HealthResponse = response.json().await?;
27 Ok(health)
28 }
29
30 pub async fn ping(&self) -> Result<Duration, DataApiError> {
47 let start = Instant::now();
48 let response = self.client.get(self.base_url.clone()).send().await?;
49 let latency = start.elapsed();
50
51 if !response.status().is_success() {
52 return Err(DataApiError::from_response(response).await);
53 }
54
55 Ok(latency)
56 }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct HealthResponse {
62 pub data: String,
64}