Body conversion for File
This commit is contained in:
21
src/body.rs
21
src/body.rs
@@ -1,4 +1,5 @@
|
||||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
|
||||
/// Body type for a request.
|
||||
pub struct Body {
|
||||
@@ -60,19 +61,33 @@ impl<'a> From<&'a str> for Body {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<File> for Body {
|
||||
#[inline]
|
||||
fn from(f: File) -> Body {
|
||||
let len = f.metadata().map(|m| m.len()).ok();
|
||||
Body {
|
||||
reader: Kind::Reader(Box::new(f), len),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Wraps a `std::io::Write`.
|
||||
//pub struct Pipe(Kind);
|
||||
|
||||
|
||||
pub fn as_hyper_body<'a>(body: &'a Body) -> ::hyper::client::Body<'a> {
|
||||
pub fn as_hyper_body<'a>(body: &'a mut Body) -> ::hyper::client::Body<'a> {
|
||||
match body.reader {
|
||||
Kind::Bytes(ref bytes) => {
|
||||
let len = bytes.len();
|
||||
::hyper::client::Body::BufBody(bytes, len)
|
||||
},
|
||||
Kind::Reader(..) => unimplemented!()
|
||||
}
|
||||
Kind::Reader(ref mut reader, len_opt) => {
|
||||
match len_opt {
|
||||
Some(len) => ::hyper::client::Body::SizedBody(reader, len),
|
||||
None => ::hyper::client::Body::ChunkedBody(reader),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -167,8 +167,8 @@ impl<'a> RequestBuilder<'a> {
|
||||
let mut req = client.inner.request(method.clone(), url.clone())
|
||||
.headers(headers.clone());
|
||||
|
||||
if let Some(ref b) = body {
|
||||
let body = body::as_hyper_body(&b);
|
||||
if let Some(ref mut b) = body {
|
||||
let body = body::as_hyper_body(b);
|
||||
req = req.body(body);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user