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::io::Read;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
/// Body type for a request.
|
/// Body type for a request.
|
||||||
pub struct Body {
|
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`.
|
// Wraps a `std::io::Write`.
|
||||||
//pub struct Pipe(Kind);
|
//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 {
|
match body.reader {
|
||||||
Kind::Bytes(ref bytes) => {
|
Kind::Bytes(ref bytes) => {
|
||||||
let len = bytes.len();
|
let len = bytes.len();
|
||||||
::hyper::client::Body::BufBody(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())
|
let mut req = client.inner.request(method.clone(), url.clone())
|
||||||
.headers(headers.clone());
|
.headers(headers.clone());
|
||||||
|
|
||||||
if let Some(ref b) = body {
|
if let Some(ref mut b) = body {
|
||||||
let body = body::as_hyper_body(&b);
|
let body = body::as_hyper_body(b);
|
||||||
req = req.body(body);
|
req = req.body(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user