use reqwest::StatusCode;
use crate::{
client::MarzbanAPIClient,
error::ApiError,
models::{errors::HTTPValidationError, system::CoreStats},
};
impl MarzbanAPIClient {
pub async fn get_core_stats(&self) -> Result<CoreStats, ApiError> {
let url = format!("{}/api/core", self.inner.base_url);
let response = self
.send_with_auth_retry(|| async {
self.prepare_request(reqwest::Method::GET, url.clone())
})
.await?;
match response.status() {
StatusCode::OK => response
.json::<CoreStats>()
.await
.map_err(ApiError::NetworkError),
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().await?;
Err(ApiError::ApiResponseError(format!(
"Validation Error: {error_response:?}"
)))
}
_ => Err(ApiError::UnexpectedResponse),
}
}
pub async fn restart_core(&self) -> Result<String, ApiError> {
let url = format!("{}/api/core", self.inner.base_url);
let response = self
.send_with_auth_retry(|| async {
self.prepare_request(reqwest::Method::POST, url.clone())
})
.await?;
match response.status() {
StatusCode::OK => response.text().await.map_err(ApiError::NetworkError),
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().await?;
Err(ApiError::ApiResponseError(format!(
"Validation Error: {error_response:?}"
)))
}
_ => Err(ApiError::UnexpectedResponse),
}
}
pub async fn get_core_config(&self) -> Result<String, ApiError> {
let url = format!("{}/api/core/config", self.inner.base_url);
let response = self
.send_with_auth_retry(|| async {
self.prepare_request(reqwest::Method::GET, url.clone())
})
.await?;
match response.status() {
StatusCode::OK => response.text().await.map_err(ApiError::NetworkError),
StatusCode::FORBIDDEN => {
Err(ApiError::ApiResponseError("You're not allowed".to_string()))
}
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().await?;
Err(ApiError::ApiResponseError(format!(
"Validation Error: {error_response:?}"
)))
}
_ => Err(ApiError::UnexpectedResponse),
}
}
pub async fn modify_core_config(
&self,
config_as_json: impl AsRef<str>,
) -> Result<String, ApiError> {
let url = format!("{}/api/core/config", self.inner.base_url);
let config_as_json = config_as_json.as_ref();
let response = self
.send_with_auth_retry(|| async {
self.prepare_request(reqwest::Method::PUT, url.clone())
.json(config_as_json)
})
.await?;
match response.status() {
StatusCode::OK => response.text().await.map_err(ApiError::NetworkError),
StatusCode::FORBIDDEN => {
Err(ApiError::ApiResponseError("You're not allowed".to_string()))
}
StatusCode::UNPROCESSABLE_ENTITY => {
let error_response = response.json::<HTTPValidationError>().await?;
Err(ApiError::ApiResponseError(format!(
"Validation Error: {error_response:?}"
)))
}
_ => Err(ApiError::UnexpectedResponse),
}
}
}