41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use anyhow::Result;
|
|
use reqwest::blocking::Client;
|
|
|
|
use crate::{
|
|
api::endpoints::UptimeKumaEndpoints,
|
|
data::{self, heartbeat::HeartbeatResponse, status_page::model::StatusPageResponse},
|
|
};
|
|
|
|
#[derive(Debug, Clone)]
|
|
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()));
|
|
}
|
|
}
|
|
}
|