2019-09-02 04:29:36 +00:00
|
|
|
use super::{ApiError, ApiResult};
|
|
|
|
use http::header;
|
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
|
|
|
use serde::Serialize;
|
|
|
|
use ssz::Encode;
|
|
|
|
|
|
|
|
pub enum Encoding {
|
|
|
|
JSON,
|
|
|
|
SSZ,
|
|
|
|
YAML,
|
2019-09-13 10:42:56 +00:00
|
|
|
TEXT,
|
2019-09-02 04:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ResponseBuilder {
|
|
|
|
encoding: Encoding,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseBuilder {
|
2019-09-13 10:42:56 +00:00
|
|
|
pub fn new(req: &Request<Body>) -> Result<Self, ApiError> {
|
|
|
|
let content_header = req
|
|
|
|
.headers()
|
|
|
|
.get(header::CONTENT_TYPE)
|
|
|
|
.map_or(Ok(""), |h| h.to_str())
|
|
|
|
.map_err(|e| {
|
|
|
|
ApiError::InvalidQueryParams(format!(
|
|
|
|
"The content-type header contains invalid characters: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.map(|h| String::from(h))?;
|
|
|
|
|
|
|
|
let encoding = match content_header {
|
|
|
|
ref h if h.starts_with("application/ssz") => Encoding::SSZ,
|
|
|
|
ref h if h.starts_with("application/yaml") => Encoding::YAML,
|
|
|
|
ref h if h.starts_with("text/plain") => Encoding::TEXT,
|
2019-09-02 04:29:36 +00:00
|
|
|
_ => Encoding::JSON,
|
|
|
|
};
|
2019-09-13 10:42:56 +00:00
|
|
|
Ok(Self { encoding })
|
2019-09-02 04:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn body<T: Serialize + Encode>(self, item: &T) -> ApiResult {
|
2019-09-13 10:42:56 +00:00
|
|
|
match self.encoding {
|
|
|
|
Encoding::SSZ => Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header("content-type", "application/ssz")
|
|
|
|
.body(Body::from(item.as_ssz_bytes()))
|
|
|
|
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e))),
|
|
|
|
_ => self.body_no_ssz(item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn body_no_ssz<T: Serialize>(self, item: &T) -> ApiResult {
|
2019-09-04 14:36:06 +00:00
|
|
|
let (body, content_type) = match self.encoding {
|
2019-09-13 10:42:56 +00:00
|
|
|
Encoding::JSON => (
|
|
|
|
Body::from(serde_json::to_string(&item).map_err(|e| {
|
|
|
|
ApiError::ServerError(format!(
|
|
|
|
"Unable to serialize response body as JSON: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})?),
|
|
|
|
"application/json",
|
|
|
|
),
|
|
|
|
Encoding::SSZ => {
|
|
|
|
return Err(ApiError::UnsupportedType(
|
|
|
|
"Response cannot be encoded as SSZ.".into(),
|
|
|
|
));
|
2019-09-10 14:40:22 +00:00
|
|
|
}
|
2019-09-04 14:36:06 +00:00
|
|
|
Encoding::YAML => (
|
|
|
|
Body::from(serde_yaml::to_string(&item).map_err(|e| {
|
|
|
|
ApiError::ServerError(format!(
|
|
|
|
"Unable to serialize response body as YAML: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})?),
|
2019-09-13 10:42:56 +00:00
|
|
|
"application/yaml",
|
2019-09-04 14:36:06 +00:00
|
|
|
),
|
2019-09-13 10:42:56 +00:00
|
|
|
Encoding::TEXT => {
|
|
|
|
return Err(ApiError::UnsupportedType(
|
|
|
|
"Response cannot be encoded as plain text.".into(),
|
|
|
|
));
|
|
|
|
}
|
2019-09-02 04:29:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
2019-09-04 14:36:06 +00:00
|
|
|
.header("content-type", content_type)
|
2019-09-02 04:29:36 +00:00
|
|
|
.body(Body::from(body))
|
|
|
|
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e)))
|
|
|
|
}
|
2019-09-10 14:40:22 +00:00
|
|
|
|
2019-09-13 09:38:40 +00:00
|
|
|
pub fn body_text(self, text: String) -> ApiResult {
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header("content-type", "text/plain; charset=utf-8")
|
|
|
|
.body(Body::from(text))
|
|
|
|
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e)))
|
|
|
|
}
|
2019-09-02 04:29:36 +00:00
|
|
|
}
|