diff --git a/Cargo.lock b/Cargo.lock index 772199f..881c44a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,15 +99,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "construct" -version = "0.1.0" -dependencies = [ - "construct-http", - "reqwest", -] - -[[package]] -name = "construct-http" -version = "0.1.0" +version = "1.2.0" dependencies = [ "async-trait", "construct-models", diff --git a/Cargo.toml b/Cargo.toml index 6ef24f2..1412c83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "1" -members = ["construct", "construct-http", "construct-models", "examples"] +members = ["construct", "construct-models", "examples"] [workspace.package] authors = ["perp"] diff --git a/construct-http/Cargo.toml b/construct-http/Cargo.toml deleted file mode 100644 index cfb49d5..0000000 --- a/construct-http/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "construct-http" -version = "0.1.0" -edition = "2021" -authors.workspace = true -license.workspace = true -repository.workspace = true -keywords.workspace = true - -[dependencies] -async-trait = "0.1.80" -construct-models = { path = "../construct-models" } -reqwest = { version = "0.12.4", features = ["json", "socks"] } -serde = { version = "1.0.202", features = ["derive"] } diff --git a/construct-http/src/account/mod.rs b/construct-http/src/account/mod.rs deleted file mode 100644 index d7e0e9e..0000000 --- a/construct-http/src/account/mod.rs +++ /dev/null @@ -1,36 +0,0 @@ -use construct_models::account::Account; - -use crate::prelude::*; - -/// Account endpoint group -#[derive(Debug)] -pub struct AccountGroup { - client: reqwest::Client, - api_id: String, - api_secret: String, -} - -impl AccountGroup { - /// Return a new [`AccountGroup`] - pub fn new(client: reqwest::Client, api_id: &str, api_secret: &str) -> Self { - Self { - client, - api_id: api_id.to_string(), - api_secret: api_secret.to_string(), - } - } - - /// Return information about your account - pub async fn fetch(&self) -> Result { - Ok(self - .client - .get(format!("{}/v1/account", BASE_URL)) - .basic_auth(&self.api_id, Some(&self.api_secret)) - .send() - .await? - .process_error() - .await? - .json() - .await?) - } -} diff --git a/construct-http/src/lib.rs b/construct-http/src/lib.rs deleted file mode 100644 index 4ef4363..0000000 --- a/construct-http/src/lib.rs +++ /dev/null @@ -1,37 +0,0 @@ -use async_trait::async_trait; -use construct_models::Error; - -pub mod account; - -pub(crate) mod prelude { - pub(crate) use crate::{ResponseExt, Result, BASE_URL}; -} - -/// Base URL of Censys API -pub const BASE_URL: &str = "https://search.censys.io/api"; - -/// Custom error Result type -pub type Result = std::result::Result; - -/// Handle response error -#[async_trait] -trait ResponseExt { - async fn process_error(self) -> Result - where - Self: Sized; -} - -/// Match response status code -/// and handle the error -#[async_trait] -impl ResponseExt for reqwest::Response { - async fn process_error(self) -> Result - where - Self: Sized, - { - match self.status().as_u16() { - 200 => Ok(self), - _ => Err(Error::API(self.json().await?)), - } - } -} diff --git a/construct/Cargo.toml b/construct/Cargo.toml index 0872424..e98eeaa 100644 --- a/construct/Cargo.toml +++ b/construct/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "construct" -version = "0.1.0" +version = "1.2.0" edition = "2021" authors.workspace = true license.workspace = true @@ -8,5 +8,7 @@ repository.workspace = true keywords.workspace = true [dependencies] -construct-http = { path = "../construct-http" } -reqwest = { version = "0.12.4", features = ["socks"] } +async-trait = "0.1.80" +construct-models = { path = "../construct-models" } +reqwest = { version = "0.12.4", features = ["json", "socks"] } +serde = { version = "1.0.202", features = ["derive"] } diff --git a/construct/src/account/mod.rs b/construct/src/account/mod.rs new file mode 100644 index 0000000..70da721 --- /dev/null +++ b/construct/src/account/mod.rs @@ -0,0 +1,19 @@ +use construct_models::account::Account; + +use crate::prelude::*; + +impl Construct { + /// Returns information about your account + pub async fn fetch_account(&self) -> Result { + Ok(self + .client + .get(format!("{}/v1/account", self.base_url)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/lib.rs b/construct/src/lib.rs index bd25f29..49f47fa 100644 --- a/construct/src/lib.rs +++ b/construct/src/lib.rs @@ -1,9 +1,49 @@ -use construct_http::account::AccountGroup; +use async_trait::async_trait; +use construct_models::Error; + +pub mod account; +pub mod metadata; + +pub(crate) mod prelude { + pub(crate) use crate::{Construct, ResponseExt, Result}; +} + +/// Base URL of Censys API +const BASE_URL: &str = "https://search.censys.io/api"; + +/// Custom error Result type +pub type Result = std::result::Result; + +/// Handle response error +#[async_trait] +trait ResponseExt { + async fn process_error(self) -> Result + where + Self: Sized; +} + +/// Match response status code +/// and handle the error +#[async_trait] +impl ResponseExt for reqwest::Response { + async fn process_error(self) -> Result + where + Self: Sized, + { + match self.status().as_u16() { + 200 => Ok(self), + _ => Err(Error::API(self.json().await?)), + } + } +} /// Construct client #[derive(Debug)] pub struct Construct { - pub account: AccountGroup, + client: reqwest::Client, + base_url: String, + api_id: String, + api_secret: String, } impl Construct { @@ -16,7 +56,10 @@ impl Construct { .unwrap(); Self { - account: AccountGroup::new(client, api_id, api_secret), + client, + base_url: BASE_URL.to_owned(), + api_id: api_id.to_owned(), + api_secret: api_secret.to_owned(), } } } diff --git a/construct/src/metadata/hosts.rs b/construct/src/metadata/hosts.rs new file mode 100644 index 0000000..ead1459 --- /dev/null +++ b/construct/src/metadata/hosts.rs @@ -0,0 +1,19 @@ +use construct_models::metadata::Hosts; + +use crate::prelude::*; + +impl Construct { + /// Returns host metadata about what Censys scans for + pub async fn fetch_metadata_hosts(&self) -> Result { + Ok(self + .client + .get(format!("{}/v2/metadata/hosts", self.base_url)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/metadata/mod.rs b/construct/src/metadata/mod.rs new file mode 100644 index 0000000..36ff907 --- /dev/null +++ b/construct/src/metadata/mod.rs @@ -0,0 +1 @@ +pub(crate) mod hosts; diff --git a/examples/basic.rs b/examples/basic.rs index 54624d9..6ac9796 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -6,6 +6,6 @@ async fn main() { let construct = Construct::new("id", "secret"); // Fetch account information - let account = construct.account.fetch().await.unwrap(); + let account = construct.fetch_account().await.unwrap(); println!("{:#?}", account); }