Make API friendly to block explorers (#702)

* Add validator index to duties response

* Add `get_state` method to beacon chain

* Improve /beacon/validators endpoint

* Add validators/all and validators/active endpoints

* Start refactor of HTTP docs

* Document /beacon/heads endpoint

* Remove some unused API endpoints

* Improve API docs

* Add methods to get all validator duties

* Improve docs

* Remove dead links

* Make tables left-justified

* Add /consensus/vote_count endpoint

* Add /consensus/individual_votes endpoint

* Update formatting

* Tidy

* Add committees endpoint

* Strictly require 0x prefix for serde in BLS

* Update docs to have 0x prefix

* Fix failing tests

* Add unfinished code

* Improve testing, fix bugs

* Tidy, ensure all beacon endpoints smoke tested

* Fix pubkey cache error

* Address comments with docs
This commit is contained in:
Paul Hauner
2019-12-19 11:45:28 +11:00
committed by GitHub
parent d756bc9ecd
commit 251aea645c
28 changed files with 1831 additions and 268 deletions

View File

@@ -13,11 +13,7 @@ impl<'a> UrlQuery<'a> {
///
/// Returns `Err` if `req` does not contain any query parameters.
pub fn from_request<T>(req: &'a Request<T>) -> Result<Self, ApiError> {
let query_str = req.uri().query().ok_or_else(|| {
ApiError::BadRequest(
"URL query must be valid and contain at least one key.".to_string(),
)
})?;
let query_str = req.uri().query().unwrap_or_else(|| "");
Ok(UrlQuery(url::form_urlencoded::parse(query_str.as_bytes())))
}
@@ -31,12 +27,21 @@ impl<'a> UrlQuery<'a> {
.map(|(key, value)| (key.into_owned(), value.into_owned()))
.ok_or_else(|| {
ApiError::BadRequest(format!(
"URL query must contain at least one of the following keys: {:?}",
"URL query must be valid and contain at least one of the following keys: {:?}",
keys
))
})
}
/// Returns the first `(key, value)` pair found where the `key` is in `keys`, if any.
///
/// Returns `None` if no match is found.
pub fn first_of_opt(mut self, keys: &[&str]) -> Option<(String, String)> {
self.0
.find(|(key, _value)| keys.contains(&&**key))
.map(|(key, value)| (key.into_owned(), value.into_owned()))
}
/// Returns the value for `key`, if and only if `key` is the only key present in the query
/// parameters.
pub fn only_one(self, key: &str) -> Result<String, ApiError> {