use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
#[derive(Clone, Debug)]
pub struct GetSecuritySummaryParams {
pub x_request_id: Option<String>,
pub with_dangerous_cve: Option<bool>,
pub with_dangerous_artifact: Option<bool>
}
#[derive(Clone, Debug)]
pub struct ListVulnerabilitiesParams {
pub x_request_id: Option<String>,
pub q: Option<String>,
pub page: Option<i64>,
pub page_size: Option<i64>,
pub tune_count: Option<bool>,
pub with_tag: Option<bool>
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetSecuritySummaryError {
Status401(models::Errors),
Status403(models::Errors),
Status404(models::Errors),
Status500(models::Errors),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListVulnerabilitiesError {
Status400(models::Errors),
Status401(models::Errors),
Status500(models::Errors),
UnknownValue(serde_json::Value),
}
pub async fn get_security_summary(configuration: &configuration::Configuration, params: GetSecuritySummaryParams) -> Result<models::SecuritySummary, Error<GetSecuritySummaryError>> {
let uri_str = format!("{}/security/summary", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = params.with_dangerous_cve {
req_builder = req_builder.query(&[("with_dangerous_cve", ¶m_value.to_string())]);
}
if let Some(ref param_value) = params.with_dangerous_artifact {
req_builder = req_builder.query(&[("with_dangerous_artifact", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(param_value) = params.x_request_id {
req_builder = req_builder.header("X-Request-Id", param_value.to_string());
}
if let Some(ref auth_conf) = configuration.basic_auth {
req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SecuritySummary`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::SecuritySummary`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<GetSecuritySummaryError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn list_vulnerabilities(configuration: &configuration::Configuration, params: ListVulnerabilitiesParams) -> Result<Vec<models::VulnerabilityItem>, Error<ListVulnerabilitiesError>> {
let uri_str = format!("{}/security/vul", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = params.q {
req_builder = req_builder.query(&[("q", ¶m_value.to_string())]);
}
if let Some(ref param_value) = params.page {
req_builder = req_builder.query(&[("page", ¶m_value.to_string())]);
}
if let Some(ref param_value) = params.page_size {
req_builder = req_builder.query(&[("page_size", ¶m_value.to_string())]);
}
if let Some(ref param_value) = params.tune_count {
req_builder = req_builder.query(&[("tune_count", ¶m_value.to_string())]);
}
if let Some(ref param_value) = params.with_tag {
req_builder = req_builder.query(&[("with_tag", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(param_value) = params.x_request_id {
req_builder = req_builder.header("X-Request-Id", param_value.to_string());
}
if let Some(ref auth_conf) = configuration.basic_auth {
req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::VulnerabilityItem>`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::VulnerabilityItem>`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListVulnerabilitiesError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}