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
//! Response types

use std::collections::HashMap;
use std::ops::Not;

/// Representation of API Gateway response
///
/// # Examples
///
/// ```
/// # use gateway::Response;
/// let response: Response = Response::default();
/// assert!(response.status_code() == 200);
/// assert!(response.headers().is_empty());
/// assert!(response.body() == "");
/// ```
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Response {
    status_code: u16,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    headers: HashMap<String, String>,
    #[serde(skip_serializing_if = "String::is_empty")]
    body: String,
    #[serde(skip_serializing_if = "Not::not")]
    is_base64_encoded: bool,
}

impl Default for Response {
    fn default() -> Self {
        Response::builder().build()
    }
}

impl Response {
    pub fn builder() -> Builder {
        Builder::new()
    }

    pub fn status_code(&self) -> u16 {
        self.status_code
    }

    pub fn headers(&self) -> &HashMap<String, String> {
        &self.headers
    }

    pub fn body(&self) -> &String {
        &self.body
    }

    pub fn is_base64_encoded(&self) -> bool {
        self.is_base64_encoded
    }
}

/// An HTTP response builder
///
/// You will typically should create one with `Response::builder()`
#[derive(Debug, Default)]
pub struct Builder {
    status_code: u16,
    headers: HashMap<String, String>,
    body: String,
    is_base64_encoded: bool,
}

impl Builder {
    pub fn new() -> Self {
        Self {
            status_code: 200,
            ..Default::default()
        }
    }

    pub fn status_code(&mut self, c: u16) -> &mut Self {
        self.status_code = c;
        self
    }

    /// Set response body. If body is base64 encoded
    /// set `base64_encoded(true)`.
    pub fn body<B>(&mut self, b: B) -> &mut Self
    where
        B: Into<String>,
    {
        self.body = b.into();
        self
    }

    pub fn header<K, V>(&mut self, k: K, v: V) -> &mut Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.headers.insert(k.into(), v.into());
        self
    }

    /// Sets a hint to API gateway that response body is base64 encoded
    pub fn base64_encoded(&mut self, is: bool) -> &mut Self {
        self.is_base64_encoded = is;
        self
    }

    pub fn build(&self) -> Response {
        Response {
            status_code: self.status_code,
            headers: self.headers.clone(),
            body: self.body.clone(),
            is_base64_encoded: self.is_base64_encoded,
        }
    }
}

#[cfg(test)]
mod tests {

    use Response;
    use serde_json;

    #[test]
    fn default_response() {
        assert_eq!(Response::default().status_code, 200)
    }
    #[test]
    fn builder_body() {
        assert_eq!(Response::builder().body("foo").build().body, "foo")
    }
    #[test]
    fn serialize_default() {
        assert_eq!(
            serde_json::to_string(&Response::default()).expect("failed to serialize response"),
            r#"{"statusCode":200}"#
        );
    }

    #[test]
    fn serialize_body() {
        assert_eq!(
            serde_json::to_string(&Response::builder().body("foo").build())
                .expect("failed to serialize response"),
            r#"{"statusCode":200,"body":"foo"}"#
        );
    }
}