1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use serde_json::{ Value, json };
use std::error::Error;
use tracing::{ info, error };
// crate imports
use crate::{ NameCheapClient };
use crate::utils::request_builder::Request;
use crate::utils::xml_parser::parse_xml_to_json;
/// Represents the parameters required for setting DNS host records.
#[derive(Debug, Clone)]
pub struct HostRequest {
pub host_name: String,
pub record_type: String,
pub address: String,
pub mx_pref: Option<String>,
pub email_type: Option<String>,
pub ttl: Option<String>,
pub flag: Option<String>,
pub tag: Option<String>,
}
impl HostRequest {
pub fn new(
host_name: String,
record_type: String,
address: String,
mx_pref: Option<String>,
email_type: Option<String>,
ttl: Option<String>,
flag: Option<String>,
tag: Option<String>
) -> Self {
HostRequest {
host_name,
record_type,
address,
mx_pref,
email_type,
ttl,
flag,
tag,
}
}
}
impl NameCheapClient {
/// Sets DNS host records for a given domain.
///
/// # Example
///
/// ```rust
/// use namecheap::NameCheapClient;
///
/// #[tokio::main]
/// async fn main() {
/// let client = NameCheapClient::new(
/// "api_user".to_string(),
/// "api_key".to_string(),
/// "client_ip".to_string(),
/// "user_name".to_string(),
/// false
/// );
///
/// let host_records = vec![
/// HostRequest {
/// host_name: "@".to_string(),
/// record_type: "A".to_string(),
/// address: "213.87.128.103".to_string(),
/// mx_pref: None,
/// email_type: None,
/// ttl: Some("3600".to_string()),
/// flag: None,
/// tag: None,
/// },
/// HostRequest {
/// host_name: "www".to_string(),
/// record_type: "CNAME".to_string(),
/// address: "example.com.".to_string(),
/// mx_pref: None,
/// email_type: None,
/// ttl: Some("1200".to_string()),
/// flag: None,
/// tag: None,
/// }
/// ];
///
/// let result = client.domains_dns_set_hosts("domain", "com", host_records).await.unwrap();
/// println!("Set Hosts Result: {:?}", result);
/// }
/// ```
pub async fn domains_dns_set_hosts(
&self,
sld: &str,
tld: &str,
new_hosts: Vec<HostRequest>
) -> Result<Value, Box<dyn Error>> {
// Retrieve existing hosts
let existing_hosts = self.domains_dns_get_hosts(sld, tld).await?;
info!("Existing Hosts: {:#?}", existing_hosts);
// Check if there are existing hosts
let combined_hosts: Vec<Value> = if
let Some(existing_hosts_array) = existing_hosts.as_array()
{
// Combine existing and new hosts
existing_hosts_array
.iter()
.cloned()
.chain(
new_hosts
.iter()
.map(|host| {
json!({
"HostName": host.host_name,
"RecordType": host.record_type,
"Address": host.address,
"MXPref": host.mx_pref,
"EmailType": host.email_type,
"TTL": host.ttl,
"Flag": host.flag,
"Tag": host.tag
})
})
)
.collect()
} else {
// If no existing hosts, use only new hosts
new_hosts
.iter()
.map(|host| {
json!({
"HostName": host.host_name,
"RecordType": host.record_type,
"Address": host.address,
"MXPref": host.mx_pref,
"EmailType": host.email_type,
"TTL": host.ttl,
"Flag": host.flag,
"Tag": host.tag
})
})
.collect()
};
info!("Combined Hosts: {:#?}", combined_hosts);
let request_values: Vec<Value> = combined_hosts
.iter()
.enumerate()
.flat_map(|(index, host)| {
let idx = index + 1;
vec![
json!({
"Key": format!("HostName{}", idx),
"Value": host.get("HostName").unwrap_or(&Value::String("".to_string())).as_str().unwrap().to_string()
}),
json!({
"Key": format!("RecordType{}", idx),
"Value": host.get("RecordType").unwrap_or(&Value::String("".to_string())).as_str().unwrap().to_string()
}),
json!({
"Key": format!("Address{}", idx),
"Value": host.get("Address").unwrap_or(&Value::String("".to_string())).as_str().unwrap().to_string()
}),
json!({
"Key": format!("TTL{}", idx),
"Value": host.get("TTL").unwrap_or(&Value::String("".to_string())).as_str().unwrap().to_string()
})
]
})
.collect();
let body: Value =
json!({
"authDetails": {
"ParentUserType": "",
"ParentUserId": 0,
"UserId": "",
"UserName": self.user_name,
"ClientIp": self.client_ip,
"EndUserIp": "",
"AdminUserName": self.user_name,
"DisableSecurityNotification": true,
"AllowWhenDomainLocked": true,
"ProceedWhenDomainLockedFlag": true,
"DefaultChargeForUserName": "",
"Roles": ["User"]
},
"request": {
"RequestValues": request_values,
"SLD": sld,
"TLD": tld
}
});
info!("Request Body: {:#?}", body);
let xml_body =
r#"
<?xml version="1.0" encoding="UTF-8"?>
<ApiRequest>
<Command>namecheap.domains.dns.setHosts</Command>
<ClientIp>{client_ip}</ClientIp>
<UserName>{user_name}</UserName>
<ApiUser>{api_user}</ApiUser>
<ApiKey>{api_key}</ApiKey>
<SLD>{sld}</SLD>
<TLD>{tld}</TLD>
{hosts}
</ApiRequest>
"#;
let hosts_xml: String = combined_hosts
.iter()
.enumerate()
.map(|(index, host)| {
format!(
r#"<Host HostId="{}" Name="{}" Type="{}" Address="{}" TTL="{}" />"#,
index + 1,
host
.get("HostName")
.unwrap_or(&Value::String("".to_string()))
.as_str()
.unwrap(),
host
.get("RecordType")
.unwrap_or(&Value::String("".to_string()))
.as_str()
.unwrap(),
host.get("Address").unwrap_or(&Value::String("".to_string())).as_str().unwrap(),
host.get("TTL").unwrap_or(&Value::String("".to_string())).as_str().unwrap()
)
})
.collect::<Vec<String>>()
.join("\n");
let xml_body = xml_body
.replace("{client_ip}", &self.client_ip)
.replace("{user_name}", &self.user_name)
.replace("{api_user}", &self.api_user)
.replace("{api_key}", &self.api_key)
.replace("{sld}", sld)
.replace("{tld}", tld)
.replace("{hosts}", &hosts_xml);
let client = reqwest::Client::new();
let url = format!(
"https://api.namecheap.com/xml.response?ApiUser={api_user}&ApiKey={api_key}&UserName={user_name}&Command={command}&ClientIp={client_ip}&SLD={sld}&TLD={tld}",
api_user = self.api_user,
api_key = self.api_key,
user_name = self.user_name,
command = "namecheap.domains.dns.setHosts",
client_ip = self.client_ip,
sld = sld,
tld = tld
);
let response = client
.post(&url)
.header("Content-Type", "application/xml")
.body(xml_body)
.send().await?;
let response_text = response.text().await?;
let json_value: Value = parse_xml_to_json(&response_text)?;
info!("Response: {:#?}", json_value);
let result = json_value
.pointer("/ApiResponse/CommandResponse/DomainDNSSetHostsResult")
.ok_or("Failed to set host records")?
.clone();
// Check if the operation was successful
if result.get("IsSuccess").and_then(Value::as_bool).unwrap_or(false) {
info!("Set Hosts operation was successful.");
} else {
error!("Set Hosts operation failed.");
}
info!("Set Hosts Result: {:#?}", result);
Ok(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
use serde_json::json;
use tracing::info;
#[tokio::test]
async fn test_domains_dns_set_hosts() {
dotenv().ok();
let client = NameCheapClient::new_from_env().unwrap();
let new_host1 = HostRequest {
host_name: "@".to_string(),
record_type: "A".to_string(),
address: "213.87.128.103".to_string(),
mx_pref: None,
email_type: None,
ttl: Some("3600".to_string()),
flag: None,
tag: None,
};
let new_host2 = HostRequest {
host_name: "www".to_string(),
record_type: "CNAME".to_string(),
address: "example.com.".to_string(),
mx_pref: None,
email_type: None,
ttl: Some("1200".to_string()),
flag: None,
tag: None,
};
let new_hosts = vec![new_host1, new_host2];
let result = client.domains_dns_set_hosts("xylex", "ai", new_hosts).await.unwrap();
info!("Set Hosts Result: {:#?}", result);
// Check if the operation was successful
assert!(
result.get("IsSuccess").and_then(Value::as_bool).unwrap_or(false),
"Expected successful host record setting"
);
}
}