ipscanner/resources/
ip.rs1use serde_json::json;
2
3use crate::client::segment;
4use crate::types::{
5 AsnResponse, GeolocationResponse, History, HistoryParams, LookupResult, MyIp, ProxyResponse,
6 VpnResponse, WhoisResponse,
7};
8use crate::{Client, Error};
9
10#[derive(Debug, Clone, Copy)]
12pub struct Ip<'a> {
13 client: &'a Client,
14}
15
16impl<'a> Ip<'a> {
17 pub(crate) fn new(client: &'a Client) -> Self {
18 Ip { client }
19 }
20
21 pub async fn lookup(&self, target: &str) -> Result<LookupResult, Error> {
23 self.client
24 .post("/ip/lookup", &json!({ "target": target }))
25 .await
26 }
27
28 pub async fn vpn(&self, ip: &str) -> Result<VpnResponse, Error> {
30 self.client.get(&format!("/vpn/{}", segment(ip)), &[]).await
31 }
32
33 pub async fn proxy(&self, ip: &str) -> Result<ProxyResponse, Error> {
35 self.client
36 .get(&format!("/proxy/{}", segment(ip)), &[])
37 .await
38 }
39
40 pub async fn geo(&self, ip: &str) -> Result<GeolocationResponse, Error> {
42 self.client.get(&format!("/geo/{}", segment(ip)), &[]).await
43 }
44
45 pub async fn asn(&self, ip: &str) -> Result<AsnResponse, Error> {
47 self.client.get(&format!("/asn/{}", segment(ip)), &[]).await
48 }
49
50 pub async fn whois(&self, domain: &str) -> Result<WhoisResponse, Error> {
52 self.client
53 .get(&format!("/whois/{}", segment(domain)), &[])
54 .await
55 }
56
57 pub async fn history(&self, params: &HistoryParams) -> Result<History, Error> {
59 let mut q = Vec::new();
60 if let Some(v) = params.limit {
61 q.push(("limit", v.to_string()));
62 }
63 if let Some(v) = params.before {
64 q.push(("before", v.to_string()));
65 }
66 if let Some(v) = ¶ms.verdict {
67 q.push(("verdict", v.clone()));
68 }
69 self.client.get("/ip/history", &q).await
70 }
71
72 pub async fn demo(&self, ip: &str) -> Result<LookupResult, Error> {
74 self.client
75 .get(&format!("/demo/{}", segment(ip)), &[])
76 .await
77 }
78
79 pub async fn myip(&self) -> Result<MyIp, Error> {
81 self.client.get("/myip", &[]).await
82 }
83}