Skip to main content

starfyi/
lib.rs

1//! Rust client for [StarFYI](https://starfyi.com) REST API.
2//!
3//! ```rust
4//! let client = starfyi::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://starfyi.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 comparisons.
36    pub fn list_comparisons(&self) -> Result<Value, Box<dyn std::error::Error>> {
37        self.get("/api/v1/comparisons/")
38    }
39
40    /// Get comparison by slug.
41    pub fn get_comparison(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
42        self.get(&format!("/api/v1/comparisons/{}/", slug))
43    }
44    /// List all constellations.
45    pub fn list_constellations(&self) -> Result<Value, Box<dyn std::error::Error>> {
46        self.get("/api/v1/constellations/")
47    }
48
49    /// Get constellation by slug.
50    pub fn get_constellation(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
51        self.get(&format!("/api/v1/constellations/{}/", slug))
52    }
53    /// List all deep sky.
54    pub fn list_deep_sky(&self) -> Result<Value, Box<dyn std::error::Error>> {
55        self.get("/api/v1/deep-sky/")
56    }
57
58    /// Get deep sky by slug.
59    pub fn get_deep_sky(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
60        self.get(&format!("/api/v1/deep-sky/{}/", slug))
61    }
62    /// List all exoplanets.
63    pub fn list_exoplanets(&self) -> Result<Value, Box<dyn std::error::Error>> {
64        self.get("/api/v1/exoplanets/")
65    }
66
67    /// Get exoplanet by slug.
68    pub fn get_exoplanet(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
69        self.get(&format!("/api/v1/exoplanets/{}/", 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 glossary categories.
90    pub fn list_glossary_categories(&self) -> Result<Value, Box<dyn std::error::Error>> {
91        self.get("/api/v1/glossary-categories/")
92    }
93
94    /// Get glossary category by slug.
95    pub fn get_glossary_category(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
96        self.get(&format!("/api/v1/glossary-categories/{}/", slug))
97    }
98    /// List all guide series.
99    pub fn list_guide_series(&self) -> Result<Value, Box<dyn std::error::Error>> {
100        self.get("/api/v1/guide-series/")
101    }
102
103    /// Get guide sery by slug.
104    pub fn get_guide_sery(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
105        self.get(&format!("/api/v1/guide-series/{}/", slug))
106    }
107    /// List all guides.
108    pub fn list_guides(&self) -> Result<Value, Box<dyn std::error::Error>> {
109        self.get("/api/v1/guides/")
110    }
111
112    /// Get guide by slug.
113    pub fn get_guide(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
114        self.get(&format!("/api/v1/guides/{}/", slug))
115    }
116    /// List all spectral classes.
117    pub fn list_spectral_classes(&self) -> Result<Value, Box<dyn std::error::Error>> {
118        self.get("/api/v1/spectral-classes/")
119    }
120
121    /// Get spectral classe by slug.
122    pub fn get_spectral_classe(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
123        self.get(&format!("/api/v1/spectral-classes/{}/", slug))
124    }
125    /// List all stars.
126    pub fn list_stars(&self) -> Result<Value, Box<dyn std::error::Error>> {
127        self.get("/api/v1/stars/")
128    }
129
130    /// Get star by slug.
131    pub fn get_star(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
132        self.get(&format!("/api/v1/stars/{}/", slug))
133    }
134}
135
136impl Default for Client {
137    fn default() -> Self { Self::new() }
138}