2025-01-18 21:27:14 +01:00
|
|
|
use http::Request;
|
|
|
|
use http_body_util::BodyExt;
|
|
|
|
use hyper::body::Incoming;
|
|
|
|
use serde::de::DeserializeOwned;
|
2025-01-23 17:14:37 +01:00
|
|
|
use std::error::Error;
|
2025-01-18 21:27:14 +01:00
|
|
|
|
|
|
|
pub struct Requester;
|
|
|
|
|
|
|
|
impl Requester {
|
2025-01-18 21:35:15 +01:00
|
|
|
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error + Send + Sync>>
|
2025-01-18 21:27:14 +01:00
|
|
|
where
|
2025-01-23 17:14:37 +01:00
|
|
|
T: DeserializeOwned,
|
2025-01-18 21:27:14 +01:00
|
|
|
{
|
2025-01-23 16:06:44 +01:00
|
|
|
let body = req.collect().await?.to_bytes();
|
2025-01-18 21:27:14 +01:00
|
|
|
Ok(serde_json::from_slice(&body)?)
|
|
|
|
}
|
2025-01-23 17:14:37 +01:00
|
|
|
|
|
|
|
pub async fn extract_body_str(
|
|
|
|
req: Request<Incoming>,
|
|
|
|
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
|
|
|
let body = req.collect().await?.to_bytes();
|
2025-01-23 17:15:50 +01:00
|
|
|
Ok(String::from_utf8(body.to_vec())?)
|
2025-01-23 17:14:37 +01:00
|
|
|
}
|
2025-01-18 21:27:14 +01:00
|
|
|
}
|