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:
Luke Anderson
2019-09-11 00:40:22 +10:00
parent b0090df543
commit b8667217f0
10 changed files with 143 additions and 138 deletions

View File

@@ -1,25 +1,23 @@
use crate::helpers::get_beacon_chain_from_request;
use crate::{success_response, ApiResult};
use crate::helpers::*;
use crate::{ApiResult, BoxFut};
use beacon_chain::BeaconChainTypes;
use hyper::{Body, Request};
use version;
/// Read the version string from the current Lighthouse build.
pub fn get_version(_req: Request<Body>) -> ApiResult {
let body = Body::from(
serde_json::to_string(&version::version())
.expect("Version should always be serialializable as JSON."),
);
Ok(success_response(body))
pub fn get_version(req: Request<Body>) -> BoxFut {
success_response_json(req, &version::version())
}
/// Read the genesis time from the current beacon chain state.
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> BoxFut {
let bc = get_beacon_chain_from_request::<T>(&req);
let (_beacon_chain, head_state) = match bc {
Ok((bc, hs)) => (bc, hs),
Err(e) => {
return e.into();
}
};
let gen_time: u64 = head_state.genesis_time;
let body = Body::from(
serde_json::to_string(&gen_time)
.expect("Genesis should time always have a valid JSON serialization."),
);
Ok(success_response(body))
success_response(req, &gen_time)
}