use serde_json::json;
use crate::client::segment;
use crate::types::{
AsnResponse, GeolocationResponse, History, HistoryParams, LookupResult, MyIp, ProxyResponse,
VpnResponse, WhoisResponse,
};
use crate::{Client, Error};
#[derive(Debug, Clone, Copy)]
pub struct Ip<'a> {
client: &'a Client,
}
impl<'a> Ip<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Ip { client }
}
pub async fn lookup(&self, target: &str) -> Result<LookupResult, Error> {
self.client
.post("/ip/lookup", &json!({ "target": target }))
.await
}
pub async fn vpn(&self, ip: &str) -> Result<VpnResponse, Error> {
self.client.get(&format!("/vpn/{}", segment(ip)), &[]).await
}
pub async fn proxy(&self, ip: &str) -> Result<ProxyResponse, Error> {
self.client
.get(&format!("/proxy/{}", segment(ip)), &[])
.await
}
pub async fn geo(&self, ip: &str) -> Result<GeolocationResponse, Error> {
self.client.get(&format!("/geo/{}", segment(ip)), &[]).await
}
pub async fn asn(&self, ip: &str) -> Result<AsnResponse, Error> {
self.client.get(&format!("/asn/{}", segment(ip)), &[]).await
}
pub async fn whois(&self, domain: &str) -> Result<WhoisResponse, Error> {
self.client
.get(&format!("/whois/{}", segment(domain)), &[])
.await
}
pub async fn history(&self, params: &HistoryParams) -> Result<History, Error> {
let mut q = Vec::new();
if let Some(v) = params.limit {
q.push(("limit", v.to_string()));
}
if let Some(v) = params.before {
q.push(("before", v.to_string()));
}
if let Some(v) = ¶ms.verdict {
q.push(("verdict", v.clone()));
}
self.client.get("/ip/history", &q).await
}
pub async fn demo(&self, ip: &str) -> Result<LookupResult, Error> {
self.client
.get(&format!("/demo/{}", segment(ip)), &[])
.await
}
pub async fn myip(&self) -> Result<MyIp, Error> {
self.client.get("/myip", &[]).await
}
}