Redirect CheckResponse deserialization to improve interface

This commit is contained in:
Dirkjan Ochtman 2022-01-27 12:35:50 +01:00 committed by masalachai
parent a5d6643d6f
commit fdec3f29fc
6 changed files with 49 additions and 31 deletions

View File

@ -41,7 +41,7 @@ impl Extension for NoExtension {
/// Type that represents the <name> tag for host check response /// Type that represents the <name> tag for host check response
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Available { struct Available {
/// The resource name /// The resource name
#[serde(rename = "$value")] #[serde(rename = "$value")]
pub id: StringValue<'static>, pub id: StringValue<'static>,
@ -52,7 +52,7 @@ pub struct Available {
/// Type that represents the &lt;cd&gt; tag for domain check response /// Type that represents the &lt;cd&gt; tag for domain check response
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct CheckResponseDataItem { struct CheckResponseDataItem {
/// Data under the &lt;name&gt; tag /// Data under the &lt;name&gt; tag
#[serde(rename = "name", alias = "id")] #[serde(rename = "name", alias = "id")]
pub resource: Available, pub resource: Available,
@ -62,7 +62,7 @@ pub struct CheckResponseDataItem {
/// Type that represents the &lt;chkData&gt; tag for host check response /// Type that represents the &lt;chkData&gt; tag for host check response
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct CheckData { struct CheckData {
/// Data under the &lt;cd&gt; tag /// Data under the &lt;cd&gt; tag
#[serde(rename = "cd")] #[serde(rename = "cd")]
pub list: Vec<CheckResponseDataItem>, pub list: Vec<CheckResponseDataItem>,
@ -70,12 +70,42 @@ pub struct CheckData {
/// Type that represents the &lt;resData&gt; tag for host check response /// Type that represents the &lt;resData&gt; tag for host check response
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct CheckResponse { struct DeserializedCheckResponse {
/// Data under the &lt;chkData&gt; tag /// Data under the &lt;chkData&gt; tag
#[serde(rename = "chkData")] #[serde(rename = "chkData")]
pub check_data: CheckData, pub check_data: CheckData,
} }
#[derive(Debug)]
pub struct Checked {
pub id: String,
pub available: bool,
pub reason: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(from = "DeserializedCheckResponse")]
pub struct CheckResponse {
pub list: Vec<Checked>,
}
impl From<DeserializedCheckResponse> for CheckResponse {
fn from(rsp: DeserializedCheckResponse) -> Self {
Self {
list: rsp
.check_data
.list
.into_iter()
.map(|item| Checked {
id: item.resource.id.0.into_owned(),
available: item.resource.available,
reason: item.reason.map(|r| r.0.into_owned()),
})
.collect(),
}
}
}
/// The <option> type in EPP XML login requests /// The <option> type in EPP XML login requests
#[derive(Serialize, Deserialize, Debug, PartialEq)] #[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename = "options")] #[serde(rename = "options")]

View File

@ -74,16 +74,10 @@ mod tests {
assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.result.message, SUCCESS_MSG.into());
assert_eq!( assert_eq!(results.list[0].id, "eppdev-contact-1");
results.check_data.list[0].resource.id, assert!(!results.list[0].available);
"eppdev-contact-1".into() assert_eq!(results.list[1].id, "eppdev-contact-2");
); assert!(results.list[1].available);
assert!(!results.check_data.list[0].resource.available);
assert_eq!(
results.check_data.list[1].resource.id,
"eppdev-contact-2".into()
);
assert!(results.check_data.list[1].resource.available);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
} }

View File

@ -75,10 +75,10 @@ mod tests {
assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.result.message, SUCCESS_MSG.into());
assert_eq!(result.check_data.list[0].resource.id, "eppdev.com".into()); assert_eq!(result.list[0].id, "eppdev.com");
assert!(result.check_data.list[0].resource.available); assert!(result.list[0].available);
assert_eq!(result.check_data.list[1].resource.id, "eppdev.net".into()); assert_eq!(result.list[1].id, "eppdev.net");
assert!(!result.check_data.list[1].resource.available); assert!(!result.list[1].available);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
} }

View File

@ -79,16 +79,10 @@ mod tests {
assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
assert_eq!(object.result.message, SUCCESS_MSG.into()); assert_eq!(object.result.message, SUCCESS_MSG.into());
assert_eq!( assert_eq!(result.list[0].id, "host1.eppdev-1.com");
result.check_data.list[0].resource.id, assert!(result.list[0].available);
"host1.eppdev-1.com".into() assert_eq!(result.list[1].id, "ns1.testing.com");
); assert!(!result.list[1].available);
assert!(result.check_data.list[0].resource.available);
assert_eq!(
result.check_data.list[1].resource.id,
"ns1.testing.com".into()
);
assert!(!result.check_data.list[1].resource.available);
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into()); assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
assert_eq!(object.tr_ids.server_tr_id, SVTRID.into()); assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
} }

View File

@ -71,9 +71,9 @@
//! let response = client.transact(&domain_check, "transaction-id").await.unwrap(); //! let response = client.transact(&domain_check, "transaction-id").await.unwrap();
//! //!
//! // print the availability results //! // print the availability results
//! response.res_data.unwrap().check_data.list //! response.res_data.unwrap().list
//! .iter() //! .iter()
//! .for_each(|chk| println!("Domain: {}, Available: {}", chk.resource.id, chk.resource.available)); //! .for_each(|chk| println!("Domain: {}, Available: {}", chk.id, chk.available));
//! //!
//! // Close the connection //! // Close the connection
//! client.transact(&Logout, "transaction-id").await.unwrap(); //! client.transact(&Logout, "transaction-id").await.unwrap();

View File

@ -107,5 +107,5 @@ async fn client() {
assert_eq!(rsp.result.code, ResultCode::CommandCompletedSuccessfully); assert_eq!(rsp.result.code, ResultCode::CommandCompletedSuccessfully);
let result = rsp.res_data().unwrap(); let result = rsp.res_data().unwrap();
assert_eq!(result.check_data.list[0].resource.id, "eppdev.com".into()); assert_eq!(result.list[0].id, "eppdev.com");
} }