Validator API (#504)

* Implemented more REST API endpoints.
 - Added many of the endpoints, which return 501 - Not Implemented
 - Created helper function to return a not implemented error
 - Created a parse_pubkey function to get a PublicKey from a hex string
 - Created a HTTP handler for the validator endpoints
 - Started implementing validator/duties endpoint.

* Fleshed out get validator duties.
 - Re-implemented the get validator duties function for the REST API
 - Added an 'as_hex_string' function to FakePublicKey, beacuse it was missing.

* Fixed small caching/state bug.

* Extended to array of API inputs.
 - Created  function for getting arrays from GET parameters.
 - Extended get validator duties function to support array of validator duties.

* Tidy API to be more consistent with recent decisions

* Addressing Paul's comments.
 - Cleaning up function to get list of proposers.
 - Removing unnecessary serde annotations
 - Clarifying error messages
 - Only accept pubkeys if they are '0x' prefixed.

* Fixed formatting with rustfmt.
This commit is contained in:
Luke Anderson
2019-08-28 00:40:35 +10:00
parent ce43a59fff
commit 328f11d564
7 changed files with 253 additions and 6 deletions

View File

@@ -2,6 +2,8 @@ use crate::ApiError;
use hyper::Request;
/// Provides handy functions for parsing the query parameters of a URL.
#[derive(Clone, Copy)]
pub struct UrlQuery<'a>(url::form_urlencoded::Parse<'a>);
impl<'a> UrlQuery<'a> {
@@ -11,9 +13,7 @@ impl<'a> UrlQuery<'a> {
pub fn from_request<T>(req: &'a Request<T>) -> Result<Self, ApiError> {
let query_str = req.uri().query().ok_or_else(|| {
ApiError::InvalidQueryParams(
"URL query must be valid and contain at least one
key."
.to_string(),
"URL query must be valid and contain at least one key.".to_string(),
)
})?;
@@ -60,6 +60,23 @@ impl<'a> UrlQuery<'a> {
)))
}
}
/// Returns a vector of all values present where `key` is in `keys
///
/// If no match is found, an `InvalidQueryParams` error is returned.
pub fn all_of(mut self, key: &str) -> Result<Vec<String>, ApiError> {
let queries: Vec<_> = self
.0
.filter_map(|(k, v)| {
if k.eq(key) {
Some(v.into_owned())
} else {
None
}
})
.collect();
Ok(queries)
}
}
#[cfg(test)]