mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-11 18:04:18 +00:00
Made async functions work!
- Cleaned up imports - Moved ApiError and such to it's own error.rs - Obsoleted 'success_response' in favour of new async regular and json only flavours - Made ApiError work async and be derived from hyper errors - Added a check to ensure an error is thrown if a non-json encoding is requested on a json-only function - Made all the individual service functions return futures (only node and network for now)
This commit is contained in:
@@ -27,15 +27,9 @@ impl ResponseBuilder {
|
||||
|
||||
pub fn body<T: Serialize + Encode>(self, item: &T) -> ApiResult {
|
||||
let (body, content_type) = match self.encoding {
|
||||
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::JSON => {
|
||||
return self.body_json(item);
|
||||
}
|
||||
Encoding::SSZ => (Body::from(item.as_ssz_bytes()), "application/ssz"),
|
||||
Encoding::YAML => (
|
||||
Body::from(serde_yaml::to_string(&item).map_err(|e| {
|
||||
@@ -54,4 +48,17 @@ impl ResponseBuilder {
|
||||
.body(Body::from(body))
|
||||
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e)))
|
||||
}
|
||||
|
||||
pub fn body_json<T: Serialize>(self, item: &T) -> ApiResult {
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header("content-type", "application/json")
|
||||
.body(Body::from(serde_json::to_string(&item).map_err(|e| {
|
||||
ApiError::ServerError(format!(
|
||||
"Unable to serialize response body as JSON: {:?}",
|
||||
e
|
||||
))
|
||||
})?))
|
||||
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user