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> {
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()));
}
self.fetch_and_parse(endpoints.heartbeat_url(), data::heartbeat::parse_response)
}
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() {
let json_text = response.text()?;
data::status_page::parse_response(&json_text)
parser(&json_text)
} else {
return Err(anyhow::anyhow!(response.status()));
Err(anyhow::anyhow!(
"URL: {}, Error: {}",
url,
response.status()
))
}
}
}