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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use task::api_credentials::ApiCredentials;
use task::rest_client::{RestOperations, RestClient, RestClientError};
use task::tasks::Tasks;

pub struct HabiticaClient {
    rest_client: Box<RestOperations>,
    url: &'static str,
}

impl HabiticaClient {
    /// Creates a new HabiticaClient
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate habitica_rust_client;
    ///
    /// use habitica_rust_client::task::api_credentials::ApiCredentials;
    /// use habitica_rust_client::task::habitica_client::HabiticaClient;
    ///
    /// let api_credentials = ApiCredentials::new("user_id".to_string(), "api_token".to_string());
    ///
    /// HabiticaClient::new(api_credentials);
    ///
    /// ```
    pub fn new(api_credentials: ApiCredentials) -> HabiticaClient {
        let rest_client = RestClient::new(api_credentials);

        HabiticaClient {
            rest_client,
            url: "https://habitica.com/api/v3/tasks/user",
        }
    }

    /// Returns all of users tasks (habits, dailies, to-dos)
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate habitica_rust_client;
    ///
    /// use habitica_rust_client::task::api_credentials::ApiCredentials;
    /// use habitica_rust_client::task::habitica_client::HabiticaClient;
    /// use std::env;
    ///
    /// let api_credentials = ApiCredentials::new(
    ///        env::var("API_USER").unwrap().to_string(),
    ///        env::var("API_KEY").unwrap().to_string(),
    /// );
    ///
    /// HabiticaClient::new(api_credentials).get_all_tasks();
    ///
    /// ```
    ///
    /// # Errors
    ///
    /// If the REST call to Habitica Api does not succeed (status code diferrent from 200) it will return an error with a String that contains what happened
    ///
    pub fn get_all_tasks(&self) -> Result<Tasks, RestClientError> {
        let response = self.rest_client.get(self.url);

        Ok(Tasks::new(response?))
    }

}

#[cfg(test)]
mod tests {

    use super::*;
    use serde_json;
    use serde_json::Value;

    #[test]
    fn return_tasks_when_rest_call_succeds() {
        let client = HabiticaClient {
            rest_client: Box::new(SuccessRestClient {}),
            url: "https://habitica.com/api/v3/tasks/user"
        };

        let tasks = client.get_all_tasks();

        assert!(tasks.is_ok());
    }

    #[test]
    fn return_err_when_rest_call_fails() {
        let client = HabiticaClient {
            rest_client: Box::new(ErrorRestClient {}),
            url: "https://habitica.com/api/v3/tasks/user"
        };

        let tasks = client.get_all_tasks();

        assert!(tasks.is_err());
    }

    struct ErrorRestClient;
    struct SuccessRestClient;

    impl RestOperations for ErrorRestClient {
        fn get(&self, _url: &str) -> Result<Value, RestClientError> {
            Err(RestClientError::new("deu ruim".to_string()))
        }
    }

    impl RestOperations for SuccessRestClient {
        fn get(&self, _url: &str) -> Result<Value, RestClientError> {
            let data = r#"{
                                "data": [
                                    {"text": "Todo"}
                                ]
                             }"#;
            let raw_tasks: Value = serde_json::from_str(data).unwrap();

            Ok(Value::from(raw_tasks))
        }
    }

}