Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
cn-kali-team committed Nov 22, 2023
1 parent cffebc0 commit f0be07d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
43 changes: 20 additions & 23 deletions nvd-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
use reqwest::{ClientBuilder, RequestBuilder};
use crate::error::Error;
use reqwest::{ClientBuilder, RequestBuilder};
use serde_json::Value::Object;

mod v2;
mod error;

mod v2;
const BASE_URL: &'static str = "https://services.nvd.nist.gov/rest/json/";
const VERSION: f32 = 2.0;
#[derive(Debug, Clone)]
pub struct NVDApi {
base_path: String,
client: reqwest::Client,
}

impl NVDApi {
pub fn new(api_token: String) -> Result<Self, Error> {
pub fn new(api_token: Option<String>) -> Result<Self, Error> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
"Notion-Version",
reqwest::header::HeaderValue::from_static(NOTION_API_VERSION),
);
let mut auth_value = reqwest::header::HeaderValue::from_str(&format!("Bearer {api_token}"))
.map_err(|source| Error::InvalidApiToken { source })?;
.map_err(|source| Error::InvalidApiToken { source })?;
auth_value.set_sensitive(true);
headers.insert(reqwest::header::AUTHORIZATION, auth_value);
let api_client = ClientBuilder::new()
.default_headers(headers)
.build()
.map_err(|source| Error::ErrorBuildingClient { source })?;
.default_headers(headers)
.build()
.map_err(|source| Error::ErrorBuildingClient { source })?;
Ok(NVDApi {
base_path: "https://api.notion.com/v1".to_owned(),
base_path: BASE_URL.to_owned(),
client: api_client,
})
}
Expand All @@ -36,18 +34,17 @@ impl NVDApi {
pub async fn request(&self, request: RequestBuilder) -> Result<Object, Error> {
let request = request.build()?;
let json = self
.client
.execute(request)
.await
.map_err(|source| Error::RequestFailed { source })?
.text()
.await
.map_err(|source| Error::ResponseIoError { source })?;
let result =
serde_json::from_str(&json).map_err(|source| Error::JsonParseError { source })?;
.client
.execute(request)
.await
.map_err(|source| Error::RequestFailed { source })?
.text()
.await
.map_err(|source| Error::ResponseIoError { source })?;
let result = serde_json::from_str(&json).map_err(|source| Error::JsonParseError { source })?;
match result {
Object::Error { error } => Err(Error::ApiError { error }),
response => Ok(response),
}
}
}
}
10 changes: 10 additions & 0 deletions nvd-api/src/v2/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::v2::vulnerabilities::Vulnerabilities;
use crate::{Error, NVDApi, Object};
const ROUTER: &str = "cve";

impl NVDApi {
pub async fn vulnerabilities(&self, query: Vulnerabilities) -> Result<Object, Error> {
let u = format!("{}/{}", self.base_path, ROUTER);
self.request(self.client.get(u).query(&query)).await
}
}
3 changes: 1 addition & 2 deletions nvd-api/src/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod vulnerabilities;
mod api;

const BASE_URL: &'static str = "https://services.nvd.nist.gov/rest/json/";
const VERSION: f32 = 2.0;
20 changes: 10 additions & 10 deletions nvd-api/src/v2/vulnerabilities.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

// https://nvd.nist.gov/developers/vulnerabilities
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct CVE {
pub struct Vulnerabilities {
cpe_name: Option<String>,
cve_id: Option<String>,
cvss_v2_metrics: Option<String>,
Expand Down Expand Up @@ -29,7 +29,7 @@ struct CVE {
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct VirtualMatch {
pub struct VirtualMatch {
virtual_match_string: String,
#[serde(flatten)]
version_start: Option<VersionStart>,
Expand All @@ -38,43 +38,43 @@ struct VirtualMatch {
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct VersionStart {
pub struct VersionStart {
version_start: String,
version_start_type: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct VersionEnd {
pub struct VersionEnd {
version_end: String,
version_end_type: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
enum VersionType {
pub enum VersionType {
Including,
Excluding,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct LimitOffset {
pub struct LimitOffset {
results_per_page: Option<u64>,
start_index: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct PubDate {
pub struct PubDate {
pub_start_date: String,
pub_end_date: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct Keyword {
pub struct Keyword {
keyword_exact_match: bool,
keyword_search: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)]
struct LastModDate {
pub struct LastModDate {
last_mod_start_date: String,
last_mod_end_date: String,
}

0 comments on commit f0be07d

Please sign in to comment.