made deserialization in EppClient::transact() generic
This commit is contained in:
parent
89ed02c4d4
commit
4de42f1951
|
@ -14,5 +14,5 @@ async fn main() {
|
||||||
|
|
||||||
let epp_hello = request::Hello::new();
|
let epp_hello = request::Hello::new();
|
||||||
|
|
||||||
client.transact(&epp_hello).await.unwrap();
|
client.transact::<EppGreeting>(&epp_hello).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,13 @@ use std::{str, u32};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use futures::executor::block_on;
|
use futures::executor::block_on;
|
||||||
use std::{error::Error, net::ToSocketAddrs, io as stdio};
|
use std::{error::Error, fmt::Debug, net::ToSocketAddrs, io as stdio};
|
||||||
use tokio_rustls::{TlsConnector, rustls::ClientConfig, webpki::DNSNameRef, client::TlsStream};
|
use tokio_rustls::{TlsConnector, rustls::ClientConfig, webpki::DNSNameRef, client::TlsStream};
|
||||||
use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::ReadHalf, io::WriteHalf};
|
use tokio::{net::TcpStream, io::AsyncWriteExt, io::AsyncReadExt, io::split, io::ReadHalf, io::WriteHalf};
|
||||||
|
|
||||||
use crate::config::{CONFIG, EppClientConnection};
|
use crate::config::{CONFIG, EppClientConnection};
|
||||||
use crate::error;
|
use crate::error;
|
||||||
|
use crate::epp::object::EppObject;
|
||||||
use crate::epp::request::{EppRequest, Login, Logout};
|
use crate::epp::request::{EppRequest, Login, Logout};
|
||||||
use crate::epp::response::EppCommandResponse;
|
use crate::epp::response::EppCommandResponse;
|
||||||
use crate::epp::xml::EppXml;
|
use crate::epp::xml::EppXml;
|
||||||
|
@ -146,12 +147,12 @@ impl EppClient {
|
||||||
let client_tr_id = EppRequest::generate_client_tr_id(&client.credentials.0)?;
|
let client_tr_id = EppRequest::generate_client_tr_id(&client.credentials.0)?;
|
||||||
let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_tr_id.as_str());
|
let login_request = Login::new(&client.credentials.0, &client.credentials.1, client_tr_id.as_str());
|
||||||
|
|
||||||
client.transact(&login_request).await?;
|
client.transact::<EppCommandResponse>(&login_request).await?;
|
||||||
|
|
||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn transact(&mut self, request: &EppRequest) -> Result<String, Box<dyn Error>> {
|
pub async fn transact<E: EppXml + Debug>(&mut self, request: &EppRequest) -> Result<E::Output, Box<dyn Error>> {
|
||||||
let epp_xml = request.serialize()?;
|
let epp_xml = request.serialize()?;
|
||||||
|
|
||||||
println!("Request:\r\n{}", epp_xml);
|
println!("Request:\r\n{}", epp_xml);
|
||||||
|
@ -160,11 +161,11 @@ impl EppClient {
|
||||||
|
|
||||||
println!("Response:\r\n{}", response);
|
println!("Response:\r\n{}", response);
|
||||||
|
|
||||||
let response_obj = EppCommandResponse::deserialize(&response).unwrap();
|
let response_obj = E::deserialize(&response)?;
|
||||||
|
|
||||||
println!("Response:\r\n{:?}", response_obj);
|
println!("Response:\r\n{:?}", response_obj);
|
||||||
|
|
||||||
Ok(response)
|
Ok(response_obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn Error>> {
|
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn Error>> {
|
||||||
|
@ -179,7 +180,7 @@ impl EppClient {
|
||||||
let client_tr_id = EppRequest::generate_client_tr_id(&self.credentials.0).unwrap();
|
let client_tr_id = EppRequest::generate_client_tr_id(&self.credentials.0).unwrap();
|
||||||
let epp_logout = Logout::new(client_tr_id.as_str());
|
let epp_logout = Logout::new(client_tr_id.as_str());
|
||||||
|
|
||||||
self.transact(&epp_logout).await;
|
self.transact::<EppCommandResponse>(&epp_logout).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use quick_xml::de::from_str;
|
use quick_xml::de::from_str;
|
||||||
use quick_xml::se;
|
use quick_xml::se;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use std::error::Error;
|
use std::{error::Error, fmt::Debug};
|
||||||
|
|
||||||
use crate::epp::object::EppObject;
|
use crate::epp::object::EppObject;
|
||||||
use crate::epp::xml::{EppXml, EPP_XML_HEADER};
|
use crate::epp::xml::{EppXml, EPP_XML_HEADER};
|
||||||
|
|
||||||
impl<T: Serialize + DeserializeOwned> EppXml for EppObject<T> {
|
impl<T: Serialize + DeserializeOwned + Debug> EppXml for EppObject<T> {
|
||||||
type Object = EppObject<T>;
|
type Output = EppObject<T>;
|
||||||
|
|
||||||
fn serialize(&self) -> Result<String, Box<dyn Error>> {
|
fn serialize(&self) -> Result<String, Box<dyn Error>> {
|
||||||
let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?);
|
let epp_xml = format!("{}\r\n{}", EPP_XML_HEADER, se::to_string(self)?);
|
||||||
|
@ -15,7 +15,7 @@ impl<T: Serialize + DeserializeOwned> EppXml for EppObject<T> {
|
||||||
Ok(epp_xml)
|
Ok(epp_xml)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize(epp_xml: &str) -> Result<Self::Object, Box<dyn Error>> {
|
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>> {
|
||||||
match from_str(epp_xml) {
|
match from_str(epp_xml) {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
Err(e) => Err(format!("epp-client Deserialization Error: {}", e).into()),
|
Err(e) => Err(format!("epp-client Deserialization Error: {}", e).into()),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::error::Error;
|
use std::{error::Error, fmt::Debug};
|
||||||
|
|
||||||
// use crate::epp::object::EppObject;
|
// use crate::epp::object::EppObject;
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@ pub const EPP_VERSION: &str = "1.0";
|
||||||
pub const EPP_LANG: &str = "en";
|
pub const EPP_LANG: &str = "en";
|
||||||
|
|
||||||
pub trait EppXml {
|
pub trait EppXml {
|
||||||
type Object;
|
type Output: Debug;
|
||||||
|
|
||||||
fn serialize(&self) -> Result<String, Box<dyn Error>>;
|
fn serialize(&self) -> Result<String, Box<dyn Error>>;
|
||||||
fn deserialize(epp_xml: &str) -> Result<Self::Object, Box<dyn Error>>;
|
fn deserialize(epp_xml: &str) -> Result<Self::Output, Box<dyn Error>>;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue