Refactoring
This commit is contained in:
parent
4eb4c64398
commit
349e0cf3ff
3 changed files with 39 additions and 14 deletions
|
|
@ -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()
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,28 @@ impl UptimeKumaEndpoints {
|
|||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,7 +188,7 @@ impl App {
|
|||
}
|
||||
Err(e) => {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue