2021-07-24 20:15:59 +00:00
|
|
|
//! Manages sending/receiving EppObject request and responses to the registry connection
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
2021-11-05 22:14:05 +00:00
|
|
|
//! ```no_run
|
|
|
|
//! use std::collections::HashMap;
|
|
|
|
//!
|
2021-12-01 13:43:23 +00:00
|
|
|
//! use epp_client::config::{EppClientConfig, RegistryConfig};
|
2021-07-24 20:15:59 +00:00
|
|
|
//! use epp_client::EppClient;
|
2021-11-26 22:21:38 +00:00
|
|
|
//! use epp_client::domain::check::DomainCheck;
|
|
|
|
//! use epp_client::common::NoExtension;
|
2021-07-24 20:15:59 +00:00
|
|
|
//!
|
|
|
|
//! #[tokio::main]
|
|
|
|
//! async fn main() {
|
2021-11-05 22:14:05 +00:00
|
|
|
//!
|
|
|
|
//! // Create a config
|
2021-12-01 13:43:23 +00:00
|
|
|
//! let mut registry: HashMap<String, RegistryConfig> = HashMap::new();
|
2021-11-05 22:14:05 +00:00
|
|
|
//! registry.insert(
|
|
|
|
//! "registry_name".to_owned(),
|
2021-12-01 13:43:23 +00:00
|
|
|
//! RegistryConfig {
|
2021-11-05 22:14:05 +00:00
|
|
|
//! host: "example.com".to_owned(),
|
|
|
|
//! port: 700,
|
|
|
|
//! tls_files: None,
|
|
|
|
//! },
|
|
|
|
//! );
|
|
|
|
//! let config = EppClientConfig { registry };
|
|
|
|
//!
|
|
|
|
//! // Create an instance of EppClient, passing the config and the registry you want to connect to
|
|
|
|
//! let mut client = match EppClient::new(&config, "registry_name").await {
|
|
|
|
//! Ok(client) => client,
|
|
|
|
//! Err(e) => panic!("Failed to create EppClient: {}", e)
|
|
|
|
//! };
|
|
|
|
//!
|
|
|
|
//! // Make a EPP Hello call to the registry
|
|
|
|
//! let greeting = client.hello().await.unwrap();
|
|
|
|
//! println!("{:?}", greeting);
|
|
|
|
//!
|
|
|
|
//! // Execute an EPP Command against the registry with distinct request and response objects
|
2021-12-09 09:17:00 +00:00
|
|
|
//! let domain_check = DomainCheck::new(vec!["eppdev.com", "eppdev.net"]);
|
2021-12-07 18:19:05 +00:00
|
|
|
//! let response = client.transact(&domain_check, "transaction-id").await.unwrap();
|
2021-11-05 22:14:05 +00:00
|
|
|
//! println!("{:?}", response);
|
|
|
|
//!
|
2021-07-24 20:15:59 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2021-12-09 09:17:00 +00:00
|
|
|
use std::error::Error;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
2021-12-09 09:17:00 +00:00
|
|
|
use crate::common::NoExtension;
|
2021-10-22 00:11:24 +00:00
|
|
|
use crate::config::EppClientConfig;
|
2021-12-13 15:31:51 +00:00
|
|
|
use crate::connection::EppConnection;
|
2021-11-02 21:34:13 +00:00
|
|
|
use crate::error;
|
2021-12-08 15:38:58 +00:00
|
|
|
use crate::hello::{Greeting, GreetingDocument, HelloDocument};
|
2021-12-09 09:17:00 +00:00
|
|
|
use crate::request::{Command, Extension, Transaction};
|
2021-11-30 23:39:54 +00:00
|
|
|
use crate::response::Response;
|
2021-11-26 19:36:28 +00:00
|
|
|
use crate::xml::EppXml;
|
2021-12-01 13:21:43 +00:00
|
|
|
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Instances of the EppClient type are used to transact with the registry.
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Once initialized, the EppClient instance can serialize EPP requests to XML and send them
|
|
|
|
/// to the registry and deserialize the XML responses from the registry to local types
|
2021-07-22 14:01:46 +00:00
|
|
|
pub struct EppClient {
|
|
|
|
connection: EppConnection,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EppClient {
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Creates a new EppClient object and does an EPP Login to a given registry to become ready
|
|
|
|
/// for subsequent transactions on this client instance
|
2021-10-22 00:11:24 +00:00
|
|
|
pub async fn new(
|
|
|
|
config: &EppClientConfig,
|
|
|
|
registry: &str,
|
|
|
|
) -> Result<EppClient, Box<dyn Error>> {
|
|
|
|
let registry_creds = match config.registry(registry) {
|
|
|
|
Some(creds) => creds,
|
|
|
|
None => return Err(format!("missing credentials for {}", registry).into()),
|
|
|
|
};
|
|
|
|
|
2021-12-13 15:31:06 +00:00
|
|
|
Ok(EppClient {
|
|
|
|
connection: EppConnection::connect(registry.to_string(), registry_creds).await?,
|
|
|
|
})
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 23:39:54 +00:00
|
|
|
/// Executes an EPP Hello call and returns the response as an `Greeting`
|
2021-12-01 18:06:05 +00:00
|
|
|
pub async fn hello(&mut self) -> Result<Greeting, Box<dyn Error>> {
|
2021-12-08 15:38:58 +00:00
|
|
|
let hello_xml = HelloDocument::default().serialize()?;
|
2021-07-22 14:01:46 +00:00
|
|
|
|
|
|
|
let response = self.connection.transact(&hello_xml).await?;
|
|
|
|
|
2021-12-08 15:38:58 +00:00
|
|
|
Ok(GreetingDocument::deserialize(&response)?.data)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 18:19:05 +00:00
|
|
|
pub async fn transact<'a, C: 'a, E: 'a>(
|
2021-11-26 21:50:22 +00:00
|
|
|
&mut self,
|
2021-12-07 18:19:05 +00:00
|
|
|
data: impl Into<RequestData<'a, C, E>> + 'a,
|
2021-11-26 21:50:22 +00:00
|
|
|
id: &str,
|
2021-12-09 09:17:00 +00:00
|
|
|
) -> Result<Response<C::Response, E::Response>, error::Error>
|
2021-11-26 21:50:22 +00:00
|
|
|
where
|
2021-12-09 09:17:00 +00:00
|
|
|
C: Transaction<E> + Command,
|
|
|
|
E: Extension,
|
2021-11-26 21:50:22 +00:00
|
|
|
{
|
2021-12-09 09:17:00 +00:00
|
|
|
let data = data.into();
|
|
|
|
let epp_xml = <C as Transaction<E>>::serialize_request(data.command, data.extension, id)?;
|
2021-11-26 21:50:22 +00:00
|
|
|
|
|
|
|
let response = self.connection.transact(&epp_xml).await?;
|
|
|
|
|
2021-12-09 09:17:00 +00:00
|
|
|
C::deserialize_response(&response)
|
2021-11-26 21:50:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 20:15:59 +00:00
|
|
|
/// Accepts raw EPP XML and returns the raw EPP XML response to it.
|
2021-07-26 19:27:18 +00:00
|
|
|
/// Not recommended for direct use but sometimes can be useful for debugging
|
2021-07-22 14:01:46 +00:00
|
|
|
pub async fn transact_xml(&mut self, xml: &str) -> Result<String, Box<dyn Error>> {
|
2021-10-27 22:45:32 +00:00
|
|
|
self.connection.transact(xml).await
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:34:01 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection in raw xml form
|
2021-07-22 14:01:46 +00:00
|
|
|
pub fn xml_greeting(&self) -> String {
|
2021-10-27 22:45:32 +00:00
|
|
|
String::from(&self.connection.greeting)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 23:39:54 +00:00
|
|
|
/// Returns the greeting received on establishment of the connection as an `Greeting`
|
2021-12-01 18:06:05 +00:00
|
|
|
pub fn greeting(&self) -> Result<Greeting, error::Error> {
|
2021-12-08 15:38:58 +00:00
|
|
|
GreetingDocument::deserialize(&self.connection.greeting).map(|obj| obj.data)
|
2021-07-22 14:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 09:17:00 +00:00
|
|
|
|
2021-12-07 18:19:05 +00:00
|
|
|
pub struct RequestData<'a, C, E> {
|
|
|
|
command: &'a C,
|
|
|
|
extension: Option<&'a E>,
|
2021-12-09 09:17:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 18:19:05 +00:00
|
|
|
impl<'a, C: Command> From<&'a C> for RequestData<'a, C, NoExtension> {
|
|
|
|
fn from(command: &'a C) -> Self {
|
2021-12-09 09:17:00 +00:00
|
|
|
Self {
|
|
|
|
command,
|
|
|
|
extension: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 18:19:05 +00:00
|
|
|
impl<'a, C: Command, E: Extension> From<(&'a C, &'a E)> for RequestData<'a, C, E> {
|
|
|
|
fn from((command, extension): (&'a C, &'a E)) -> Self {
|
2021-12-09 09:17:00 +00:00
|
|
|
Self {
|
|
|
|
command,
|
|
|
|
extension: Some(extension),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|