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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use serde::{Deserialize, de::DeserializeOwned};
use serde_json::Value;

use crate::schema;

/// Represents a response from Asana
///
/// This is a JSON object that could have 2 (or more idk) keys: "data" or "errors". You can
/// deserialize a payload from the Asana API into one of these.
///
/// You can use `value()` or `values()` to get the data returned by Asana, serialized into one
/// of the structs in the [`schema`](crate::schema) module. You can use `errors()` to return a
/// vector of `schema::Error`s.
#[derive(Deserialize)]
pub struct Response {
    #[serde(default)]
    pub data: serde_json::Value,
    #[serde(default)]
    pub errors: Vec<schema::Error>,
}

impl Response {
    /// Consumes the response, returning `Ok()` with the type provided or
    /// `Err()` with a vector of `schema::Error`s.
    pub fn into<T: DeserializeOwned>(self) -> Result<T, Vec<schema::Error>> {
        if self.errors.len() > 0 {
            return Err(self.errors);
        }

        match serde_json::from_value::<T>(self.data) {
            Ok(value) => Ok(value),
            Err(_) => Err(vec![])
        }
    }

    /// Returns the single value of type T. If Asana returns an array of objects,
    /// this will return None. See `values()`.
    ///
    /// ```rust
    /// use rust_asana::Response;
    /// use rust_asana::schema::UserCompact;
    ///
    /// // This is UserCompact object, defined by Asana
    /// let payload = r#"{
    ///     "data": {
    ///         "gid": "12345",
    ///         "resource_type": "user",
    ///         "name": "Greg Sanchez"
    ///     }
    /// }"#;
    ///
    /// let resp = serde_json::from_str::<Response>(&payload).unwrap();
    /// let user = resp.value::<UserCompact>();
    /// assert!(user.is_some());
    /// assert_eq!(user.unwrap().name, "Greg Sanchez");
    /// ```
    pub fn value<T: DeserializeOwned>(&self) -> Option<T> {
        serde_json::from_value::<T>(self.data.clone()).ok()
    }

    /// Same as `values()`, but returns the Asana errors (always a vector).
    pub fn errors(&self) -> Option<Vec<schema::Error>> {
        if self.errors.len() > 0 {
            return Some(self.errors.clone());
        } else {
            return None;
        }
    }

    /// This is the same as `value()`, but it returns a vector of values. Some requests
    /// from Asana return an array of values, which this is meant for.
    ///
    /// **Important Note**: This methid *will work* on a payload of only one object. It
    /// will convert it to a vector of one item. As long as there is serializable data in the
    /// `data` field of the payload, this will return the object(s).
    ///
    /// If the response is an empty array, this will return Some([]), not None
    ///
    /// ```rust
    /// use rust_asana::Response;
    /// use rust_asana::schema::UserCompact;
    ///
    /// // This is UserCompact object, defined by Asana
    /// let payload = r#"{
    ///     "data": [
    ///         {
    ///             "gid": "12345",
    ///             "resource_type": "user",
    ///             "name": "Greg Sanchez"
    ///         },
    ///         {
    ///             "gid": "54321",
    ///             "resource_type": "user",
    ///             "name": "Luke Lastname"
    ///         }
    ///     ]
    /// }"#;
    ///
    /// let resp = serde_json::from_str::<Response>(&payload).unwrap();
    /// let user = resp.values::<UserCompact>();
    /// assert!(user.is_some());
    /// assert_eq!(user.unwrap()[0].name, "Greg Sanchez");
    /// ```
    pub fn values<T: DeserializeOwned>(&self) -> Option<Vec<T>> {
        if let Some(value) = self.value::<T>() {
            Some(vec![value])
        } else {
            serde_json::from_value::<Vec<T>>(self.data.clone()).ok()
        }
    }
}



#[cfg(test)]
mod tests {
    use super::*;

