use &[u8] instead of Bytes for requests

This commit is contained in:
Greg Johnston 2024-01-19 12:08:27 -05:00
parent 2fa60103b4
commit e179db1d42
5 changed files with 12 additions and 14 deletions

View file

@ -29,12 +29,7 @@ where
let mut buffer: Vec<u8> = Vec::new();
ciborium::ser::into_writer(&self, &mut buffer)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
Request::try_new_post_bytes(
path,
accepts,
Cbor::CONTENT_TYPE,
Bytes::from(buffer),
)
Request::try_new_post_bytes(path, accepts, Cbor::CONTENT_TYPE, &buffer)
}
}

View file

@ -35,8 +35,12 @@ where
) -> Result<Request, ServerFnError<CustErr>> {
let encoded = rkyv::to_bytes::<T, 1024>(&self)
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
let bytes = Bytes::copy_from_slice(encoded.as_ref());
Request::try_new_post_bytes(path, accepts, Rkyv::CONTENT_TYPE, bytes)
Request::try_new_post_bytes(
path,
accepts,
Rkyv::CONTENT_TYPE,
encoded.as_ref(),
)
}
}
@ -50,7 +54,7 @@ where
{
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
let body_bytes = req.try_into_bytes().await?;
rkyv::from_bytes::<T>(body_bytes.as_ref())
rkyv::from_bytes::<T>(&body_bytes)
.map_err(|e| ServerFnError::Args(e.to_string()))
}
}

View file

@ -78,13 +78,12 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
path: &str,
accepts: &str,
content_type: &str,
body: Bytes,
body: &[u8],
) -> Result<Self, ServerFnError<CustErr>> {
let server_url = get_server_url();
let mut url = String::with_capacity(server_url.len() + path.len());
url.push_str(server_url);
url.push_str(path);
let body: &[u8] = &body;
let body = Uint8Array::from(body).buffer();
Ok(Self(SendWrapper::new(
Request::post(&url)

View file

@ -45,7 +45,7 @@ where
path: &str,
content_type: &str,
accepts: &str,
body: Bytes,
body: &[u8],
) -> Result<Self, ServerFnError<CustErr>>;
/// Attempts to construct a new `POST` request with form data as the body.

View file

@ -56,14 +56,14 @@ impl<CustErr> ClientReq<CustErr> for Request {
path: &str,
accepts: &str,
content_type: &str,
body: Bytes,
body: &[u8],
) -> Result<Self, ServerFnError<CustErr>> {
let url = format!("{}{}", get_server_url(), path);
CLIENT
.post(url)
.header(CONTENT_TYPE, content_type)
.header(ACCEPT, accepts)
.body(body)
.body(body.to_owned())
.build()
.map_err(|e| ServerFnError::Request(e.to_string()))
}