feat: Reporting
This commit is contained in:
33
src/api.rs
33
src/api.rs
@@ -1,3 +1,5 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::models::{ApiResponse, SuplError};
|
||||
|
||||
pub fn fetch_api(provider_url: &str) -> Result<ApiResponse, SuplError> {
|
||||
@@ -14,3 +16,34 @@ pub fn fetch_api(provider_url: &str) -> Result<ApiResponse, SuplError> {
|
||||
reason: e.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct StatusResponse {
|
||||
working: bool,
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
pub fn test_api(provider_url: &str) -> Result<(), SuplError> {
|
||||
let trimmed = provider_url.trim_end_matches('/');
|
||||
let url = format!("{}/status", trimmed);
|
||||
|
||||
let status: StatusResponse = minreq::get(&url)
|
||||
.send()
|
||||
.map_err(|e| SuplError::NetworkError {
|
||||
reason: e.to_string(),
|
||||
})?
|
||||
.json()
|
||||
.map_err(|e| SuplError::ParseError {
|
||||
reason: e.to_string(),
|
||||
})?;
|
||||
|
||||
if status.working {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(SuplError::ParseError {
|
||||
reason: status
|
||||
.message
|
||||
.unwrap_or_else(|| "Provider reported not working".to_string()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user