trainfyi 0.1.0

Rust client for the TrainFYI API — https://trainfyi.com
Documentation
//! Rust client for [TrainFYI](https://trainfyi.com) REST API.
//!
//! ```rust
//! let client = trainfyi::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://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)
    }

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

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

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

    /// Get operator by slug.
    pub fn get_operator(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/operators/{}/", slug))
    }
    /// List all route pairs.
    pub fn list_route_pairs(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/route-pairs/")
    }

    /// Get route pair by slug.
    pub fn get_route_pair(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/route-pairs/{}/", slug))
    }
    /// List all stations.
    pub fn list_stations(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/stations/")
    }

    /// Get station by slug.
    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() }
}