Skip to content

Commit

Permalink
Merge pull request #384 from philippeitis/update-mime
Browse files Browse the repository at this point in the history
Update mime dependency
  • Loading branch information
philippeitis authored Oct 19, 2022
2 parents fb884e1 + f6b195d commit b20e630
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion google-apis-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ doctest = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mime = "^ 0.2.0"
mime = "^ 0.3"
serde = { version = "^ 1.0", features = ["derive"] }
serde_with = "2.0.1"
serde_json = "^ 1.0"
Expand Down
37 changes: 22 additions & 15 deletions google-apis-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use hyper::header::{HeaderMap, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, USER
use hyper::Method;
use hyper::StatusCode;

use mime::{Attr, Mime, SubLevel, TopLevel, Value};
use mime::Mime;

use serde_json as json;

Expand Down Expand Up @@ -333,6 +333,13 @@ pub struct MultiPartReader<'a> {
}

impl<'a> MultiPartReader<'a> {
// TODO: This should be an associated constant
/// Returns the mime-type representing our multi-part message.
/// Use it with the ContentType header.
pub fn mime_type() -> Mime {
Mime::from_str(&format!("multipart/related;boundary={}", BOUNDARY)).expect("valid mimetype")
}

/// Reserve memory for exactly the given amount of parts
pub fn reserve_exact(&mut self, cap: usize) {
self.raw_parts.reserve_exact(cap);
Expand All @@ -358,26 +365,13 @@ impl<'a> MultiPartReader<'a> {
let mut headers = HeaderMap::new();
headers.insert(
CONTENT_TYPE,
hyper::header::HeaderValue::from_str(&format!("{}", mime_type)).unwrap(),
hyper::header::HeaderValue::from_str(&mime_type.to_string()).unwrap(),
);
headers.insert(CONTENT_LENGTH, size.into());
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()),
)],
)
}

/// Returns true if we are totally used
fn is_depleted(&self) -> bool {
self.raw_parts.is_empty()
Expand Down Expand Up @@ -777,6 +771,7 @@ mod test_api {
use std::str::FromStr;

use ::serde::{Deserialize, Serialize};
use mime;
use serde_json as json;

#[test]
Expand Down Expand Up @@ -836,4 +831,16 @@ mod test_api {
let dlg: &mut dyn Delegate = &mut dd;
with_send(dlg);
}

#[test]
fn test_mime() {
let mime = MultiPartReader::mime_type();

assert_eq!(mime::MULTIPART, mime.type_());
assert_eq!("related", mime.subtype());
assert_eq!(
Some(BOUNDARY),
mime.get_param("boundary").map(|x| x.as_str())
);
}
}
3 changes: 1 addition & 2 deletions src/generator/templates/Cargo.toml.mako
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ path = "src/main.rs"
% endif
[dependencies]
## TODO: temporary, remove when yup-oauth2 is optional
anyhow = "^ 1.0"
hyper-rustls = "0.23.0"
## Must match the one hyper uses, otherwise there are duplicate similarly named `Mime` structs
mime = "^ 0.2.0"
mime = "^ 0.3.0"
serde = { version = "^ 1.0", features = ["derive"] }
serde_json = "^ 1.0"
itertools = "^ 0.10"
Expand Down
13 changes: 6 additions & 7 deletions src/generator/templates/api/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ else {
let url = url::Url::parse_with_params(&url, params).unwrap();
% if request_value:
let mut json_mime_type: mime::Mime = "application/json".parse().unwrap();
let mut json_mime_type = mime::APPLICATION_JSON;
let mut request_value_reader =
{
let mut value = json::value::to_value(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).expect("serde to work");
Expand Down Expand Up @@ -747,10 +747,9 @@ else {
${READER_SEEK | indent_all_but_first_by(5)}
mp_reader.add_part(&mut request_value_reader, request_size, json_mime_type.clone())
.add_part(&mut reader, size, reader_mime_type.clone());
let mime_type = mp_reader.mime_type();
(&mut mp_reader as &mut (dyn io::Read + Send), (CONTENT_TYPE, mime_type.to_string()))
(&mut mp_reader as &mut (dyn io::Read + Send), client::MultiPartReader::mime_type())
},
_ => (&mut request_value_reader as &mut (dyn io::Read + Send), (CONTENT_TYPE, json_mime_type.to_string())),
_ => (&mut request_value_reader as &mut (dyn io::Read + Send), json_mime_type.clone()),
};
% endif
let client = &self.hub.client;
Expand All @@ -774,15 +773,15 @@ else {
% if request_value:
% if not simple_media_param:
let request = req_builder
.header(CONTENT_TYPE, format!("{}", json_mime_type.to_string()))
.header(CONTENT_TYPE, json_mime_type.to_string())
.header(CONTENT_LENGTH, request_size as u64)
.body(hyper::body::Body::from(request_value_reader.get_ref().clone()))\
% else:
let mut body_reader_bytes = vec![];
body_reader.read_to_end(&mut body_reader_bytes).unwrap();
let request = req_builder
.header(content_type.0, content_type.1.to_string())
.body(hyper::body::Body::from(body_reader_bytes))\
.header(CONTENT_TYPE, content_type.to_string())
.body(hyper::body::Body::from(body_reader_bytes))\
% endif ## not simple_media_param
% else:
% if simple_media_param:
Expand Down

0 comments on commit b20e630

Please sign in to comment.