1
0
mirror of https://github.com/pcvolkmer/checkbar.git synced 2025-04-19 19:16:50 +00:00

Extract response check

This commit is contained in:
Paul-Christian Volkmer 2023-01-14 23:13:34 +01:00
parent 80f42de06a
commit e2f4e8463b
2 changed files with 31 additions and 34 deletions

View File

@ -1,6 +1,8 @@
use reqwest::Response;
use serde::Deserialize;
use crate::checker::{CheckResult, CheckState}; use crate::checker::{CheckResult, CheckState};
use crate::config::CheckConfig; use crate::config::CheckConfig;
use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
struct ActuatorResponse { struct ActuatorResponse {
@ -17,29 +19,22 @@ impl Checker<'_> {
} }
pub async fn check(&self) -> CheckResult { pub async fn check(&self) -> CheckResult {
let state = match reqwest::get(self.check_config.url.as_str()).await {
Ok(r) => {
if r.status().is_success() {
match r.json::<ActuatorResponse>().await {
Ok(ar) => {
if ar.status == "UP" {
CheckState::Up
} else {
CheckState::Warn
}
}
_ => CheckState::Warn,
}
} else {
CheckState::Warn
}
}
Err(_) => CheckState::Down,
};
CheckResult { CheckResult {
name: self.check_config.name.to_string(), name: self.check_config.name.to_string(),
state, state: match reqwest::get(self.check_config.url.as_str()).await {
Ok(r) => Self::check_response(r).await,
Err(_) => CheckState::Down,
},
} }
} }
async fn check_response(response: Response) -> CheckState {
if response.status().is_success() {
return match response.json::<ActuatorResponse>().await {
Ok(ar) if ar.status == "UP" => CheckState::Up,
_ => CheckState::Warn,
};
}
CheckState::Warn
}
} }

View File

@ -1,3 +1,5 @@
use reqwest::Response;
use crate::checker::{CheckResult, CheckState}; use crate::checker::{CheckResult, CheckState};
use crate::config::CheckConfig; use crate::config::CheckConfig;
@ -11,20 +13,20 @@ impl Checker<'_> {
} }
pub async fn check(&self) -> CheckResult { pub async fn check(&self) -> CheckResult {
let state = match reqwest::get(self.check_config.url.as_str()).await { CheckResult {
Ok(r) => { name: self.check_config.name.to_string(),
if r.status().is_success() { state: match reqwest::get(self.check_config.url.as_str()).await {
Ok(r) => Self::check_response(r).await,
Err(_) => CheckState::Down,
},
}
}
async fn check_response(response: Response) -> CheckState {
if response.status().is_success() {
CheckState::Up CheckState::Up
} else { } else {
CheckState::Warn CheckState::Warn
} }
} }
Err(_) => CheckState::Down,
};
CheckResult {
name: self.check_config.name.to_string(),
state,
}
}
} }