From 6fe4eb1956e23b8d75d1f831c8ad830afba8de6d Mon Sep 17 00:00:00 2001 From: midefos Date: Sat, 18 Jan 2025 21:27:14 +0100 Subject: [PATCH] adding requester to extract json body --- src/lib.rs | 2 ++ src/requester.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/requester.rs diff --git a/src/lib.rs b/src/lib.rs index 7168272..133fb5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/requester.rs b/src/requester.rs new file mode 100644 index 0000000..7f972e1 --- /dev/null +++ b/src/requester.rs @@ -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(req: Request) -> Result> + where + T: DeserializeOwned, + { + let body = req.collect().await.unwrap().to_bytes(); + Ok(serde_json::from_slice(&body)?) + } +}