use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, Default)]
pub struct ApiFailure {
pub message: String,
pub call: String,
pub errors: Vec<String>,
pub warnings: Vec<String>,
pub messages: Vec<String>,
pub result_code: Option<i32>,
pub reason: Option<String>,
pub whm_result: Option<i32>,
pub whm_status: Option<i32>,
pub stack: Vec<String>,
}
impl ApiFailure {
pub fn has_errors(&self) -> bool {
!self.errors.is_empty() || !self.message.is_empty()
}
pub fn has_warnings(&self) -> bool {
!self.warnings.is_empty()
}
}
impl std::fmt::Display for ApiFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}] {}", self.call, self.message)?;
if !self.errors.is_empty() {
write!(f, " (errors: {})", self.errors.join("; "))?;
}
if !self.warnings.is_empty() {
write!(f, " (warnings: {})", self.warnings.join("; "))?;
}
if let Some(reason) = &self.reason {
write!(f, " — {reason}")?;
}
Ok(())
}
}
#[derive(Error, Debug)]
pub enum CpanelError {
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("{0}")]
ApiError(ApiFailure),
#[error("{0}")]
ApiWarning(ApiFailure),
#[error("Authentication failed: {0}")]
Auth(String),
#[error("Invalid response: {0}")]
InvalidResponse(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiResponse<T> {
pub status: i32,
pub status_msg: String,
pub data: Option<T>,
pub errors: Option<Vec<String>>,
pub warnings: Option<Vec<String>>,
}
impl<T> ApiResponse<T> {
pub fn is_ok(&self) -> bool {
self.status == 1
}
pub fn into_result(self) -> Result<T, CpanelError> {
if self.status != 1 {
let msg = self
.errors
.as_ref()
.and_then(|e| e.first().cloned())
.unwrap_or_else(|| self.status_msg.clone());
let mut failure = ApiFailure {
message: msg,
..Default::default()
};
failure.errors = self.errors.unwrap_or_default();
failure.warnings = self.warnings.unwrap_or_default();
return Err(CpanelError::ApiError(failure));
}
self.data
.ok_or_else(|| CpanelError::InvalidResponse("No data in response".into()))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpanelResult<T> {
pub cpanelresult: ActionResult<T>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionResult<T> {
pub data: Option<Vec<T>>,
pub error: Option<String>,
pub stack: Option<Vec<StackFrame>>,
pub section: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StackFrame {
pub args: Option<Vec<String>>,
pub func: Option<String>,
pub module: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UapiResult<T> {
pub metadata: Option<Metadata>,
pub data: Option<T>,
pub errors: Option<Vec<String>>,
pub messages: Option<Vec<String>>,
pub status: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Metadata {
pub result: Option<i32>,
pub reason: Option<String>,
pub status: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WhmApiResponse<T> {
pub version: f64,
pub metadata: WhmMetadata,
pub data: Option<T>,
pub errors: Option<Vec<String>>,
pub messages: Option<Vec<String>>,
pub status: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WhmMetadata {
pub result: i32,
pub reason: Option<String>,
pub status: i32,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct QueryParams {
pub user: Option<String>,
pub domain: Option<String>,
pub zone: Option<String>,
pub page: Option<i32>,
pub plan: Option<String>,
pub limit: Option<i32>,
}
impl QueryParams {
pub fn new() -> Self {
Self::default()
}
pub fn user(mut self, user: &str) -> Self {
self.user = Some(user.into());
self
}
pub fn domain(mut self, domain: &str) -> Self {
self.domain = Some(domain.into());
self
}
pub fn zone(mut self, zone: &str) -> Self {
self.zone = Some(zone.into());
self
}
pub fn page(mut self, page: i32) -> Self {
self.page = Some(page);
self
}
pub fn plan(mut self, plan: &str) -> Self {
self.plan = Some(plan.into());
self
}
pub fn limit(mut self, limit: i32) -> Self {
self.limit = Some(limit);
self
}
pub fn to_pairs(&self) -> Vec<(&str, String)> {
let mut pairs = Vec::new();
if let Some(ref v) = self.user {
pairs.push(("user", v.clone()));
}
if let Some(ref v) = self.domain {
pairs.push(("domain", v.clone()));
}
if let Some(ref v) = self.zone {
pairs.push(("zone", v.clone()));
}
if let Some(v) = self.page {
pairs.push(("page", v.to_string()));
}
if let Some(ref v) = self.plan {
pairs.push(("plan", v.clone()));
}
if let Some(v) = self.limit {
pairs.push(("limit", v.to_string()));
}
pairs
}
}