use reqwest::Method;
use serde_json::Value;
use super::{query, urlencode};
use crate::error::Result;
use crate::http::HttpTransport;
use crate::models::{
DkimRotation, Domain, DomainAvailability, DomainCheck, DomainDiagnosis, DomainHealth,
DomainListItem, DomainTransfer, TransferDomain,
};
#[derive(Debug, Clone)]
pub struct Domains {
http: HttpTransport,
}
impl Domains {
pub(crate) fn new(http: HttpTransport) -> Self {
Self { http }
}
pub async fn list(&self) -> Result<Vec<DomainListItem>> {
self.http.request(Method::GET, "/v1/domains/").await
}
pub async fn create(&self, name: &str) -> Result<Domain> {
let body = serde_json::json!({ "name": name });
self.http
.request_json(Method::POST, "/v1/domains/", &body)
.await
}
pub async fn get(&self, id: &str) -> Result<Domain> {
self.http
.request(Method::GET, &format!("/v1/domains/{}", urlencode(id)))
.await
}
pub async fn delete(&self, id: &str) -> Result<()> {
self.http
.request_empty(Method::DELETE, &format!("/v1/domains/{}", urlencode(id)))
.await
}
pub async fn verify(&self, id: &str) -> Result<Domain> {
self.http
.request(
Method::POST,
&format!("/v1/domains/{}/verify", urlencode(id)),
)
.await
}
pub async fn health(&self, id: &str) -> Result<DomainHealth> {
self.http
.request(
Method::GET,
&format!("/v1/domains/{}/health", urlencode(id)),
)
.await
}
pub async fn diagnose(&self, id: &str) -> Result<DomainDiagnosis> {
self.http
.request(
Method::GET,
&format!("/v1/domains/{}/diagnose", urlencode(id)),
)
.await
}
pub async fn mx_status(&self, id: &str) -> Result<Value> {
self.http
.request(
Method::GET,
&format!("/v1/domains/{}/mx-status", urlencode(id)),
)
.await
}
pub async fn published_records(&self, id: &str) -> Result<Value> {
self.http
.request(
Method::GET,
&format!("/v1/domains/{}/published-records", urlencode(id)),
)
.await
}
pub async fn rotate_dkim(&self, id: &str) -> Result<DkimRotation> {
self.http
.request(
Method::POST,
&format!("/v1/domains/{}/rotate-dkim", urlencode(id)),
)
.await
}
pub async fn transfer(&self, id: &str, params: &TransferDomain) -> Result<DomainTransfer> {
self.http
.request_json(
Method::POST,
&format!("/v1/domains/{}/transfer", urlencode(id)),
params,
)
.await
}
pub async fn check_availability(&self, name: &str) -> Result<DomainAvailability> {
let q = query(&[("name", Some(name.to_string()))]);
self.http
.request(Method::GET, &format!("/v1/domains/check-availability{q}"))
.await
}
pub async fn check(&self, name: &str) -> Result<DomainCheck> {
self.http
.request(
Method::GET,
&format!("/v1/domains/check/{}", urlencode(name)),
)
.await
}
}