use std::io::{self, Cursor};
use std::sync::Arc;
use ureq_proto::BodyMode;
use super::{Body, BodyDataSource, ContentEncoding, ResponseInfo};
pub struct BodyBuilder {
info: ResponseInfo,
limit: Option<u64>,
}
impl BodyBuilder {
pub(crate) fn new() -> Self {
BodyBuilder {
info: ResponseInfo {
content_encoding: ContentEncoding::None,
mime_type: None,
charset: None,
body_mode: BodyMode::NoBody,
},
limit: None,
}
}
pub fn mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.info.mime_type = Some(mime_type.into());
self
}
pub fn charset(mut self, charset: impl Into<String>) -> Self {
self.info.charset = Some(charset.into());
self
}
pub fn limit(mut self, l: u64) -> Self {
self.limit = Some(l);
self
}
pub fn data(mut self, data: impl Into<Vec<u8>>) -> Body {
let data: Vec<u8> = data.into();
let len = self.limit.unwrap_or(data.len() as u64);
self.info.body_mode = BodyMode::LengthDelimited(len);
self.reader(Cursor::new(data))
}
pub fn reader(self, data: impl io::Read + Send + Sync + 'static) -> Body {
Body {
source: BodyDataSource::Reader(Box::new(data)),
info: Arc::new(self.info),
}
}
}