Skip to main content

zipfyi/
lib.rs

1//! Rust client for [ZipFYI](https://zipfyi.com) REST API.
2//!
3//! ```rust
4//! let client = zipfyi::Client::new();
5//! let result = client.search("query").unwrap();
6//! ```
7
8use serde_json::Value;
9
10pub struct Client {
11    base_url: String,
12    http: reqwest::blocking::Client,
13}
14
15impl Client {
16    pub fn new() -> Self {
17        Self {
18            base_url: "https://zipfyi.com".to_string(),
19            http: reqwest::blocking::Client::new(),
20        }
21    }
22
23    fn get(&self, path: &str) -> Result<Value, Box<dyn std::error::Error>> {
24        let url = format!("{}{}", self.base_url, path);
25        let resp = self.http.get(&url).send()?.json()?;
26        Ok(resp)
27    }
28
29    pub fn search(&self, query: &str) -> Result<Value, Box<dyn std::error::Error>> {
30        let url = format!("{}/api/v1/search/?q={}", self.base_url, query);
31        let resp = self.http.get(&url).send()?.json()?;
32        Ok(resp)
33    }
34
35    /// List all blog posts.
36    pub fn list_blog_posts(&self) -> Result<Value, Box<dyn std::error::Error>> {
37        self.get("/api/v1/blog-posts/")
38    }
39
40    /// Get blog post by slug.
41    pub fn get_blog_post(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
42        self.get(&format!("/api/v1/blog-posts/{}/", slug))
43    }
44    /// List all blog series.
45    pub fn list_blog_series(&self) -> Result<Value, Box<dyn std::error::Error>> {
46        self.get("/api/v1/blog-series/")
47    }
48
49    /// Get blog sery by slug.
50    pub fn get_blog_sery(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
51        self.get(&format!("/api/v1/blog-series/{}/", slug))
52    }
53    /// List all cities.
54    pub fn list_cities(&self) -> Result<Value, Box<dyn std::error::Error>> {
55        self.get("/api/v1/cities/")
56    }
57
58    /// Get city by slug.
59    pub fn get_city(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
60        self.get(&format!("/api/v1/cities/{}/", slug))
61    }
62    /// List all countries.
63    pub fn list_countries(&self) -> Result<Value, Box<dyn std::error::Error>> {
64        self.get("/api/v1/countries/")
65    }
66
67    /// Get country by slug.
68    pub fn get_country(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
69        self.get(&format!("/api/v1/countries/{}/", slug))
70    }
71    /// List all faqs.
72    pub fn list_faqs(&self) -> Result<Value, Box<dyn std::error::Error>> {
73        self.get("/api/v1/faqs/")
74    }
75
76    /// Get faq by slug.
77    pub fn get_faq(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
78        self.get(&format!("/api/v1/faqs/{}/", slug))
79    }
80    /// List all glossary.
81    pub fn list_glossary(&self) -> Result<Value, Box<dyn std::error::Error>> {
82        self.get("/api/v1/glossary/")
83    }
84
85    /// Get term by slug.
86    pub fn get_term(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
87        self.get(&format!("/api/v1/glossary/{}/", slug))
88    }
89    /// List all postal codes.
90    pub fn list_postal_codes(&self) -> Result<Value, Box<dyn std::error::Error>> {
91        self.get("/api/v1/postal-codes/")
92    }
93
94    /// Get postal code by slug.
95    pub fn get_postal_code(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
96        self.get(&format!("/api/v1/postal-codes/{}/", slug))
97    }
98    /// List all states.
99    pub fn list_states(&self) -> Result<Value, Box<dyn std::error::Error>> {
100        self.get("/api/v1/states/")
101    }
102
103    /// Get state by slug.
104    pub fn get_state(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
105        self.get(&format!("/api/v1/states/{}/", slug))
106    }
107}
108
109impl Default for Client {
110    fn default() -> Self { Self::new() }
111}