beerfyi 0.1.0

Rust client for the BeerFYI API — https://beerfyi.com
Documentation
//! Rust client for [BeerFYI](https://beerfyi.com) REST API.
//!
//! ```rust
//! let client = beerfyi::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://beerfyi.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 breweries.
    pub fn list_breweries(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/breweries/")
    }

    /// Get brewery by slug.
    pub fn get_brewery(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/breweries/{}/", slug))
    }
    /// List all categories.
    pub fn list_categories(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/categories/")
    }

    /// Get category by slug.
    pub fn get_category(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/categories/{}/", slug))
    }
    /// 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 glossary.
    pub fn list_glossary(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/glossary/")
    }

    /// Get term by slug.
    pub fn get_term(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/glossary/{}/", slug))
    }
    /// List all guides.
    pub fn list_guides(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/guides/")
    }

    /// Get guide by slug.
    pub fn get_guide(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/guides/{}/", slug))
    }
    /// List all hops.
    pub fn list_hops(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/hops/")
    }

    /// Get hop by slug.
    pub fn get_hop(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/hops/{}/", slug))
    }
    /// List all malts.
    pub fn list_malts(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/malts/")
    }

    /// Get malt by slug.
    pub fn get_malt(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/malts/{}/", slug))
    }
    /// List all regions.
    pub fn list_regions(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/regions/")
    }

    /// Get region by slug.
    pub fn get_region(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/regions/{}/", slug))
    }
    /// List all styles.
    pub fn list_styles(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/styles/")
    }

    /// Get style by slug.
    pub fn get_style(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/styles/{}/", slug))
    }
    /// List all tools.
    pub fn list_tools(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/tools/")
    }

    /// Get tool by slug.
    pub fn get_tool(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/tools/{}/", slug))
    }
    /// List all yeasts.
    pub fn list_yeasts(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/yeasts/")
    }

    /// Get yeast by slug.
    pub fn get_yeast(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/yeasts/{}/", slug))
    }
}

impl Default for Client {
    fn default() -> Self { Self::new() }
}