chemfyi 0.1.0

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

    /// Get application by slug.
    pub fn get_application(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/applications/{}/", 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 compounds.
    pub fn list_compounds(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/compounds/")
    }

    /// Get compound by slug.
    pub fn get_compound(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/compounds/{}/", slug))
    }
    /// List all elements.
    pub fn list_elements(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/elements/")
    }

    /// Get element by slug.
    pub fn get_element(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/elements/{}/", slug))
    }
    /// List all experiments.
    pub fn list_experiments(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/experiments/")
    }

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

    /// Get glossary category by slug.
    pub fn get_glossary_category(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/glossary-categories/{}/", slug))
    }
    /// List all guide series.
    pub fn list_guide_series(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/guide-series/")
    }

    /// Get guide sery by slug.
    pub fn get_guide_sery(&self, slug: &str) -> Result<Value, Box<dyn std::error::Error>> {
        self.get(&format!("/api/v1/guide-series/{}/", 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 reactions.
    pub fn list_reactions(&self) -> Result<Value, Box<dyn std::error::Error>> {
        self.get("/api/v1/reactions/")
    }

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

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