use crate::transport::InfraClientError;
pub(crate) async fn read_body(
mut response: reqwest::Response,
max_response_bytes: usize,
service: &'static str,
) -> Result<Vec<u8>, InfraClientError> {
if response
.content_length()
.is_some_and(|size| size > max_response_bytes as u64)
{
return Err(InfraClientError::ResponseTooLarge {
service,
limit: max_response_bytes,
});
}
let mut body = Vec::with_capacity(
response
.content_length()
.unwrap_or_default()
.min(max_response_bytes as u64) as usize,
);
while let Some(chunk) = response
.chunk()
.await
.map_err(|source| InfraClientError::Request { service, source })?
{
if body.len().saturating_add(chunk.len()) > max_response_bytes {
return Err(InfraClientError::ResponseTooLarge {
service,
limit: max_response_bytes,
});
}
body.extend_from_slice(&chunk);
}
Ok(body)
}