Skip to main content

polyoxide_data/api/
health.rs

1use 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/// Health namespace for API health operations
10#[derive(Clone)]
11pub struct Health {
12    pub(crate) client: Client,
13    pub(crate) base_url: Url,
14}
15
16impl Health {
17    /// Check API health status
18    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    /// Measure the round-trip time (RTT) to the Polymarket Data API.
31    ///
32    /// Makes a GET request to the API root and returns the latency.
33    ///
34    /// # Example
35    ///
36    /// ```no_run
37    /// use polyoxide_data::DataApi;
38    ///
39    /// # async fn example() -> Result<(), polyoxide_data::DataApiError> {
40    /// let client = DataApi::new()?;
41    /// let latency = client.health().ping().await?;
42    /// println!("API latency: {}ms", latency.as_millis());
43    /// # Ok(())
44    /// # }
45    /// ```
46    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/// Health check response
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct HealthResponse {
62    /// Status indicator (returns "OK" when healthy)
63    pub data: String,
64}