Moved crates & added 2 endpoints

This commit is contained in:
perp 2024-05-25 00:22:03 +01:00
parent 55af3e1455
commit 41d421b082
11 changed files with 93 additions and 104 deletions

10
Cargo.lock generated
View File

@ -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",

View File

@ -1,6 +1,6 @@
[workspace]
resolver = "1"
members = ["construct", "construct-http", "construct-models", "examples"]
members = ["construct", "construct-models", "examples"]
[workspace.package]
authors = ["perp"]

View File

@ -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"] }

View File

@ -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<Account> {
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?)
}
}

View File

@ -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<T> = std::result::Result<T, Error>;
/// Handle response error
#[async_trait]
trait ResponseExt {
async fn process_error(self) -> Result<Self>
where
Self: Sized;
}
/// Match response status code
/// and handle the error
#[async_trait]
impl ResponseExt for reqwest::Response {
async fn process_error(self) -> Result<Self>
where
Self: Sized,
{
match self.status().as_u16() {
200 => Ok(self),
_ => Err(Error::API(self.json().await?)),
}
}
}

View File

@ -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"] }

View File

@ -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<Account> {
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?)
}
}

View File

@ -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<T> = std::result::Result<T, Error>;
/// Handle response error
#[async_trait]
trait ResponseExt {
async fn process_error(self) -> Result<Self>
where
Self: Sized;
}
/// Match response status code
/// and handle the error
#[async_trait]
impl ResponseExt for reqwest::Response {
async fn process_error(self) -> Result<Self>
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(),
}
}
}

View File

@ -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<Hosts> {
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?)
}
}

View File

@ -0,0 +1 @@
pub(crate) mod hosts;

View File

@ -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);
}