mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-15 19:02:42 +00:00
* and_then to then remove expect move convert_rejection to utils remove signer from vc api * remove key * remove auth header * revert * Merge branch 'unstable' of https://github.com/sigp/lighthouse into vc-api-fix * merge unstable * revert * Merge branch 'unstable' of https://github.com/sigp/lighthouse into vc-api-fix * Merge branch 'unstable' of https://github.com/sigp/lighthouse into vc-api-fix * refactor blocking json task * linting * revert logging * remove response signing checks in validtor http_api client * remove notion of public key, prefixes, and simplify token generation * fmt * Remove outdated comment on public key
41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
use crate::reject::convert_rejection;
|
|
use serde::Serialize;
|
|
use warp::reply::{Reply, Response};
|
|
|
|
/// A convenience wrapper around `blocking_task`.
|
|
pub async fn blocking_task<F, T>(func: F) -> Result<T, warp::Rejection>
|
|
where
|
|
F: FnOnce() -> Result<T, warp::Rejection> + Send + 'static,
|
|
T: Send + 'static,
|
|
{
|
|
tokio::task::spawn_blocking(func)
|
|
.await
|
|
.unwrap_or_else(|_| Err(warp::reject::reject()))
|
|
}
|
|
|
|
/// A convenience wrapper around `blocking_task` that returns a `warp::reply::Response`.
|
|
///
|
|
/// Using this method consistently makes it possible to simplify types using `.unify()` or `.uor()`.
|
|
pub async fn blocking_response_task<F, T>(func: F) -> Result<Response, warp::Rejection>
|
|
where
|
|
F: FnOnce() -> Result<T, warp::Rejection> + Send + 'static,
|
|
T: Reply + Send + 'static,
|
|
{
|
|
blocking_task(func).await.map(Reply::into_response)
|
|
}
|
|
|
|
/// A convenience wrapper around `blocking_task` for use with `warp` JSON responses.
|
|
pub async fn blocking_json_task<F, T>(func: F) -> Response
|
|
where
|
|
F: FnOnce() -> Result<T, warp::Rejection> + Send + 'static,
|
|
T: Serialize + Send + 'static,
|
|
{
|
|
let result = blocking_response_task(|| {
|
|
let response = func()?;
|
|
Ok(warp::reply::json(&response))
|
|
})
|
|
.await;
|
|
|
|
convert_rejection(result).await
|
|
}
|