From b7d25ef259352f9e2c9ac5e1157552a88d5f7d5b Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 30 Apr 2026 21:33:12 -0700 Subject: [PATCH] MOdularizing kuma interface --- src/{ => kuma}/api.rs | 12 +++++++++--- src/{ => kuma}/data.rs | 0 src/kuma/mod.rs | 2 ++ src/main.rs | 6 +++--- 4 files changed, 14 insertions(+), 6 deletions(-) rename src/{ => kuma}/api.rs (84%) rename src/{ => kuma}/data.rs (100%) create mode 100644 src/kuma/mod.rs diff --git a/src/api.rs b/src/kuma/api.rs similarity index 84% rename from src/api.rs rename to src/kuma/api.rs index b1f5080..38f8b5d 100644 --- a/src/api.rs +++ b/src/kuma/api.rs @@ -8,12 +8,12 @@ pub const BASE_URL: &str = "https://uptime.shockrah.xyz"; #[macro_export] macro_rules! heartbeat { - ($slug:expr) => { format!("{}/api/status-page/heartbeat/{}", crate::api::BASE_URL, $slug) } + ($slug:expr) => { format!("{}/api/status-page/heartbeat/{}", BASE_URL, $slug) } } #[macro_export] macro_rules! endpoints { - ($slug:expr) => { format!("{}/api/status-page/{}", crate::api::BASE_URL, $slug) } + ($slug:expr) => { format!("{}/api/status-page/{}", BASE_URL, $slug) } } /// A single heartbeat within a monitor's latest status @@ -48,10 +48,11 @@ pub struct KumaStatusPage { } impl KumaMonitor { + /// Generates a full monitor object with the most recent available heartbeats pub async fn new(val: &Value) -> Result { // Populate the monitor with it's respective heartbeats at that time let id = val["id"].as_i64().unwrap_or(0); - let name = val["name"].to_string(); + let name = val["name"].as_str().unwrap().into(); let response: Value = reqwest::get(heartbeat!("pub")).await?.json().await?; if let Some(list) = &response["heartbeatList"][id.to_string()].as_array() { let heartbeats = list.iter().map(|item| { @@ -72,6 +73,9 @@ impl KumaMonitor { impl KumaStatusPage { + /// Monitors require their own logic to fetch with their heartbeats on load. + /// This func is only really called by Self::get when we are first loading + /// a new status page for the first time async fn get_monitors(json: &Value) -> Vec { let mut monitors = vec![]; for group in json["publicGroupList"].as_array().unwrap_or(&vec![]) { @@ -84,6 +88,8 @@ impl KumaStatusPage { return monitors; } + /// Main entrypoint for KumaStatePage API hits. Here we get the basics of the page + /// along with any and all heartbeats for the monitors on that page pub async fn get(slug: &str) -> Result { let endpoint = endpoints!(slug); let resp: Value = reqwest::get(&endpoint).await?.json().await?; diff --git a/src/data.rs b/src/kuma/data.rs similarity index 100% rename from src/data.rs rename to src/kuma/data.rs diff --git a/src/kuma/mod.rs b/src/kuma/mod.rs new file mode 100644 index 0000000..783f2bc --- /dev/null +++ b/src/kuma/mod.rs @@ -0,0 +1,2 @@ +pub mod api; +pub mod data; diff --git a/src/main.rs b/src/main.rs index 43761e6..6c9c9f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ -mod data; -mod api; +mod kuma; use reqwest::{self, Error}; -use crate::api::KumaStatusPage; +use crate::kuma::api::KumaStatusPage; use serde_json; @@ -10,6 +9,7 @@ use serde_json; #[tokio::main] async fn main() -> Result<(), Error> { let public = KumaStatusPage::get("pub").await?; + // Debugging the initial API response match serde_json::to_string(&public) { Ok(result) => println!("{result}"), Err(_) => eprintln!("bruh")