Skip to main content

ipscanner/resources/
directory.rs

1use crate::client::segment;
2use crate::types::{AsnDetail, AsnSearch, AsnTop, AsnTopParams};
3use crate::{Client, Error};
4
5/// The public ASN directory. Keyless. Get one with [`Client::asn_directory`].
6#[derive(Debug, Clone, Copy)]
7pub struct AsnDirectory<'a> {
8    client: &'a Client,
9}
10
11impl<'a> AsnDirectory<'a> {
12    pub(crate) fn new(client: &'a Client) -> Self {
13        AsnDirectory { client }
14    }
15
16    /// The largest autonomous systems by address count or prefix count.
17    pub async fn top(&self, params: &AsnTopParams) -> Result<AsnTop, Error> {
18        let mut q = Vec::new();
19        if let Some(v) = params.top {
20            q.push(("top", v.to_string()));
21        }
22        if let Some(v) = &params.by {
23            q.push(("by", v.clone()));
24        }
25        self.client.get("/asn/directory", &q).await
26    }
27
28    /// Searches by name or number.
29    pub async fn search(&self, query: &str, limit: Option<u32>) -> Result<AsnSearch, Error> {
30        let mut q = vec![("q", query.to_string())];
31        if let Some(v) = limit {
32            q.push(("limit", v.to_string()));
33        }
34        self.client.get("/asn/directory", &q).await
35    }
36
37    /// One autonomous system. Accepts `15169` or `"AS15169"`.
38    pub async fn get(&self, asn: impl ToString) -> Result<AsnDetail, Error> {
39        let raw = asn.to_string();
40        let raw = raw.trim();
41        let number = raw
42            .strip_prefix("AS")
43            .or_else(|| raw.strip_prefix("as"))
44            .unwrap_or(raw);
45        self.client
46            .get(&format!("/asn/directory/{}", segment(number)), &[])
47            .await
48    }
49}