Refactoring

This commit is contained in:
Marco De Araujo 2026-01-05 13:51:55 -04:00
parent 4eb4c64398
commit 349e0cf3ff
3 changed files with 39 additions and 14 deletions

View file

@ -19,23 +19,30 @@ impl UptimeKumaClient {
} }
pub fn fetch_heartbeat(&self, endpoints: &UptimeKumaEndpoints) -> Result<HeartbeatResponse> { pub fn fetch_heartbeat(&self, endpoints: &UptimeKumaEndpoints) -> Result<HeartbeatResponse> {
let response = self.client.get(endpoints.heartbeat_url()).send()?; self.fetch_and_parse(endpoints.heartbeat_url(), data::heartbeat::parse_response)
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> { pub fn fetch_status_page(&self, endpoints: &UptimeKumaEndpoints) -> Result<StatusPageResponse> {
let response = self.client.get(endpoints.status_page_url()).send()?; self.fetch_and_parse(
endpoints.status_page_url(),
data::status_page::parse_response,
)
}
fn fetch_and_parse<T, F>(&self, url: String, parser: F) -> Result<T>
where
F: FnOnce(&str) -> Result<T>,
{
let response = self.client.get(url.clone()).send()?;
if response.status().is_success() { if response.status().is_success() {
let json_text = response.text()?; let json_text = response.text()?;
data::status_page::parse_response(&json_text) parser(&json_text)
} else { } else {
return Err(anyhow::anyhow!(response.status())); Err(anyhow::anyhow!(
"URL: {}, Error: {}",
url,
response.status()
))
} }
} }
} }

View file

@ -16,10 +16,28 @@ impl UptimeKumaEndpoints {
} }
pub fn heartbeat_url(&self) -> String { pub fn heartbeat_url(&self) -> String {
format!("{}api/status-page/heartbeat/{}", self.base_url, self.slug) let mut url = self.get_url();
url.path_segments_mut()
.unwrap()
.push("heartbeat")
.push(&self.slug);
url.to_string()
} }
pub fn status_page_url(&self) -> String { pub fn status_page_url(&self) -> String {
format!("{}api/status-page/{}", self.base_url, self.slug) let mut url = self.get_url();
url.path_segments_mut()
.unwrap()
.push(&self.slug);
url.to_string()
}
fn get_url(&self) -> Url {
let mut url = self.base_url.clone();
url.path_segments_mut()
.unwrap()
.push("api")
.push("status-page");
url
} }
} }

View file

@ -188,7 +188,7 @@ impl App {
} }
Err(e) => { Err(e) => {
let mut error = HashMap::new(); let mut error = HashMap::new();
error.insert("error", e.root_cause().to_string()); error.insert("error", e.to_string());
self.state.error_message = Some(t_with_args("update-fail", &error)); self.state.error_message = Some(t_with_args("update-fail", &error));
} }
} }