adding requester to extract json body

This commit is contained in:
midefos 2025-01-18 21:27:14 +01:00
parent e9e3b05dc5
commit 6fe4eb1956
2 changed files with 19 additions and 0 deletions

View File

@ -1,7 +1,9 @@
mod builder;
mod config;
mod requester;
mod responder;
mod server;
pub use requester::Requester;
pub use responder::Responder;
pub use server::Server;

17
src/requester.rs Normal file
View 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)?)
}
}