use super::{CommentGetResponse, CommentPutResponse, VtClient, VtType};
use anyhow::{Context, Result};
use reqwest::Client;
use serde_json::{from_str, json};
impl<'a> VtClient<'a> {
pub async fn put_comment(
self,
resource: &str,
comment: &str,
vt_type: &VtType,
) -> Result<CommentPutResponse> {
let url = match vt_type {
VtType::File => {
format!("{}/files/{resource}/comments", self.endpoint)
}
VtType::Url => {
format!("{}/urls/{resource}/comments", self.endpoint)
}
VtType::Domain => {
format!("{}/domains/{resource}/comments", self.endpoint)
}
VtType::Ip => {
format!("{}/ip-address/{resource}/comments", self.endpoint)
}
};
let datas = json!({
"data": {
"type": "comment",
"attributes": {
"text": comment
}
}
});
let resp = Client::new()
.post(&url)
.header("x-apikey", self.api_key)
.json(&datas)
.send()
.await
.context("Error! Probably maximum request limit achieved!")?;
let text: &str = &resp.text().await?;
Ok(from_str(text)?)
}
pub async fn get_comment(self, resource: &str, vt_type: &VtType) -> Result<CommentGetResponse> {
let url = match vt_type {
VtType::File => {
format!("{}/files/{resource}/comments", self.endpoint)
}
VtType::Url => {
format!("{}/urls/{resource}/comments", self.endpoint)
}
VtType::Domain => {
format!("{}/domains/{resource}/comments", self.endpoint)
}
VtType::Ip => {
format!("{}/ip-address/{resource}/comments", self.endpoint)
}
};
let resp = Client::new()
.get(&url)
.header("x-apikey", self.api_key)
.send()
.await
.context("Error! Probably maximum request limit achieved!")?;
let text: &str = &resp.text().await?;
Ok(from_str(text)?)
}
pub async fn delete_comment(self, resource: &str) -> Result<bool> {
let url = format!("{}/comments/{resource}", self.endpoint);
let resp = Client::new()
.delete(&url)
.header("x-apikey", self.api_key)
.send()
.await
.context("Error! Probably maximum request limit achieved!")?;
let text: &str = &resp.text().await?;
Ok(!text.contains("NotFoundError"))
}
}