rainy_sdk/endpoints/health.rs
1use crate::client::RainyClient;
2use crate::error::Result;
3use crate::models::{HealthCheck, ServiceStatus};
4use serde::Deserialize;
5
6impl RainyClient {
7 /// Performs a basic health check on the Rainy API.
8 ///
9 /// This method is useful for quickly verifying that the API is up and running.
10 ///
11 /// # Returns
12 ///
13 /// A `Result` containing a `HealthCheck` struct with basic health information.
14 ///
15 /// # Example
16 ///
17 /// ```rust,no_run
18 /// # use rainy_sdk::RainyClient;
19 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
20 /// let client = RainyClient::with_api_key("user-api-key")?;
21 /// let health = client.health_check().await?;
22 /// println!("API Status: {}", health.status);
23 /// # Ok(())
24 /// # }
25 /// ```
26 pub async fn health_check(&self) -> Result<HealthCheck> {
27 #[derive(Deserialize)]
28 struct RootHealthResponse {
29 status: String,
30 timestamp: String,
31 }
32
33 let response = self
34 .http_client()
35 .get(self.root_url("/health"))
36 .send()
37 .await?;
38 let payload: RootHealthResponse = self.handle_response(response).await?;
39
40 Ok(HealthCheck {
41 status: payload.status,
42 timestamp: payload.timestamp,
43 uptime: 0.0,
44 services: ServiceStatus {
45 database: false,
46 redis: None,
47 providers: false,
48 },
49 })
50 }
51
52 /// Performs a detailed health check on the Rainy API and its underlying services.
53 ///
54 /// This method provides more in-depth information, including the status of the database,
55 /// Redis, and connections to AI providers.
56 ///
57 /// # Returns
58 ///
59 /// A `Result` containing a `HealthCheck` struct with detailed service status.
60 ///
61 /// # Example
62 ///
63 /// ```rust,no_run
64 /// # use rainy_sdk::RainyClient;
65 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
66 /// let client = RainyClient::with_api_key("user-api-key")?;
67 /// let health = client.detailed_health_check().await?;
68 /// println!("Database status: {}", health.services.database);
69 /// println!("Providers status: {}", health.services.providers);
70 /// # Ok(())
71 /// # }
72 /// ```
73 pub async fn detailed_health_check(&self) -> Result<HealthCheck> {
74 #[derive(Deserialize)]
75 struct DependencyFlags {
76 database: bool,
77 redis: bool,
78 #[serde(rename = "openrouterConfigured")]
79 openrouter_configured: bool,
80 #[serde(rename = "polarConfigured")]
81 polar_configured: bool,
82 }
83 #[derive(Deserialize)]
84 struct DependenciesHealthResponse {
85 status: String,
86 timestamp: String,
87 dependencies: DependencyFlags,
88 }
89
90 let response = self
91 .http_client()
92 .get(self.root_url("/health/dependencies"))
93 .send()
94 .await?;
95 let payload: DependenciesHealthResponse = self.handle_response(response).await?;
96
97 Ok(HealthCheck {
98 status: payload.status,
99 timestamp: payload.timestamp,
100 uptime: 0.0,
101 services: ServiceStatus {
102 database: payload.dependencies.database,
103 redis: Some(payload.dependencies.redis),
104 providers: payload.dependencies.openrouter_configured
105 && payload.dependencies.polar_configured,
106 },
107 })
108 }
109}