use self::types::{
DnsRecord, DnsRecordCreateRequest, DnsRecordCreateResponse, DnsRecordEditRequest, DnsRecordListResponse,
DnssecCreateRequest, DnssecRecord, DnssecRecordListResponse,
};
use super::{client::Porkbun, endpoints, types::StatusResponse};
use crate::{porkbun::dns::types::{DnsRecordCreateOptions, DnsRecordEditOptions}, Result};
pub mod types;
pub struct Dns<'a> {
client: &'a Porkbun,
domain: &'a str,
}
impl<'a> Dns<'a> {
pub(super) fn new(client: &'a Porkbun, domain: &'a str) -> Self {
Self { client, domain }
}
pub async fn create_record(&self, options: DnsRecordCreateOptions<'_>) -> Result<DnsRecordCreateResponse> {
let path = format!("{}{}", endpoints::DNS_CREATE, self.domain);
let body = DnsRecordCreateRequest {
auth: self.client.auth.clone(),
name: options.name,
r#type: options.r#type,
content: options.content,
ttl: options.ttl,
prio: options.prio,
};
self.client.post(&path, &body).await
}
pub async fn edit_record_by_id(&self, record_id: u64, options: DnsRecordEditOptions<'_>) -> Result<StatusResponse> {
let path = format!("{}{}/{}", endpoints::DNS_EDIT_BY_ID, self.domain, record_id);
let body = DnsRecordEditRequest {
auth: self.client.auth.clone(),
name: options.name,
r#type: options.r#type,
content: options.content,
ttl: options.ttl,
prio: options.prio,
};
self.client.post(&path, &body).await
}
pub async fn delete_record_by_id(&self, record_id: u64) -> Result<StatusResponse> {
let path = format!("{}{}/{}", endpoints::DNS_DELETE_BY_ID, self.domain, record_id);
self.client.post(&path, &self.client.auth).await
}
pub async fn retrieve_all_records(&self) -> Result<Vec<DnsRecord>> {
let path = format!("{}{}", endpoints::DNS_RETRIEVE_BY_DOMAIN, self.domain);
let response: DnsRecordListResponse = self.client.post(&path, &self.client.auth).await?;
Ok(response.records)
}
pub async fn retrieve_record_by_id(&self, record_id: u64) -> Result<Option<DnsRecord>> {
let path = format!("{}{}/{}", endpoints::DNS_RETRIEVE_BY_DOMAIN, self.domain, record_id);
let response: DnsRecordListResponse = self.client.post(&path, &self.client.auth).await?;
Ok(response.records.into_iter().next())
}
pub async fn retrieve_records_by_name_type(&self, record_type: &str, subdomain: &str) -> Result<Vec<DnsRecord>> {
let mut path = format!(
"{}{}/{}",
endpoints::DNS_RETRIEVE_BY_NAME_TYPE,
self.domain,
record_type
);
if !subdomain.is_empty() {
path.push('/');
path.push_str(subdomain);
}
let response: DnsRecordListResponse = self.client.post(&path, &self.client.auth).await?;
Ok(response.records)
}
pub async fn create_dnssec_record(&self, record: &'a DnssecRecord) -> Result<StatusResponse> {
let path = format!("{}{}", endpoints::DNSSEC_CREATE, self.domain);
let body = DnssecCreateRequest {
auth: self.client.auth.clone(),
record,
};
self.client.post(&path, &body).await
}
pub async fn get_dnssec_records(&self) -> Result<std::collections::HashMap<String, DnssecRecord>> {
let path = format!("{}{}", endpoints::DNSSEC_GET, self.domain);
let response: DnssecRecordListResponse = self.client.post(&path, &self.client.auth).await?;
Ok(response.records)
}
pub async fn delete_dnssec_record(&self, key_tag: &str) -> Result<StatusResponse> {
let path = format!("{}{}/{}", endpoints::DNSSEC_DELETE, self.domain, key_tag);
self.client.post(&path, &self.client.auth).await
}
}