use super::{FileRescanResponse, FileScanResponse, VtClient};
use anyhow::{Context, Result};
use reqwest::Client;
use serde_json::{from_str, Value};
impl<'a> VtClient<'a> {
pub fn scan_file(self, filename: &str) -> Result<FileScanResponse> {
let form = reqwest::blocking::multipart::Form::new()
.file("file", filename)
.context("File not found")?;
let url = format!("{}/files", self.endpoint);
let resp = reqwest::blocking::Client::new()
.post(url)
.header("x-apikey", self.api_key)
.header("accept", "application/json")
.header("content-type", "multipart/form-data")
.multipart(form)
.send()
.context("Error! Probably maximum request limit achieved!")?;
let text: &str = &resp.text()?;
Ok(from_str(text)?)
}
pub async fn get_report_file(self, resource: &str) -> Result<Value> {
let url = &[self.endpoint, "/files/", resource].join("");
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 rescan_file(self, resource: &str) -> Result<FileRescanResponse> {
let url = format!("{}/files/{resource}/analyse", self.endpoint);
let resp = Client::new()
.post(&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)?)
}
}