Updated content-type acceptance and returning, mainly for /spec/eth2_config

This commit is contained in:
Luke Anderson 2019-09-05 00:36:06 +10:00
parent bf2f459773
commit 32ca8e951d
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
2 changed files with 22 additions and 18 deletions

View File

@ -276,10 +276,7 @@ pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
Ok(success_response(Body::from(json))) Ok(success_response(Body::from(json)))
} }
/// HTTP handler to return a `BeaconState` at a given `root` or `slot`. /// HTTP handler to return a `BeaconState` at the genesis block.
///
/// Will not return a state if the request slot is in the future. Will return states higher than
/// the current head by skipping slots.
pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult { pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?; let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;

View File

@ -26,24 +26,31 @@ impl ResponseBuilder {
} }
pub fn body<T: Serialize + Encode>(self, item: &T) -> ApiResult { pub fn body<T: Serialize + Encode>(self, item: &T) -> ApiResult {
let body: Body = match self.encoding { let (body, content_type) = match self.encoding {
Encoding::JSON => Body::from(serde_json::to_string(&item).map_err(|e| { Encoding::JSON => (
ApiError::ServerError(format!( Body::from(serde_json::to_string(&item).map_err(|e| {
"Unable to serialize response body as JSON: {:?}", ApiError::ServerError(format!(
e "Unable to serialize response body as JSON: {:?}",
)) e
})?), ))
Encoding::SSZ => Body::from(item.as_ssz_bytes()), })?),
Encoding::YAML => Body::from(serde_yaml::to_string(&item).map_err(|e| { "application/json",
ApiError::ServerError(format!( ),
"Unable to serialize response body as YAML: {:?}", Encoding::SSZ => (Body::from(item.as_ssz_bytes()), "application/ssz"),
e Encoding::YAML => (
)) Body::from(serde_yaml::to_string(&item).map_err(|e| {
})?), ApiError::ServerError(format!(
"Unable to serialize response body as YAML: {:?}",
e
))
})?),
"application/ssz",
),
}; };
Response::builder() Response::builder()
.status(StatusCode::OK) .status(StatusCode::OK)
.header("content-type", content_type)
.body(Body::from(body)) .body(Body::from(body))
.map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e))) .map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e)))
} }