Skip to content

Commit

Permalink
addresses #89 by moving all client stuff to the client crate
Browse files Browse the repository at this point in the history
  • Loading branch information
anewton1998 committed Dec 5, 2024
1 parent 05853b4 commit df52ae4
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 132 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion icann-rdap-cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::process::{ExitCode, Termination};

use icann_rdap_client::iana_request::IanaResponseError;
use icann_rdap_client::RdapClientError;
use icann_rdap_common::iana::IanaResponseError;
use minus::MinusError;
use thiserror::Error;

Expand Down
4 changes: 2 additions & 2 deletions icann-rdap-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bootstrap::BootstrapType;
use clap::builder::styling::AnsiColor;
use clap::builder::Styles;
use icann_rdap_client::client::create_client;
use icann_rdap_client::client::ClientConfig;
use icann_rdap_common::check::CheckClass;
use icann_rdap_common::client::create_client;
use icann_rdap_common::client::ClientConfig;
use query::InrBackupBootstrap;
use query::ProcessType;
use query::ProcessingParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use reqwest::{
Client,
};

use crate::media_types::{JSON_MEDIA_TYPE, RDAP_MEDIA_TYPE};
use icann_rdap_common::media_types::{JSON_MEDIA_TYPE, RDAP_MEDIA_TYPE};

#[cfg(not(target_arch = "wasm32"))]
use crate::VERSION;
use icann_rdap_common::VERSION;

