Reorganizing files

This commit is contained in:
Marco De Araujo 2025-12-23 08:57:20 -04:00
parent 1c4077ffc3
commit 883a0669fe
11 changed files with 105 additions and 45 deletions

40
src/api/client.rs Normal file
View file

@ -0,0 +1,40 @@
use anyhow::Result;
use reqwest::blocking::Client;
use crate::{
api::endpoints::UptimeKumaEndpoints,
data::{self, heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse},
};
pub struct UptimeKumaClient {
client: Client,
}
impl UptimeKumaClient {
pub fn new() -> Self {
Self {
client: Client::new(),
}
}
pub fn fetch_heartbeat(&self, endpoints: &UptimeKumaEndpoints) -> Result<HeartbeatResponse> {
let response = self.client.get(endpoints.heartbeat_url()).send()?;
if response.status().is_success() {
let json_text = response.text()?;
data::heartbeat::parse_response(&json_text)
} else {
return Err(anyhow::anyhow!(response.status()));
}
}
pub fn fetch_status_page(&self, endpoints: &UptimeKumaEndpoints) -> Result<StatusPageResponse> {
let response = self.client.get(endpoints.status_page_url()).send()?;
if response.status().is_success() {
let json_text = response.text()?;
data::status_page::parse_response(&json_text)
} else {
return Err(anyhow::anyhow!(response.status()));
}
}
}

25
src/api/endpoints.rs Normal file
View file

@ -0,0 +1,25 @@
use url::Url;
#[derive(Debug, Clone)]
pub struct UptimeKumaEndpoints {
base_url: Url,
slug: String,
}
impl UptimeKumaEndpoints {
pub fn new(base_url: &str, slug: &str) -> Result<Self, url::ParseError> {
let base_url = Url::parse(base_url.trim_end_matches("/"))?;
Ok(Self {
base_url,
slug: slug.to_string(),
})
}
pub fn heartbeat_url(&self) -> String {
format!("{}api/status-page/heartbeat/{}", self.base_url, self.slug)
}
pub fn status_page_url(&self) -> String {
format!("{}api/status-page/{}", self.base_url, self.slug)
}
}

5
src/api/mod.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod client;
pub mod endpoints;
pub use client::UptimeKumaClient;
pub use endpoints::UptimeKumaEndpoints;