use serde_json::{ Value, json };
use std::error::Error;
use tracing::{ info, error };
use crate::{ NameCheapClient, Host };
use crate::utils::request_builder::Request;
impl NameCheapClient {
pub async fn domains_dns_get_hosts(
&self,
sld: &str,
tld: &str
) -> Result<Value, Box<dyn Error>> {
let command = "namecheap.domains.dns.getHosts";
let params = json!({ "SLD": sld, "TLD": tld });
let response = Request::new(
self.clone(),
command.to_string(),
Some(1),
None,
Some(params),
).send().await?;
info!("Response: {:#?}", response);
let hosts = response
.pointer("/ApiResponse/CommandResponse/DomainDNSGetHostsResult/host")
.cloned()
.unwrap_or_else(|| json!([]));
Ok(hosts)
}
}
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
use serde_json::json;
use tracing::info;
#[tokio::test]
async fn test_domains_dns_get_hosts() {
dotenv().ok();
let client = NameCheapClient::new_from_env().unwrap();
let host_records = client.domains_dns_get_hosts("xylex", "ai").await.unwrap();
info!("Host Records: {:#?}", host_records);
assert!(host_records.as_array().map_or(false, |arr| arr.len() >= 2), "Expected at least two host records");
}
}