mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-02-23 15:49:49 +01:00
feat(doit): multi-part mime-type and add_parts()
Next we will implement the actual Read method
This commit is contained in:
@@ -461,6 +461,7 @@ match result {
|
||||
## "the trait `core::marker::Sized` is not implemented for the type `std::io::Read`"
|
||||
use hyper::client::IntoBody;
|
||||
use std::io::{Read, Seek};
|
||||
use hyper::header::{ContentType, ContentLength};
|
||||
let mut params: Vec<(&str, String)> = Vec::with_capacity(${len(params) + len(reserved_params)} + ${paddfields}.len());
|
||||
% if response_schema:
|
||||
params.push(("alt", "json".to_string()));
|
||||
@@ -614,14 +615,14 @@ else {
|
||||
% if request_value and simple_media_param:
|
||||
let mut request_value_reader = io::Cursor::new(json_encoded_request.clone().into_bytes());
|
||||
let mut mp_reader: cmn::MultiPartReader = Default::default();
|
||||
let mut content_type = hyper::header::ContentType(json_mime_type.clone());
|
||||
let mut content_type = ContentType(json_mime_type.clone());
|
||||
let mut body_reader: &mut io::Read = match ${simple_media_param.type.arg_name}.as_mut() {
|
||||
Some(&mut (ref mut reader, size, ref mime)) => {
|
||||
let rsize = request_value_reader.seek(io::SeekFrom::End(0)).unwrap();
|
||||
request_value_reader.seek(io::SeekFrom::Start(0)).ok();
|
||||
mp_reader = mp_reader.add_part(&mut request_value_reader, rsize, &json_mime_type)
|
||||
.add_part(reader, size, mime);
|
||||
## TODO: content_type = multi-part
|
||||
content_type = ContentType(mp_reader.mime_type());
|
||||
&mut mp_reader
|
||||
}
|
||||
None => &mut request_value_reader,
|
||||
@@ -637,8 +638,8 @@ else {
|
||||
% if request_value:
|
||||
% if not simple_media_param:
|
||||
|
||||
.header(hyper::header::ContentType(json_mime_type.clone()))
|
||||
.header(hyper::header::ContentLength(json_encoded_request.len() as u64))
|
||||
.header(ContentType(json_mime_type.clone()))
|
||||
.header(ContentLength(json_encoded_request.len() as u64))
|
||||
.body(json_encoded_request.as_slice())\
|
||||
% else:
|
||||
|
||||
@@ -649,8 +650,8 @@ else {
|
||||
;
|
||||
% if simple_media_param and not request_value:
|
||||
if let Some(&mut (ref mut reader, size, ref mime)) = ${simple_media_param.type.arg_name}.as_mut() {
|
||||
req = req.header(hyper::header::ContentType(mime.clone()))
|
||||
.header(hyper::header::ContentLength(size))
|
||||
req = req.header(ContentType(mime.clone()))
|
||||
.header(ContentLength(size))
|
||||
.body(reader.into_body());
|
||||
}
|
||||
% endif ## media upload handling
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use std::marker::MarkerTrait;
|
||||
use std::io::{self, Read, Seek, Cursor};
|
||||
|
||||
use mime;
|
||||
use mime::{Mime, TopLevel, SubLevel, Attr, Value};
|
||||
use oauth2;
|
||||
use hyper;
|
||||
use hyper::header::{ContentType, ContentLength, Headers};
|
||||
|
||||
/// Identifies the Hub. There is only one per library, this trait is supposed
|
||||
/// to make intended use more explicit.
|
||||
@@ -117,6 +118,7 @@ pub enum Result<T = ()> {
|
||||
Success(T),
|
||||
}
|
||||
|
||||
const BOUNDARY: &'static str = "MDuXWGyeE33QFXGchb2VFWc4Z7945d";
|
||||
|
||||
/// Provides a `Read` interface that converts multiple parts into the protocol
|
||||
/// identified by [RFC2387](https://tools.ietf.org/html/rfc2387).
|
||||
@@ -124,7 +126,7 @@ pub enum Result<T = ()> {
|
||||
/// to google APIs, and might not be a fully-featured implementation.
|
||||
#[derive(Default)]
|
||||
pub struct MultiPartReader<'a> {
|
||||
raw_parts: Vec<(hyper::header::Headers, &'a mut Read)>,
|
||||
raw_parts: Vec<(Headers, &'a mut Read)>,
|
||||
current_part: Option<(Cursor<Vec<u8>>, &'a mut Read)>,
|
||||
}
|
||||
|
||||
@@ -143,11 +145,23 @@ impl<'a> MultiPartReader<'a> {
|
||||
/// # Panics
|
||||
///
|
||||
/// If this method is called after the first `read` call, it will panic
|
||||
pub fn add_part(mut self, reader: &'a mut Read, size: u64, mime_type: &mime::Mime) -> MultiPartReader<'a> {
|
||||
// let mut headers = hyper::header::Headers::
|
||||
// raw_parts.push((headers, reader));
|
||||
pub fn add_part(mut self, reader: &'a mut Read, size: u64, mime_type: &Mime) -> MultiPartReader<'a> {
|
||||
let mut headers = Headers::new();
|
||||
headers.set(ContentType(mime_type.clone()));
|
||||
headers.set(ContentLength(size));
|
||||
self.raw_parts.push((headers, reader));
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns the mime-type representing our multi-part message.
|
||||
/// Use it with the ContentType header.
|
||||
pub fn mime_type(&self) -> Mime {
|
||||
Mime(
|
||||
TopLevel::Multipart,
|
||||
SubLevel::Ext("Related".to_string()),
|
||||
vec![(Attr::Ext("boundary".to_string()), Value::Ext(BOUNDARY.to_string()))],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Read for MultiPartReader<'a> {
|
||||
|
||||
Reference in New Issue
Block a user