premilinary error handling and code re-arrangement
This commit is contained in:
parent
fca4e4b231
commit
3ea096d62f
|
@ -1,11 +1,11 @@
|
|||
use epp_client::{epp::request::generate_client_tr_id, connection::client::EppClient, connection, epp::xml::EppXml};
|
||||
use epp_client::epp::response::EppGreeting;
|
||||
use epp_client::epp::request::domain::check::EppDomainCheck;
|
||||
use epp_client::epp::response::domain::check::EppDomainCheckResponse;
|
||||
use epp_client::epp::request::contact::check::EppContactCheck;
|
||||
use epp_client::epp::response::contact::check::EppContactCheckResponse;
|
||||
use epp_client::epp::object::data::{PostalInfo, Address, Phone};
|
||||
use epp_client::epp::request::contact::create::EppContactCreate;
|
||||
use epp_client::epp::response::contact::create::EppContactCreateResponse;
|
||||
|
||||
async fn check_domains(client: &mut EppClient) {
|
||||
let domains = vec!["eppdev.com", "hexonet.net"];
|
||||
|
@ -21,7 +21,7 @@ async fn check_contacts(client: &mut EppClient) {
|
|||
client.transact::<_, EppContactCheckResponse>(&contact_check).await.unwrap();
|
||||
}
|
||||
|
||||
async fn create_contact() {
|
||||
async fn create_contact(client: &mut EppClient) {
|
||||
let street = vec!["58", "Orchid Road"];
|
||||
let address = Address::new(street, "Paris", "Paris", "392374", "FR");
|
||||
let postal_info = PostalInfo::new("int", "John Doe", "Acme Widgets", address);
|
||||
|
@ -33,9 +33,9 @@ async fn create_contact() {
|
|||
let mut contact_create = EppContactCreate::new("eppdev-contact-1", "contact@eppdev.net", postal_info, voice, "eppdev-387323", generate_client_tr_id("eppdev").unwrap().as_str());
|
||||
contact_create.set_fax(fax);
|
||||
|
||||
println!("xml: {}", contact_create.serialize().unwrap());
|
||||
// println!("xml: {}", contact_create.serialize().unwrap());
|
||||
|
||||
//client.transact::<EppContactCheck, EppContactCheckResponse>(&contact_check).await.unwrap();
|
||||
client.transact::<_, EppContactCreateResponse>(&contact_create).await.unwrap();
|
||||
}
|
||||
|
||||
async fn hello(client: &mut EppClient) {
|
||||
|
@ -48,9 +48,7 @@ async fn hello(client: &mut EppClient) {
|
|||
async fn main() {
|
||||
let mut client = match EppClient::new("hexonet").await {
|
||||
Ok(client) => {
|
||||
let greeting = client.xml_greeting();
|
||||
let greeting_object = EppGreeting::deserialize(&greeting).unwrap();
|
||||
println!("{:?}", greeting_object);
|
||||
println!("{:?}", client.greeting());
|
||||
client
|
||||
},
|
||||
Err(e) => panic!("Error: {}", e)
|
||||
|
@ -58,9 +56,9 @@ async fn main() {
|
|||
|
||||
// hello(&mut client).await;
|
||||
|
||||
check_domains(&mut client).await;
|
||||
// check_domains(&mut client).await;
|
||||
|
||||
// check_contacts(&mut client).await;
|
||||
|
||||
// create_contact().await;
|
||||
create_contact(&mut client).await;
|
||||
}
|
||||
|
|
|
@ -66,6 +66,8 @@ impl EppClient {
|
|||
|
||||
let response = self.connection.transact(&hello_xml).await?;
|
||||
|
||||
println!("hello response: {}", response);
|
||||
|
||||
Ok(EppGreeting::deserialize(&response)?)
|
||||
}
|
||||
|
||||
|
@ -86,8 +88,7 @@ impl EppClient {
|
|||
Ok(response)
|
||||
} else {
|
||||
let epp_error = EppCommandResponseError::deserialize(&response)?;
|
||||
let epp_error = error::Error::EppCommandError(error::EppCommandError::new(epp_error));
|
||||
Err(epp_error)
|
||||
Err(error::Error::EppCommandError(epp_error))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
pub mod data;
|
||||
|
||||
use std::fmt::Display;
|
||||
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::epp::xml::{EPP_XMLNS, EPP_XMLNS_XSI, EPP_XSI_SCHEMA_LOCATION};
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::{error::Error, fmt::Debug};
|
|||
|
||||
use crate::epp::object::{ElementName, EppObject};
|
||||
use crate::epp::xml::{EppXml, EPP_XML_HEADER};
|
||||
use crate::error;
|
||||
|
||||
impl<T: Serialize + DeserializeOwned + ElementName + Debug> EppXml for EppObject<T> {
|
||||
type Output = EppObject<T>;
|
||||
|
@ -15,10 +16,14 @@ impl<T: Serialize + DeserializeOwned + ElementName + Debug> EppXml for EppObject
|
|||
Ok(epp_xml)
|
||||
}
|
||||
|
||||
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>> {
|
||||
fn deserialize(epp_xml: &str) -> Result<Self::Output, error::Error> {
|
||||
let mut object: Self::Output = match from_str(epp_xml) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("epp-client Deserialization Error: {}", e).into()),
|
||||
Err(e) => {
|
||||
return Err(error::Error::EppDeserializationError(
|
||||
format!("epp-client Deserialization Error: {}", e).to_string(),
|
||||
))
|
||||
}
|
||||
};
|
||||
// object.xml = Some(epp_xml.to_string());
|
||||
Ok(object)
|
||||
|
|
|
@ -15,7 +15,7 @@ pub struct DomainList {
|
|||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, ElementName)]
|
||||
#[element_name(name = "checkr")]
|
||||
#[element_name(name = "check")]
|
||||
pub struct DomainCheck {
|
||||
#[serde(rename = "check")]
|
||||
list: DomainList,
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
pub mod check;
|
||||
pub mod create;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::epp::object::{EppObject, StringValue};
|
||||
use crate::epp::response::CommandResponse;
|
||||
|
||||
pub type EppContactCreateResponse = EppObject<CommandResponse<ContactCreateResult>>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ContactCreateData {
|
||||
pub id: StringValue,
|
||||
#[serde(rename = "crDate")]
|
||||
pub created_at: StringValue,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ContactCreateResult {
|
||||
#[serde(rename = "creData")]
|
||||
pub check_data: ContactCreateData,
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
use std::{error::Error, fmt::Debug};
|
||||
|
||||
// use crate::epp::object::EppObject;
|
||||
use crate::error;
|
||||
|
||||
pub const EPP_XML_HEADER: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"#;
|
||||
pub const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0";
|
||||
|
@ -17,5 +17,5 @@ pub trait EppXml {
|
|||
type Output: Debug;
|
||||
|
||||
fn serialize(&self) -> Result<String, Box<dyn Error>>;
|
||||
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>>;
|
||||
fn deserialize(epp_xml: &str) -> Result<Self::Output, error::Error>;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ use std::fmt::Display;
|
|||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
EppConnectionError(std::io::Error),
|
||||
EppCommandError(EppCommandError),
|
||||
EppCommandError(EppCommandResponseError),
|
||||
EppDeserializationError(String),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
|
@ -15,35 +16,21 @@ pub struct EppCommandError {
|
|||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl std::error::Error for EppCommandError {}
|
||||
|
||||
impl Display for EppCommandError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"epp-client EppCommandError: {}",
|
||||
self.epp_error.data.result.message
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl EppCommandError {
|
||||
pub fn new(epp_error: EppCommandResponseError) -> EppCommandError {
|
||||
EppCommandError {
|
||||
epp_error: epp_error,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "epp-client Exception: {:?}", self)
|
||||
match self {
|
||||
Error::EppCommandError(e) => {
|
||||
write!(f, "epp-client EppCommandError: {}", e.data.result.message)
|
||||
}
|
||||
Error::Other(e) => write!(f, "epp-client Exception: {}", e),
|
||||
_ => write!(f, "epp-client Exception: {:?}", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::boxed::Box<dyn std::error::Error>> for Error {
|
||||
fn from(e: std::boxed::Box<dyn std::error::Error>) -> Self {
|
||||
Self::Other(format!("{}", e).to_string())
|
||||
Self::Other(format!("{:?}", e).to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue