Skip to main content

oxidite_core/
request.rs

1use crate::error::{Error, Result};
2use crate::types::OxiditeRequest;
3use http_body_util::BodyExt;
4
5/// Extension trait for Request to provide helper methods
6pub trait RequestExt {
7    /// Read the entire body as a String
8    fn body_string(&mut self) -> impl std::future::Future<Output = Result<String>> + Send;
9    
10    /// Read the entire body as Bytes
11    fn body_bytes(&mut self) -> impl std::future::Future<Output = Result<bytes::Bytes>> + Send;
12}
13
14impl RequestExt for OxiditeRequest {
15    async fn body_string(&mut self) -> Result<String> {
16        let bytes = self.body_bytes().await?;
17        String::from_utf8(bytes.to_vec())
18            .map_err(|e| Error::BadRequest(format!("Invalid UTF-8: {}", e)))
19    }
20
21    async fn body_bytes(&mut self) -> Result<bytes::Bytes> {
22        let body = self.body_mut();
23        let collected = body.collect().await
24            .map_err(|e| Error::InternalServerError(format!("Failed to read body: {}", e)))?;
25        Ok(collected.to_bytes())
26    }
27}