adding requester to extract json body
This commit is contained in:
parent
e9e3b05dc5
commit
6fe4eb1956
@ -1,7 +1,9 @@
|
|||||||
mod builder;
|
mod builder;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod requester;
|
||||||
mod responder;
|
mod responder;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
|
pub use requester::Requester;
|
||||||
pub use responder::Responder;
|
pub use responder::Responder;
|
||||||
pub use server::Server;
|
pub use server::Server;
|
||||||
|
17
src/requester.rs
Normal file
17
src/requester.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use http::Request;
|
||||||
|
use http_body_util::BodyExt;
|
||||||
|
use hyper::body::Incoming;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
pub struct Requester;
|
||||||
|
|
||||||
|
impl Requester {
|
||||||
|
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error>>
|
||||||
|
where
|
||||||
|
T: DeserializeOwned,
|
||||||
|
{
|
||||||
|
let body = req.collect().await.unwrap().to_bytes();
|
||||||
|
Ok(serde_json::from_slice(&body)?)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user