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
use std::result::Result as StdResult;

use reqwest::Error as ReqwestError;
use reqwest::Response;
use serde::{Serialize, Deserialize};

use {Error, Result};
use errors::RestError;

/// Marker trait for types that are meant to use in PATCH requests.
pub trait Patch: Default + Serialize {}

/// Validator and deserializer for REST API responses.
pub trait ApiResponse : Sized {
    /// Checks if the status code returned was in the 2xx range. If so,
    /// returns the underlying response for further processing; otherwise
    /// returns an error.
    fn validate_status(self) -> Result<Response>;
    
    /// Attempts to parse the response as JSON into `T`.
    fn deserialize<T: Deserialize>(self) -> Result<T>;
    
    /// Validates the status code and attempts to deserialize into `T` in one step.
    fn validate_and_read<T: Deserialize>(self) -> Result<T> {
        self.validate_status()?.deserialize()
    }
}

impl ApiResponse for Response {
    fn validate_status(mut self) -> Result<Response> {
        if !self.status().is_success() {
            Err(RestError::new(self.status().clone(), self.json().ok()).into())
        } else {
            Ok(self)
        }
    }
    
    fn deserialize<T: Deserialize>(self) -> Result<T> {
        self.validate_status()?.json().map_err(Error::from)
    }
}

impl ApiResponse for StdResult<Response, ReqwestError> {
    fn validate_status(self) -> Result<Response> {
        self.map_err(Error::from).and_then(ApiResponse::validate_status)
    }
    
    fn deserialize<T: Deserialize>(self) -> Result<T> {
        self.validate_status().and_then(ApiResponse::deserialize)
    }
}