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)
}
pub fn list_asns(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/asns/")
}
pub fn get_asn(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/asns/{}/", slug))
}
pub fn list_cities(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/cities/")
}
pub fn get_city(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/cities/{}/", slug))
}
pub fn list_faqs(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/faqs/")
}
pub fn get_faq(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/faqs/{}/", slug))
}
pub fn list_glossary_categories(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/glossary-categories/")
}
pub fn get_glossary_category(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/glossary-categories/{}/", slug))
}
pub fn list_glossary_terms(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/glossary-terms/")
}
pub fn get_glossary_term(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/glossary-terms/{}/", slug))
}
pub fn list_ip_ranges(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/ip-ranges/")
}
pub fn get_ip_range(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/ip-ranges/{}/", slug))
}
pub fn list_isps(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/isps/")
}
pub fn get_isp(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/rest/isps/{}/", slug))
}
pub fn list_threats(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/rest/threats/")
}
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() }
}