lazy_static! {
static ref ACCEPT_HEADER_VALUES: String = format!("{RDAP_MEDIA_TYPE}, {JSON_MEDIA_TYPE}");
Expand Down
87 changes: 87 additions & 0 deletions icann-rdap-client/src/iana_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//! The IANA RDAP Bootstrap Registries.
use icann_rdap_common::httpdata::HttpData;
use icann_rdap_common::iana::IanaRegistry;
use icann_rdap_common::iana::IanaRegistryType;
use icann_rdap_common::iana::RdapBootstrapRegistry;
use reqwest::header::ACCESS_CONTROL_ALLOW_ORIGIN;
use reqwest::header::STRICT_TRANSPORT_SECURITY;
use reqwest::{
header::{CACHE_CONTROL, CONTENT_TYPE, EXPIRES, LOCATION},
Client,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct IanaResponse {
pub registry: IanaRegistry,
pub registry_type: IanaRegistryType,
pub http_data: HttpData,
}

#[derive(Debug, Error)]
pub enum IanaResponseError {
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
}

pub async fn iana_request(
registry_type: IanaRegistryType,
client: &Client,
) -> Result<IanaResponse, IanaResponseError> {
let url = registry_type.url();
let response = client.get(url).send().await?.error_for_status()?;
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(|value| value.to_str().unwrap().to_string());
let expires = response
.headers()
.get(EXPIRES)
.map(|value| value.to_str().unwrap().to_string());
let cache_control = response
.headers()
.get(CACHE_CONTROL)
.map(|value| value.to_str().unwrap().to_string());
let location = response
.headers()
.get(LOCATION)
.map(|value| value.to_str().unwrap().to_string());
let access_control_allow_origin = response
.headers()
.get(ACCESS_CONTROL_ALLOW_ORIGIN)
.map(|value| value.to_str().unwrap().to_string());
let strict_transport_security = response
.headers()
.get(STRICT_TRANSPORT_SECURITY)
.map(|value| value.to_str().unwrap().to_string());
let status_code = response.status().as_u16();
let content_length = response.content_length();
let url = response.url().to_owned();
let text = response.text().await?;
let json: RdapBootstrapRegistry = serde_json::from_str(&text)?;
let http_data = HttpData::now()
.scheme(url.scheme())
.host(
url.host_str()
.expect("URL has no host. This shouldn't happen.")
.to_owned(),
)
.status_code(status_code)
.and_location(location)
.and_content_length(content_length)
.and_content_type(content_type)
.and_expires(expires)
.and_cache_control(cache_control)
.and_access_control_allow_origin(access_control_allow_origin)
.and_strict_transport_security(strict_transport_security)
.build();
Ok(IanaResponse {
registry: IanaRegistry::RdapBootstrapRegistry(json),
registry_type,
http_data,
})
}
15 changes: 8 additions & 7 deletions icann-rdap-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
#![doc = include_str!("../README.md")]
use std::{fmt::Display, sync::PoisonError};

use iana_request::IanaResponseError;
use icann_rdap_common::{
dns_types::DomainNameError,
httpdata::HttpData,
iana::{BootstrapRegistryError, IanaResponseError},
dns_types::DomainNameError, httpdata::HttpData, iana::BootstrapRegistryError,
response::RdapResponseError,
};
use thiserror::Error;

pub mod client;
pub mod gtld;
pub mod iana_request;
pub mod md;
pub mod query;
pub mod registered_redactions;
pub mod request;

#[doc(inline)]
pub use crate::client::create_client;
#[doc(inline)]
pub use crate::client::ClientConfig;
#[doc(inline)]
pub use crate::query::bootstrap::MemoryBootstrapStore;
#[doc(inline)]
Expand All @@ -27,10 +32,6 @@ pub use crate::query::request::rdap_bootstrapped_request;
pub use crate::query::request::rdap_request;
#[doc(inline)]
pub use crate::query::request::rdap_url_request;
#[doc(inline)]
pub use icann_rdap_common::client::create_client;
#[doc(inline)]
pub use icann_rdap_common::client::ClientConfig;

/// Error returned by RDAP client functions and methods.
#[derive(Error, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions icann-rdap-client/src/query/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::sync::{Arc, RwLock};
use icann_rdap_common::{
httpdata::HttpData,
iana::{
get_preferred_url, iana_request, BootstrapRegistry, BootstrapRegistryError, IanaRegistry,
get_preferred_url, BootstrapRegistry, BootstrapRegistryError, IanaRegistry,
IanaRegistryType,
},
};
use reqwest::Client;

use crate::RdapClientError;
use crate::{iana_request::iana_request, RdapClientError};

use super::qtype::QueryType;

Expand Down
12 changes: 6 additions & 6 deletions icann-rdap-client/src/query/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use super::{
/// * client - a reference to a [reqwest::Client].
///
/// ```no_run
/// use icann_rdap_common::client::ClientConfig;
/// use icann_rdap_common::client::create_client;
/// use icann_rdap_client::client::ClientConfig;
/// use icann_rdap_client::client::create_client;
/// use icann_rdap_client::query::request::rdap_url_request;
/// use icann_rdap_client::RdapClientError;
/// use std::str::FromStr;
Expand Down Expand Up @@ -124,8 +124,8 @@ pub async fn rdap_url_request(url: &str, client: &Client) -> Result<ResponseData
/// * client - a reference to a [reqwest::Client].
///
/// ```no_run
/// use icann_rdap_common::client::ClientConfig;
/// use icann_rdap_common::client::create_client;
/// use icann_rdap_client::client::ClientConfig;
/// use icann_rdap_client::client::create_client;
/// use icann_rdap_client::query::request::rdap_request;
/// use icann_rdap_client::query::qtype::QueryType;
/// use icann_rdap_client::RdapClientError;
Expand Down Expand Up @@ -179,8 +179,8 @@ pub async fn rdap_request(
/// registry with each request which is most likely not the desired behavior.
///
/// ```no_run
/// use icann_rdap_common::client::ClientConfig;
/// use icann_rdap_common::client::create_client;
/// use icann_rdap_client::client::ClientConfig;
/// use icann_rdap_client::client::create_client;
/// use icann_rdap_client::query::request::rdap_bootstrapped_request;
/// use icann_rdap_client::query::qtype::QueryType;
/// use icann_rdap_client::query::bootstrap::MemoryBootstrapStore;
Expand Down
1 change: 0 additions & 1 deletion icann-rdap-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ idna.workspace = true
ipnet.workspace = true
lazy_static.workspace = true
prefix-trie.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
strum.workspace = true
Expand Down
83 changes: 1 addition & 82 deletions icann-rdap-common/src/iana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
use ipnet::Ipv4Net;
use ipnet::Ipv6Net;
use prefix_trie::PrefixMap;
use reqwest::header::ACCESS_CONTROL_ALLOW_ORIGIN;
use reqwest::header::STRICT_TRANSPORT_SECURITY;
use reqwest::{
header::{CACHE_CONTROL, CONTENT_TYPE, EXPIRES, LOCATION},
Client,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::httpdata::HttpData;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum IanaRegistryType {
RdapBootstrapDns,
Expand Down Expand Up @@ -89,7 +81,7 @@ impl BootstrapRegistry for IanaRegistry {
.ok_or(BootstrapRegistryError::EmptyService)?;
for tld in tlds {
// if the ldh domain ends with the tld or the tld is the empty string which means the root
if ldh.ends_with(tld) || tld.eq("") {
if ldh.ends_with(tld) || tld.is_empty() {
let urls = service.last().ok_or(BootstrapRegistryError::EmptyUrlSet)?;
let longest = longest_match.get_or_insert_with(|| (tld.len(), urls.to_owned()));
if longest.0 < tld.len() {
Expand Down Expand Up @@ -215,79 +207,6 @@ pub fn get_preferred_url(urls: Vec<String>) -> Result<String, BootstrapRegistryE
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct IanaResponse {
pub registry: IanaRegistry,
pub registry_type: IanaRegistryType,
pub http_data: HttpData,
}

#[derive(Debug, Error)]
pub enum IanaResponseError {
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
}

pub async fn iana_request(
registry_type: IanaRegistryType,
client: &Client,
) -> Result<IanaResponse, IanaResponseError> {
let url = registry_type.url();
let response = client.get(url).send().await?.error_for_status()?;
let content_type = response
.headers()
.get(CONTENT_TYPE)
.map(|value| value.to_str().unwrap().to_string());
let expires = response
.headers()
.get(EXPIRES)
.map(|value| value.to_str().unwrap().to_string());
let cache_control = response
.headers()
.get(CACHE_CONTROL)
.map(|value| value.to_str().unwrap().to_string());
let location = response
.headers()
.get(LOCATION)
.map(|value| value.to_str().unwrap().to_string());
let access_control_allow_origin = response
.headers()
.get(ACCESS_CONTROL_ALLOW_ORIGIN)
.map(|value| value.to_str().unwrap().to_string());
let strict_transport_security = response
.headers()
.get(STRICT_TRANSPORT_SECURITY)
.map(|value| value.to_str().unwrap().to_string());
let status_code = response.status().as_u16();
let content_length = response.content_length();
let url = response.url().to_owned();
let text = response.text().await?;
let json: RdapBootstrapRegistry = serde_json::from_str(&text)?;
let http_data = HttpData::now()
.scheme(url.scheme())
.host(
url.host_str()
.expect("URL has no host. This shouldn't happen.")
.to_owned(),
)
.status_code(status_code)
.and_location(location)
.and_content_length(content_length)
.and_content_type(content_type)
.and_expires(expires)
.and_cache_control(cache_control)
.and_access_control_allow_origin(access_control_allow_origin)
.and_strict_transport_security(strict_transport_security)
.build();
Ok(IanaResponse {
registry: IanaRegistry::RdapBootstrapRegistry(json),
registry_type,
http_data,
})
}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
Expand Down
1 change: 0 additions & 1 deletion icann-rdap-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(rustdoc::bare_urls)]
#![doc = include_str!("../README.md")]
pub mod check;
pub mod client;
pub mod contact;
pub mod dns_types;
pub mod httpdata;
Expand Down
4 changes: 2 additions & 2 deletions icann-rdap-srv/src/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{path::PathBuf, time::Duration};

use icann_rdap_client::{create_client, iana_request::iana_request, ClientConfig};
use icann_rdap_common::{
client::{create_client, ClientConfig},
httpdata::HttpData,
iana::{iana_request, IanaRegistry, IanaRegistryType},
iana::{IanaRegistry, IanaRegistryType},
};
use reqwest::Client;
use tokio::{
Expand Down
7 changes: 2 additions & 5 deletions icann-rdap-srv/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ use axum::{
};
use envmnt::errors::EnvmntError;
use http::StatusCode;
use icann_rdap_client::RdapClientError;
use icann_rdap_common::{
iana::IanaResponseError,
response::{types::Common, RdapResponse, RdapResponseError},
};
use icann_rdap_client::{iana_request::IanaResponseError, RdapClientError};
use icann_rdap_common::response::{types::Common, RdapResponse, RdapResponseError};
use ipnet::PrefixLenError;
use thiserror::Error;

Expand Down
2 changes: 1 addition & 1 deletion icann-rdap-srv/tests/integration/srv/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_snake_case)]

use icann_rdap_client::client::{create_client, ClientConfig};
use icann_rdap_client::query::{qtype::QueryType, request::rdap_request};
use icann_rdap_common::client::{create_client, ClientConfig};
use icann_rdap_srv::storage::{
data::{AutnumId, DomainId, EntityId, NetworkId, NetworkIdType},
StoreOps,
Expand Down
8 changes: 3 additions & 5 deletions icann-rdap-srv/tests/integration/srv/domain.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![allow(non_snake_case)]

use icann_rdap_client::{
create_client,
query::{qtype::QueryType, request::rdap_request},
RdapClientError,
};
use icann_rdap_common::{
client::{create_client, ClientConfig},
response::domain::Domain,
ClientConfig, RdapClientError,
};
use icann_rdap_common::response::domain::Domain;
use icann_rdap_srv::storage::{CommonConfig, StoreOps};

use crate::test_jig::SrvTestJig;
Expand Down
Loading

0 comments on commit df52ae4

Please sign in to comment.