ipfyi 0.1.0

Rust client for the IPFYI API — https://ipfyi.com
Documentation
//! Rust client for [IPFYI](https://ipfyi.com) REST API.
//!
//! ```rust
//! let client = ipfyi::Client::new();
//! let result = client.search("query").unwrap();
//! ```

use serde_json::Value;

pub struct Client {
    base_url: String,
    http: reqwest::blocking::Client,
}

impl Client {
    pub fn new() -> Self {
        Self {
            base_url: "https://ipfyi.com".to_string(),
            http: reqwest::blocking::Client::new(),
        }
    }

    fn get(&self, path: &str) -> Result<Value, Box<dyn std::error::Error>> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self.http.get(&url).send()?.json()?;
        Ok(resp)
    }

    pub fn search(&self, query: &str) -> Result<Value, Box<dyn std::error::Error>> {
        let url = format!("{}/api/v1/rest/search/?q={}", self.base_url, query);
        let resp = self.http.get(&url).send()?.json()?;
        Ok(resp)
    }

    /// List all asns.
    pub fn list_asns(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/asns/")
    }

    /// Get asn by slug.
    pub fn get_asn(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/asns/{}/", slug))
    }
    /// List all cities.
    pub fn list_cities(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/cities/")
    }

    /// Get city by slug.
    pub fn get_city(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/cities/{}/", slug))
    }
    /// List all faqs.
    pub fn list_faqs(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/faqs/")
    }

    /// Get faq by slug.
    pub fn get_faq(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/faqs/{}/", slug))
    }
    /// List all glossary categories.
    pub fn list_glossary_categories(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/glossary-categories/")
    }

    /// Get glossary category by slug.
    pub fn get_glossary_category(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/glossary-categories/{}/", slug))
    }
    /// List all glossary terms.
    pub fn list_glossary_terms(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/glossary-terms/")
    }

    /// Get glossary term by slug.
    pub fn get_glossary_term(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/glossary-terms/{}/", slug))
    }
    /// List all ip ranges.
    pub fn list_ip_ranges(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/ip-ranges/")
    }

    /// Get ip range by slug.
    pub fn get_ip_range(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/ip-ranges/{}/", slug))
    }
    /// List all isps.
    pub fn list_isps(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/isps/")
    }

    /// Get isp by slug.
    pub fn get_isp(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/isps/{}/", slug))
    }
    /// List all threats.
    pub fn list_threats(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/rest/threats/")
    }

    /// Get threat by slug.
    pub fn get_threat(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/rest/threats/{}/", slug))
    }
}

impl Default for Client {
    fn default() -> Self { Self::new() }
}