1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use reqwest::{Url, UrlError};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Deserialize, Serialize)]
pub struct Client {
    #[serde(default = "Client::default_host")]
    pub host: String,
    pub token: String,
    pub key: String,
}

impl Client {
    pub fn new(host: &str, token: &str, key: &str) -> Client {
        Client {
            host: String::from(host),
            token: String::from(token),
            key: String::from(key),
        }
    }

    fn config_dir() -> Result<PathBuf, Box<dyn Error>> {
        let mut config_path = dirs::config_dir().ok_or("Unable to determine config directory")?;
        config_path.push("tro");

        Ok(config_path)
    }

    fn config_path() -> Result<PathBuf, Box<dyn Error>> {
        let mut config_path = Self::config_dir()?;
        config_path.push("config.toml");

        Ok(config_path)
    }

    pub fn save_config(&self) -> Result<(), Box<dyn Error>> {
        fs::create_dir_all(Self::config_dir()?)?;

        let config_path = Client::config_path()?;
        debug!("Saving configuration to {:?}", config_path);

        fs::write(config_path, toml::to_string(self)?)?;

        Ok(())
    }

    pub fn load_config() -> Result<Client, Box<dyn Error>> {
        let config_path = Client::config_path()?;
        debug!("Loading configuration from {:?}", config_path);
        let contents = fs::read_to_string(config_path)?;

        Ok(toml::from_str(&contents)?)
    }

    pub fn default_host() -> String {
        String::from("https://api.trello.com")
    }

    /// Gets the resultant URL of the Trello Client given some path and additional
    /// parameters. The authentication credentials provided will be included as part
    /// of the generated URL
    /// ```
    /// # use reqwest::UrlError;
    /// # fn main() -> Result<(), UrlError> {
    /// let client = trello::Client {
    ///     host: String::from("https://api.trello.com"),
    ///     token: String::from("some-token"),
    ///     key: String::from("some-key"),
    /// };
    /// let url = client.get_trello_url("/1/me/boards/", &[])?;
    /// assert_eq!(
    ///     url.to_string(),
    ///     "https://api.trello.com/1/me/boards/?key=some-key&token=some-token"
    /// );
    /// let url = client.get_trello_url("/1/boards/some-id/", &[("lists", "open")])?;
    /// assert_eq!(
    ///     url.to_string(),
    ///     "https://api.trello.com/1/boards/some-id/?key=some-key&token=some-token&lists=open",
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_trello_url(&self, path: &str, params: &[(&str, &str)]) -> Result<Url, UrlError> {
        let auth_params: &[(&str, &str)] = &[("key", &self.key), ("token", &self.token)];

        Ok(Url::parse_with_params(
            &format!("{}{}", self.host, path),
            &[auth_params, params].concat(),
        )?)
    }
}