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
//! ### `domains.getContacts` Implementation
//!
//! This module provides the implementation for the `domains.getContacts` method of the NameCheap API.
//!
//! It retrieves the contact information for a specified domain.
//!
use serde_json::{ Value, json };
use std::error::Error;
use tracing::{ info, error };
// crate imports
use crate::{ NameCheapClient, Contact };
use crate::utils::request_builder::Request;
use crate::response::parse_value::parse_string;
impl NameCheapClient {
/// - `domains.getContacts`: Gets contact information for the specified domain
/// Gets contact information for the specified 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 contacts = client.domains_get_contacts("example.com").await.unwrap();
/// println!("Domain Contacts: {:?}", contacts);
/// }
/// ```
pub async fn domains_get_contacts(&self, domain_name: &str) -> Result<Value, Box<dyn Error>> {
let command: String = "namecheap.domains.getContacts".to_string();
let response: Value = Request::new(
self.clone(),
command,
None,
Some(domain_name.to_string()),
None, // params
).send().await?;
info!("Response: {:#?}", response);
// Extract contacts from the response
if let Some(api_response) = response.get("ApiResponse") {
if let Some(command_response) = api_response.get("CommandResponse") {
if let Some(result) = command_response.get("DomainContactsResult") {
let mut contacts: Value = json!({});
let mut whois_guard_contacts: Value = json!({});
let has_whois_guard: bool = result.get("WhoisGuardContact").is_some();
// Process each contact type
for contact_type in &["Registrant", "Tech", "Admin", "AuxBilling"] {
if let Some(contact_info) = result.get(contact_type) {
// Create a Contact struct for each contact type
let contact: Contact = Contact {
type_: contact_type.to_string(),
first_name: parse_string(contact_info, "FirstName", ""),
last_name: parse_string(contact_info, "LastName", ""),
address_1: parse_string(contact_info, "Address1", ""),
address_2: parse_string(contact_info, "Address2", ""),
city: parse_string(contact_info, "City", ""),
state_province: parse_string(contact_info, "StateProvince", ""),
state_province_choice: parse_string(
contact_info,
"StateProvinceChoice",
""
),
postal_code: parse_string(contact_info, "PostalCode", ""),
country: parse_string(contact_info, "Country", ""),
phone: parse_string(contact_info, "Phone", ""),
phone_ext: parse_string(contact_info, "PhoneExt", ""),
fax: parse_string(contact_info, "Fax", ""),
email_address: parse_string(contact_info, "EmailAddress", ""),
organization_name: parse_string(
contact_info,
"OrganizationName",
""
),
job_title: parse_string(contact_info, "JobTitle", ""),
read_only: parse_string(contact_info, "read_only", "false") ==
"true",
};
contacts[contact_type.to_lowercase()] = json!(contact);
}
// Process WhoisGuard contacts if available
if has_whois_guard {
if let Some(whois_guard) = result.get("WhoisGuardContact") {
if let Some(whois_contact_info) = whois_guard.get(contact_type) {
let whois_contact: Contact = Contact {
type_: contact_type.to_string(),
first_name: parse_string(
whois_contact_info,
"FirstName",
""
),
last_name: parse_string(whois_contact_info, "LastName", ""),
address_1: parse_string(whois_contact_info, "Address1", ""),
address_2: parse_string(whois_contact_info, "Address2", ""),
city: parse_string(whois_contact_info, "City", ""),
state_province: parse_string(
whois_contact_info,
"StateProvince",
""
),
state_province_choice: parse_string(
whois_contact_info,
"StateProvinceChoice",
""
),
postal_code: parse_string(
whois_contact_info,
"PostalCode",
""
),
country: parse_string(whois_contact_info, "Country", ""),
phone: parse_string(whois_contact_info, "Phone", ""),
phone_ext: parse_string(whois_contact_info, "PhoneExt", ""),
fax: parse_string(whois_contact_info, "Fax", ""),
email_address: parse_string(
whois_contact_info,
"EmailAddress",
""
),
organization_name: parse_string(
whois_contact_info,
"OrganizationName",
""
),
job_title: parse_string(whois_contact_info, "JobTitle", ""),
read_only: parse_string(
whois_contact_info,
"read_only",
"false"
) == "true",
};
whois_guard_contacts[contact_type.to_lowercase()] =
json!(whois_contact);
}
}
}
}
// Add domain information and WhoisGuard contacts if available
let mut result_json: Value =
json!({
"contacts": contacts,
"domain": parse_string(result, "domain", ""),
"domain_id": parse_string(result, "domainnameid", "")
});
if has_whois_guard {
result_json["whois_guard_contacts"] = whois_guard_contacts;
}
return Ok(result_json);
}
}
}
// Return if no contacts found
error!("Failed to extract contact information for domain: {}", domain_name);
Ok(
json!({
"error": true,
"message": format!("Failed to extract contact information for domain: {}", domain_name)
})
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
#[tokio::test]
async fn test_domains_get_contacts() {
dotenv().ok();
let client: Result<NameCheapClient, Box<dyn Error>> = NameCheapClient::new_from_env();
let client: NameCheapClient = client.unwrap();
let contacts: Value = client.domains_get_contacts("xylex.ai").await.unwrap();
info!("contacts: {:#?}", contacts);
// Basic validation
assert!(contacts.get("contacts").is_some());
assert!(contacts.get("domain").is_some());
assert!(contacts.get("domain_id").is_some());
assert!(contacts.get("whois_guard_contacts").is_some());
}
}