trello/client.rs
1use reqwest::{Url, UrlError};
2
3pub struct Client {
4 pub host: String,
5 pub token: String,
6 pub key: String,
7}
8
9impl Client {
10 pub fn new(host: &str, token: &str, key: &str) -> Client {
11 Client {
12 host: String::from(host),
13 token: String::from(token),
14 key: String::from(key),
15 }
16 }
17
18 /// Gets the resultant URL of the Trello Client given some path and additional
19 /// parameters. The authentication credentials provided will be included as part
20 /// of the generated URL
21 /// ```
22 /// # use reqwest::UrlError;
23 /// # fn main() -> Result<(), UrlError> {
24 /// let client = trello::Client {
25 /// host: String::from("https://api.trello.com"),
26 /// token: String::from("some-token"),
27 /// key: String::from("some-key"),
28 /// };
29 /// let url = client.get_trello_url("/1/me/boards/", &[])?;
30 /// assert_eq!(
31 /// url.to_string(),
32 /// "https://api.trello.com/1/me/boards/?key=some-key&token=some-token"
33 /// );
34 /// let url = client.get_trello_url("/1/boards/some-id/", &[("lists", "open")])?;
35 /// assert_eq!(
36 /// url.to_string(),
37 /// "https://api.trello.com/1/boards/some-id/?key=some-key&token=some-token&lists=open",
38 /// );
39 /// # Ok(())
40 /// # }
41 /// ```
42 pub fn get_trello_url(&self, path: &str, params: &[(&str, &str)]) -> Result<Url, UrlError> {
43 let auth_params: &[(&str, &str)] = &[("key", &self.key), ("token", &self.token)];
44
45 Ok(Url::parse_with_params(
46 &format!("{}{}", self.host, path),
47 &[auth_params, params].concat(),
48 )?)
49 }
50}