use serde_json::Value;
pub struct Client {
base_url: String,
http: reqwest::blocking::Client,
}
impl Client {
pub fn new() -> Self {
Self {
base_url: "https://trainfyi.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/search/?q={}", self.base_url, query);
let resp = self.http.get(&url).send()?.json()?;
Ok(resp)
}
pub fn list_countries(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/countries/")
}
pub fn get_country(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/countries/{}/", slug))
}
pub fn list_faqs(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/faqs/")
}
pub fn get_faq(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/faqs/{}/", slug))
}
pub fn list_operators(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/operators/")
}
pub fn get_operator(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/operators/{}/", slug))
}
pub fn list_route_pairs(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/route-pairs/")
}
pub fn get_route_pair(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/route-pairs/{}/", slug))
}
pub fn list_stations(&self) -> Result<Value, Box<dyn std::error::Error>> {
self.get("/api/v1/stations/")
}
pub fn get_station(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
self.get(&format!("/api/v1/stations/{}/", slug))
}
}
impl Default for Client {
fn default() -> Self { Self::new() }
}