    fn test_value_resp() -> &'static str {
        return r#"{
            "data": {
                "gid": "12345",
                "resource_type": "user",
                "name": "Greg Sanchez"
            }
        }"#;
    }

    fn test_vector_resp() -> &'static str {
        return r#"{
            "data": [
                {
                    "gid": "12345",
                    "resource_type": "user",
                    "name": "Greg Sanchez"
                },
                {
                    "gid": "54321",
                    "resource_type": "user",
                    "name": "Luke Lastname"
                }
            ]
        }"#;
    }

    fn test_error_resp() -> &'static str {
        return r#"{
            "errors": [
                {
                    "help": "For more information on API status codes and how to handle them, read the docs on errors: https://asana.github.io/developer-docs/#errors'",
                    "message": "project: Missing input",
                    "phrase": "6 sad squid snuggle softly"
                }
            ]
        }"#;
    }

    #[test]
    fn test_basic_deserialize_value() {
        let payload = test_value_resp();
        let resp = serde_json::from_str::<Response>(payload);
        assert!(resp.is_ok());
    }

    #[test]
    fn test_basic_deserialize_vector() {
        let payload = test_vector_resp();
        let resp = serde_json::from_str::<Response>(payload);
        assert!(resp.is_ok());
    }

    #[test]
    fn test_basic_deserialize_errors() {
        let payload = test_error_resp();
        let resp = serde_json::from_str::<Response>(payload);
        assert!(resp.is_ok());
    }

    #[test]
    fn test_deserialize_to_resource() {
        let payload = test_value_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        let resource = resp.into::<schema::UserCompact>().unwrap();
        assert_eq!(resource.name, "Greg Sanchez");
        assert_eq!(resource.gid, "12345");
    }

    #[test]
    fn test_deserialize_to_vector_resources() {
        let payload = test_vector_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        let resources = resp.into::<Vec<schema::UserCompact>>().unwrap();
        assert_eq!(resources.len(), 2);
        assert_eq!(resources[0].name, "Greg Sanchez");
        assert_eq!(resources[0].gid, "12345");
    }

    #[test]
    fn test_deserialize_asana_errors() {
        let payload = test_error_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        let errors = resp.into::<schema::UserCompact>().unwrap_err();
        assert_eq!(errors.len(), 1);
        let first_error = errors[0].clone();
        assert_eq!(first_error.message.unwrap(), "project: Missing input");
    }

    #[test]
    fn test_get_value() {
        let payload = test_value_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        if let Some(user) = resp.value::<schema::UserCompact>() {
            assert_eq!(user.name, "Greg Sanchez");
        } else {
            assert!(false);
        }
        assert!(resp.errors().is_none());
    }

    #[test]
    fn test_get_vector_values() {
        let payload = test_vector_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        if let Some(users) = resp.values::<schema::UserCompact>() {
            assert_eq!(users.len(), 2);
            assert_eq!(users[0].name, "Greg Sanchez");
        } else {
            assert!(false);
        }
        assert!(resp.errors().is_none());
    }

    #[test]
    fn test_get_errors() {
        let payload = test_error_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        assert!(resp.values::<schema::UserCompact>().is_none());
        if let Some(errors) = resp.errors() {
            assert_eq!(errors.len(), 1);
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_value_to_values() {
        // This returns a single value, not a vector
        let payload = test_value_resp();
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        // I want resp.values() to convert to a vector of one item
        if let Some(users) = resp.values::<schema::UserCompact>() {
            assert_eq!(users.len(), 1);
            assert_eq!(users[0].name, "Greg Sanchez");
        } else {
            assert!(false);
        }
    }

    #[test]
    fn test_empty_return_vector() {
        let payload = r#"{ "data": [] }"#;
        let resp = serde_json::from_str::<Response>(payload).unwrap();
        let items = resp.values::<schema::UserCompact>();
        assert!(items.is_some());
        assert_eq!(items.unwrap().len(), 0);
    }
}