mirror of
https://github.com/OMGeeky/google-apis-rs.git
synced 2026-01-18 17:30:03 +01:00
Merge branch 'syntex' into next
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
language: rust
|
||||
rust:
|
||||
- stable
|
||||
script:
|
||||
- make docs-all gen-all-cli cargo-api ARGS=test
|
||||
env:
|
||||
|
||||
@@ -10,7 +10,6 @@ To find a library of your interest, you might want to proceed looking at the [AP
|
||||
* first-class documentation with cross-links and complete code-examples
|
||||
* support all features, including downloads and resumable uploads
|
||||
* safety and resilience are built-in, allowing you to create highly available tools on top of it. For example, you can trigger retries for all operations that may temporarily fail, e.g. due to network outage.
|
||||
* *(soon)* Feature-complete command line tool to interact with each API.
|
||||
|
||||
# Live-Development
|
||||
|
||||
@@ -46,6 +45,9 @@ To generate the APIs yourself, you will need to meet the following prerequisites
|
||||
* As [*mako*][mako] is a python program, you will need python installed on your system to run it. Some other programs we call depend on python being present as well.
|
||||
* **an internet connection and wget**
|
||||
* Make will download all other prerequisites automatically into hidden directories within this repository, which requires it to make some downloads via wget.
|
||||
* **Rust Stable**
|
||||
* This project compiles on *stable* Rust only. You might consider using [Multirust][multirust] to control
|
||||
the toolchain on a per-project basis
|
||||
|
||||
## Using Make
|
||||
|
||||
@@ -229,4 +231,5 @@ The license of everything not explicitly under a different license are licensed
|
||||
[issues]: https://github.com/Byron/google-apis-rs/issues
|
||||
[playlist]: https://www.youtube.com/playlist?list=PLMHbQxe1e9Mnnqj3Hs1hRDUXFEK-TgCnz
|
||||
[playlist-thumb]: http://img.youtube.com/vi/aGXuGEl90Mo/0.jpg
|
||||
[milestones]: https://github.com/Byron/google-apis-rs/milestones
|
||||
[milestones]: https://github.com/Byron/google-apis-rs/milestones
|
||||
[multirust]: https://github.com/brson/multirust
|
||||
@@ -18,11 +18,15 @@ make:
|
||||
- source: README.md
|
||||
- source: ../LICENSE.md
|
||||
- source: ../Cargo.toml
|
||||
- source: lib.rs.in
|
||||
output_dir: src
|
||||
- source: lib.rs
|
||||
output_dir: src
|
||||
- source: build.rs
|
||||
output_dir: src
|
||||
cargo:
|
||||
build_version: "0.1.7"
|
||||
build_version: "0.1.8"
|
||||
build_script: src/build.rs
|
||||
keywords: [protocol, web, api]
|
||||
dependencies:
|
||||
- url = "*"
|
||||
- json-tools = ">= 0.3.0"
|
||||
|
||||
@@ -22,7 +22,7 @@ make:
|
||||
- source: main.rs
|
||||
output_dir: src
|
||||
cargo:
|
||||
build_version: "0.2.0"
|
||||
build_version: "0.3.0"
|
||||
keywords: [cli]
|
||||
is_executable: YES
|
||||
dependencies:
|
||||
|
||||
@@ -17,14 +17,20 @@ keywords = ["discovery", "google", "cli"]
|
||||
name = "discovery1"
|
||||
|
||||
[dependencies]
|
||||
hyper = ">= 0.4.0"
|
||||
hyper = ">= 0.5.0"
|
||||
mime = "*"
|
||||
yup-oauth2 = "*"
|
||||
strsim = "*"
|
||||
yup-hyper-mock = "*"
|
||||
serde = ">= 0.3.0"
|
||||
serde_macros = "*"
|
||||
clap = "*"
|
||||
strsim = "*"
|
||||
yup-hyper-mock = ">=1.0.0"
|
||||
clap = ">= 0.9.1"
|
||||
|
||||
# to adapt to hyper changes ...
|
||||
[dependencies.yup-oauth2]
|
||||
version = "*"
|
||||
git = "https://github.com/Byron/yup-oauth2"
|
||||
rev = "598f5ed496077e9edca36ff95e2ab352c5b22d0f"
|
||||
|
||||
|
||||
[dependencies.google-discovery1]
|
||||
path = "../discovery1"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// DO NOT EDIT
|
||||
use oauth2::{ApplicationSecret, ConsoleApplicationSecret, TokenStorage, Token};
|
||||
use serde::json;
|
||||
use serde::json::value::Value;
|
||||
use mime::Mime;
|
||||
use clap::{App, SubCommand};
|
||||
use strsim;
|
||||
@@ -19,11 +20,38 @@ use std::default::Default;
|
||||
|
||||
const FIELD_SEP: char = '.';
|
||||
|
||||
|
||||
pub enum ComplexType {
|
||||
Pod,
|
||||
Vec,
|
||||
Map,
|
||||
}
|
||||
|
||||
// Null,
|
||||
// Bool(bool),
|
||||
// I64(i64),
|
||||
// U64(u64),
|
||||
// F64(f64),
|
||||
// String(String),
|
||||
|
||||
pub enum JsonType {
|
||||
Boolean,
|
||||
Int,
|
||||
Uint,
|
||||
Float,
|
||||
String,
|
||||
}
|
||||
|
||||
pub struct JsonTypeInfo {
|
||||
pub jtype: JsonType,
|
||||
pub ctype: ComplexType,
|
||||
}
|
||||
|
||||
// Based on @erickt user comment. Thanks for the idea !
|
||||
// Remove all keys whose values are null from given value (changed in place)
|
||||
pub fn remove_json_null_values(value: &mut json::value::Value) {
|
||||
pub fn remove_json_null_values(value: &mut Value) {
|
||||
match *value {
|
||||
json::value::Value::Object(ref mut map) => {
|
||||
Value::Object(ref mut map) => {
|
||||
let mut for_removal = Vec::new();
|
||||
|
||||
for (key, mut value) in map.iter_mut() {
|
||||
@@ -96,6 +124,14 @@ impl ToString for FieldCursor {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for FieldCursor {
|
||||
fn from(value: &'static str) -> FieldCursor {
|
||||
let mut res = FieldCursor::default();
|
||||
res.set(value).unwrap();
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl FieldCursor {
|
||||
pub fn set(&mut self, value: &str) -> Result<(), CLIError> {
|
||||
if value.len() == 0 {
|
||||
@@ -201,6 +237,78 @@ impl FieldCursor {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_json_value(&self, mut object: &mut Value,
|
||||
value: &str, type_info: JsonTypeInfo,
|
||||
err: &mut InvalidOptionsError,
|
||||
orig_cursor: &FieldCursor) {
|
||||
assert!(self.0.len() > 0);
|
||||
|
||||
for field in &self.0[..self.0.len()-1] {
|
||||
let tmp = object;
|
||||
object =
|
||||
match *tmp {
|
||||
Value::Object(ref mut mapping) => {
|
||||
mapping.entry(field.to_owned()).or_insert(
|
||||
Value::Object(Default::default())
|
||||
)
|
||||
},
|
||||
_ => panic!("We don't expect non-object Values here ...")
|
||||
};
|
||||
}
|
||||
|
||||
match *object {
|
||||
Value::Object(ref mut mapping) => {
|
||||
let field = &self.0[self.0.len()-1];
|
||||
let to_jval =
|
||||
|value: &str, jtype: JsonType, err: &mut InvalidOptionsError|
|
||||
-> Value {
|
||||
match jtype {
|
||||
JsonType::Boolean =>
|
||||
Value::Bool(arg_from_str(value, err, &field, "boolean")),
|
||||
JsonType::Int =>
|
||||
Value::I64(arg_from_str(value, err, &field, "int")),
|
||||
JsonType::Uint =>
|
||||
Value::U64(arg_from_str(value, err, &field, "uint")),
|
||||
JsonType::Float =>
|
||||
Value::F64(arg_from_str(value, err, &field, "float")),
|
||||
JsonType::String =>
|
||||
Value::String(value.to_owned()),
|
||||
}
|
||||
};
|
||||
|
||||
match type_info.ctype {
|
||||
ComplexType::Pod => {
|
||||
if mapping.insert(field.to_owned(), to_jval(value, type_info.jtype, err)).is_some() {
|
||||
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
|
||||
}
|
||||
},
|
||||
ComplexType::Vec => {
|
||||
match *mapping.entry(field.to_owned())
|
||||
.or_insert(Value::Array(Default::default())) {
|
||||
Value::Array(ref mut values) => values.push(to_jval(value, type_info.jtype, err)),
|
||||
_ => unreachable!()
|
||||
}
|
||||
},
|
||||
ComplexType::Map => {
|
||||
let (key, value) = parse_kv_arg(value, err, true);
|
||||
let jval = to_jval(value.unwrap_or(""), type_info.jtype, err);
|
||||
|
||||
match *mapping.entry(field.to_owned())
|
||||
.or_insert(Value::Object(Default::default())) {
|
||||
Value::Object(ref mut value_map) => {
|
||||
if value_map.insert(key.to_owned(), jval).is_some() {
|
||||
err.issues.push(CLIError::Field(FieldError::Duplicate(orig_cursor.to_string())));
|
||||
}
|
||||
}
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn num_fields(&self) -> usize {
|
||||
self.0.len()
|
||||
}
|
||||
@@ -268,15 +376,15 @@ pub fn writer_from_opts(arg: Option<&str>) -> Result<Box<Write>, io::Error> {
|
||||
}
|
||||
|
||||
|
||||
pub fn arg_from_str<T>(arg: &str, err: &mut InvalidOptionsError,
|
||||
arg_name: &'static str,
|
||||
arg_type: &'static str) -> T
|
||||
pub fn arg_from_str<'a, T>(arg: &str, err: &mut InvalidOptionsError,
|
||||
arg_name: &'a str,
|
||||
arg_type: &'a str) -> T
|
||||
where T: FromStr + Default,
|
||||
<T as FromStr>::Err: fmt::Display {
|
||||
match FromStr::from_str(arg) {
|
||||
Err(perr) => {
|
||||
err.issues.push(
|
||||
CLIError::ParseError(arg_name, arg_type, arg.to_string(), format!("{}", perr))
|
||||
CLIError::ParseError(arg_name.to_owned(), arg_type.to_owned(), arg.to_string(), format!("{}", perr))
|
||||
);
|
||||
Default::default()
|
||||
},
|
||||
@@ -411,6 +519,7 @@ pub enum FieldError {
|
||||
PopOnEmpty(String),
|
||||
TrailingFieldSep(String),
|
||||
Unknown(String, Option<String>, Option<String>),
|
||||
Duplicate(String),
|
||||
Empty,
|
||||
}
|
||||
|
||||
@@ -437,6 +546,8 @@ impl fmt::Display for FieldError {
|
||||
};
|
||||
writeln!(f, "Field '{}' does not exist.{}", field, suffix)
|
||||
},
|
||||
FieldError::Duplicate(ref cursor)
|
||||
=> writeln!(f, "Value at '{}' was already set", cursor),
|
||||
FieldError::Empty
|
||||
=> writeln!(f, "Field names must not be empty."),
|
||||
}
|
||||
@@ -447,7 +558,7 @@ impl fmt::Display for FieldError {
|
||||
#[derive(Debug)]
|
||||
pub enum CLIError {
|
||||
Configuration(ConfigurationError),
|
||||
ParseError(&'static str, &'static str, String, String),
|
||||
ParseError(String, String, String, String),
|
||||
UnknownParameter(String, Vec<&'static str>),
|
||||
InvalidUploadProtocol(String, Vec<String>),
|
||||
InvalidKeyValueSyntax(String, bool),
|
||||
@@ -465,7 +576,7 @@ impl fmt::Display for CLIError {
|
||||
CLIError::Field(ref err) => write!(f, "Field -> {}", err),
|
||||
CLIError::InvalidUploadProtocol(ref proto_name, ref valid_names)
|
||||
=> writeln!(f, "'{}' is not a valid upload protocol. Choose from one of {}.", proto_name, valid_names.connect(", ")),
|
||||
CLIError::ParseError(arg_name, type_name, ref value, ref err_desc)
|
||||
CLIError::ParseError(ref arg_name, ref type_name, ref value, ref err_desc)
|
||||
=> writeln!(f, "Failed to parse argument '{}' with value '{}' as {} with error: {}.",
|
||||
arg_name, value, type_name, err_desc),
|
||||
CLIError::UnknownParameter(ref param_name, ref possible_values) => {
|
||||
|
||||
@@ -22,7 +22,7 @@ mod cmn;
|
||||
|
||||
use cmn::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
|
||||
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol,
|
||||
calltype_from_str, remove_json_null_values};
|
||||
calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo};
|
||||
|
||||
use std::default::Default;
|
||||
use std::str::FromStr;
|
||||
@@ -61,9 +61,11 @@ impl<'n, 'a> Engine<'n, 'a> {
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
err.issues.push(CLIError::UnknownParameter(key.to_string(),
|
||||
Vec::new() + &self.gp + &[]
|
||||
));
|
||||
err.issues.push(CLIError::UnknownParameter(key.to_string(),
|
||||
{let mut v = Vec::new();
|
||||
v.extend(self.gp.iter().map(|v|*v));
|
||||
v.extend([].iter().map(|v|*v));
|
||||
v } ));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,9 +116,11 @@ impl<'n, 'a> Engine<'n, 'a> {
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
err.issues.push(CLIError::UnknownParameter(key.to_string(),
|
||||
Vec::new() + &self.gp + &["name", "preferred"]
|
||||
));
|
||||
err.issues.push(CLIError::UnknownParameter(key.to_string(),
|
||||
{let mut v = Vec::new();
|
||||
v.extend(self.gp.iter().map(|v|*v));
|
||||
v.extend(["name", "preferred"].iter().map(|v|*v));
|
||||
v } ));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,7 +336,8 @@ fn main() {
|
||||
(_ , &Some(f)) => f,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let mut arg = Arg::with_name(arg_name_str);
|
||||
let mut arg = Arg::with_name(arg_name_str)
|
||||
.empty_values(false);
|
||||
if let &Some(short_flag) = flag {
|
||||
arg = arg.short(short_flag);
|
||||
}
|
||||
|
||||
@@ -12,13 +12,17 @@ homepage = "https://developers.google.com/discovery/"
|
||||
documentation = "http://byron.github.io/google-apis-rs/google_discovery1"
|
||||
license = "MIT"
|
||||
keywords = ["discovery", "google", "protocol", "web", "api"]
|
||||
build = "src/build.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
hyper = ">= 0.4.0"
|
||||
hyper = ">= 0.5.0"
|
||||
mime = "*"
|
||||
serde = ">= 0.3.0"
|
||||
yup-oauth2 = "*"
|
||||
url = "*"
|
||||
serde = ">= 0.3.0"
|
||||
serde_macros = "*"
|
||||
json-tools = ">= 0.3.0"
|
||||
|
||||
[build-dependencies]
|
||||
syntex = { version = "*" }
|
||||
serde_codegen = { version = "*" }
|
||||
|
||||
|
||||
@@ -290,7 +290,7 @@ impl Display for Error {
|
||||
writeln!(f, "The media size {} exceeds the maximum allowed upload size of {}"
|
||||
, resource_size, max_size),
|
||||
Error::MissingAPIKey => {
|
||||
writeln!(f, "The application's API key was not found in the configuration").ok();
|
||||
(writeln!(f, "The application's API key was not found in the configuration")).ok();
|
||||
writeln!(f, "It is used as there are no Scopes defined for this method.")
|
||||
},
|
||||
Error::BadRequest(ref err) => {
|
||||
@@ -424,8 +424,8 @@ impl<'a> Read for MultiPartReader<'a> {
|
||||
(n, true, _) if n > 0 => {
|
||||
let (headers, reader) = self.raw_parts.remove(0);
|
||||
let mut c = Cursor::new(Vec::<u8>::new());
|
||||
write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
|
||||
headers, LINE_ENDING).unwrap();
|
||||
(write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
|
||||
headers, LINE_ENDING)).unwrap();
|
||||
c.seek(SeekFrom::Start(0)).unwrap();
|
||||
self.current_part = Some((c, reader));
|
||||
}
|
||||
@@ -471,7 +471,7 @@ impl<'a> Read for MultiPartReader<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
header!{
|
||||
header!(
|
||||
#[doc="The `X-Upload-Content-Type` header."]
|
||||
(XUploadContentType, "X-Upload-Content-Type") => [Mime]
|
||||
|
||||
@@ -484,7 +484,7 @@ header!{
|
||||
)));
|
||||
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Chunk {
|
||||
@@ -494,7 +494,7 @@ pub struct Chunk {
|
||||
|
||||
impl fmt::Display for Chunk {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(fmt, "{}-{}", self.first, self.last).ok();
|
||||
(write!(fmt, "{}-{}", self.first, self.last)).ok();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -549,7 +549,7 @@ impl HeaderFormat for ContentRange {
|
||||
Some(ref c) => try!(c.fmt(fmt)),
|
||||
None => try!(fmt.write_str("*"))
|
||||
}
|
||||
write!(fmt, "/{}", self.total_length).ok();
|
||||
(write!(fmt, "/{}", self.total_length)).ok();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -668,12 +668,14 @@ impl<'a, A> ResumableUploadHelper<'a, A>
|
||||
if self.delegate.cancel_chunk_upload(&range_header) {
|
||||
return None
|
||||
}
|
||||
match self.client.post(self.url)
|
||||
.header(range_header)
|
||||
.header(ContentType(self.media_type.clone()))
|
||||
.header(UserAgent(self.user_agent.to_string()))
|
||||
.body(&mut section_reader)
|
||||
.send() {
|
||||
// workaround https://github.com/rust-lang/rust/issues/22252
|
||||
let res = self.client.post(self.url)
|
||||
.header(range_header)
|
||||
.header(ContentType(self.media_type.clone()))
|
||||
.header(UserAgent(self.user_agent.to_string()))
|
||||
.body(&mut section_reader)
|
||||
.send();
|
||||
match res {
|
||||
Ok(mut res) => {
|
||||
if res.status == StatusCode::PermanentRedirect {
|
||||
continue
|
||||
@@ -700,4 +702,28 @@ impl<'a, A> ResumableUploadHelper<'a, A>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use serde::json::value::Value;
|
||||
// Copy of src/rust/cli/cmn.rs
|
||||
// TODO(ST): Allow sharing common code between program types
|
||||
pub fn remove_json_null_values(value: &mut Value) {
|
||||
match *value {
|
||||
Value::Object(ref mut map) => {
|
||||
let mut for_removal = Vec::new();
|
||||
|
||||
for (key, mut value) in map.iter_mut() {
|
||||
if value.is_null() {
|
||||
for_removal.push(key.clone());
|
||||
} else {
|
||||
remove_json_null_values(&mut value);
|
||||
}
|
||||
}
|
||||
|
||||
for key in &for_removal {
|
||||
map.remove(key);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,9 @@ homepage = "${documentationLink}"
|
||||
documentation = "${cargo.doc_base_url}/${to_extern_crate_name(util.crate_name())}"
|
||||
license = "${copyright.license_abbrev}"
|
||||
keywords = ["${name[:20]}", ${", ".join(estr(cargo.keywords))}]
|
||||
% if cargo.get('build_script'):
|
||||
build = "${cargo.build_script}"
|
||||
% endif
|
||||
|
||||
% if cargo.get('is_executable', False):
|
||||
[[bin]]
|
||||
@@ -23,19 +26,18 @@ name = "${util.program_name()}"
|
||||
% endif
|
||||
|
||||
[dependencies]
|
||||
hyper = ">= 0.5.0"
|
||||
mime = "*"
|
||||
serde = ">= 0.3.0"
|
||||
serde_macros = "*"
|
||||
hyper = ">= 0.5.2"
|
||||
## Must match the one hyper uses, otherwise there are duplicate similarly named `Mime` structs
|
||||
mime = "0.0.11"
|
||||
serde = ">= 0.4.1"
|
||||
yup-oauth2 = "*"
|
||||
% for dep in cargo.get('dependencies', list()):
|
||||
${dep}
|
||||
% endfor
|
||||
|
||||
# to adapt to hyper changes ...
|
||||
[dependencies.yup-oauth2]
|
||||
version = "*"
|
||||
git = "https://github.com/Byron/yup-oauth2"
|
||||
rev = "598f5ed496077e9edca36ff95e2ab352c5b22d0f"
|
||||
[build-dependencies]
|
||||
syntex = { version = "*" }
|
||||
serde_codegen = { version = "*" }
|
||||
|
||||
% if make.depends_on_suffix is not None:
|
||||
|
||||
|
||||
17
src/mako/api/build.rs.mako
Normal file
17
src/mako/api/build.rs.mako
Normal file
@@ -0,0 +1,17 @@
|
||||
<%namespace name="util" file="../lib/util.mako"/>\
|
||||
extern crate syntex;
|
||||
extern crate serde_codegen;
|
||||
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
||||
let src = Path::new("src/lib.rs.in");
|
||||
let dst = Path::new(&out_dir).join("lib.rs");
|
||||
|
||||
let mut registry = syntex::Registry::new();
|
||||
serde_codegen::register(&mut registry);
|
||||
registry.expand("${util.crate_name()}", &src, &dst).unwrap();
|
||||
}
|
||||
133
src/mako/api/lib.rs.in.mako
Normal file
133
src/mako/api/lib.rs.in.mako
Normal file
@@ -0,0 +1,133 @@
|
||||
<%namespace name="lib" file="lib/lib.mako"/>\
|
||||
<%namespace name="util" file="../lib/util.mako"/>\
|
||||
<%namespace name="rbuild" file="lib/rbuild.mako"/>\
|
||||
<%namespace name="mbuild" file="lib/mbuild.mako"/>\
|
||||
<%namespace name="schema" file="lib/schema.mako"/>\
|
||||
<%
|
||||
from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment,
|
||||
rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds,
|
||||
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE,
|
||||
UNUSED_TYPE_MARKER, schema_markers)
|
||||
|
||||
c = new_context(schemas, resources, context.get('methods'))
|
||||
hub_type = hub_type(c.schemas, util.canonical_name())
|
||||
ht_params = hub_type_params_s()
|
||||
|
||||
default_user_agent = "google-api-rust-client/" + cargo.build_version
|
||||
%>\
|
||||
<%block filter="rust_comment">\
|
||||
<%util:gen_info source="${self.uri}" />\
|
||||
</%block>
|
||||
|
||||
extern crate hyper;
|
||||
extern crate serde;
|
||||
extern crate yup_oauth2 as oauth2;
|
||||
extern crate mime;
|
||||
extern crate url;
|
||||
|
||||
mod cmn;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::cell::RefCell;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::default::Default;
|
||||
use std::collections::BTreeMap;
|
||||
use serde::json;
|
||||
use std::io;
|
||||
use std::fs;
|
||||
use std::thread::sleep_ms;
|
||||
|
||||
pub use cmn::{MultiPartReader, ToParts, MethodInfo, Result, Error, CallBuilder, Hub, ReadSeek, Part,
|
||||
ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate, MethodsBuilder,
|
||||
Resource, ErrorResponse, remove_json_null_values};
|
||||
|
||||
|
||||
// ##############
|
||||
// UTILITIES ###
|
||||
// ############
|
||||
|
||||
${lib.scope_enum()}
|
||||
|
||||
|
||||
// ########
|
||||
// HUB ###
|
||||
// ######
|
||||
|
||||
/// Central instance to access all ${hub_type} related resource activities
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Instantiate a new hub
|
||||
///
|
||||
<%block filter="rust_doc_comment">\
|
||||
${lib.hub_usage_example(c)}\
|
||||
</%block>
|
||||
pub struct ${hub_type}${ht_params} {
|
||||
client: RefCell<C>,
|
||||
auth: RefCell<A>,
|
||||
_user_agent: String,
|
||||
}
|
||||
|
||||
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> Hub for ${hub_type}${ht_params} {}
|
||||
|
||||
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
|
||||
where ${', '.join(hub_type_bounds())} {
|
||||
|
||||
pub fn new(client: C, authenticator: A) -> ${hub_type}${ht_params} {
|
||||
${hub_type} {
|
||||
client: RefCell::new(client),
|
||||
auth: RefCell::new(authenticator),
|
||||
_user_agent: "${default_user_agent}".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
% for resource in sorted(c.rta_map.keys()):
|
||||
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}${rb_type_params_s(resource, c)} {
|
||||
${rb_type(resource)} { hub: &self }
|
||||
}
|
||||
% endfor
|
||||
|
||||
/// Set the user-agent header field to use in all requests to the server.
|
||||
/// It defaults to `${default_user_agent}`.
|
||||
///
|
||||
/// Returns the previously set user-agent.
|
||||
pub fn user_agent(&mut self, agent_name: String) -> String {
|
||||
let prev = self._user_agent.clone();
|
||||
self._user_agent = agent_name;
|
||||
prev
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
% if c.schemas:
|
||||
// ############
|
||||
// SCHEMAS ###
|
||||
// ##########
|
||||
% for s in c.schemas.values():
|
||||
% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
|
||||
${schema.new(s, c)}
|
||||
% endif
|
||||
% endfor
|
||||
% endif
|
||||
|
||||
// ###################
|
||||
// MethodBuilders ###
|
||||
// #################
|
||||
|
||||
% for resource in c.rta_map:
|
||||
${rbuild.new(resource, c)}
|
||||
|
||||
|
||||
% endfor
|
||||
|
||||
|
||||
// ###################
|
||||
// CallBuilders ###
|
||||
// #################
|
||||
|
||||
% for resource, methods in c.rta_map.iteritems():
|
||||
% for method in methods:
|
||||
${mbuild.new(resource, method, c)}
|
||||
|
||||
% endfor ## method in methods
|
||||
% endfor ## resource, methods
|
||||
@@ -1,19 +1,9 @@
|
||||
<%namespace name="lib" file="lib/lib.mako"/>\
|
||||
<%namespace name="util" file="../lib/util.mako"/>\
|
||||
<%namespace name="rbuild" file="lib/rbuild.mako"/>\
|
||||
<%namespace name="mbuild" file="lib/mbuild.mako"/>\
|
||||
<%namespace name="schema" file="lib/schema.mako"/>\
|
||||
<%
|
||||
from util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment,
|
||||
rb_type, hub_type, mangle_ident, hub_type_params_s, hub_type_bounds,
|
||||
rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE,
|
||||
UNUSED_TYPE_MARKER, schema_markers)
|
||||
from util import (new_context, rust_comment, rust_module_doc_comment)
|
||||
|
||||
c = new_context(schemas, resources, context.get('methods'))
|
||||
hub_type = hub_type(c.schemas, util.canonical_name())
|
||||
ht_params = hub_type_params_s()
|
||||
|
||||
default_user_agent = "google-api-rust-client/" + cargo.build_version
|
||||
%>\
|
||||
<%block filter="rust_comment">\
|
||||
<%util:gen_info source="${self.uri}" />\
|
||||
@@ -22,124 +12,11 @@
|
||||
<%block filter="rust_module_doc_comment">\
|
||||
${lib.docs(c)}
|
||||
</%block>
|
||||
|
||||
// Unused attributes happen thanks to defined, but unused structures
|
||||
// We don't warn about this, as depending on the API, some data structures or facilities are never used.
|
||||
// Instead of pre-determining this, we just disable the lint. It's manually tuned to not have any
|
||||
// unused imports in fully featured APIs. Same with unused_mut ... .
|
||||
#![allow(unused_imports, unused_mut, dead_code)]
|
||||
// Required for serde annotations
|
||||
#![feature(custom_derive, custom_attribute, plugin, slice_patterns)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate hyper;
|
||||
extern crate serde;
|
||||
extern crate yup_oauth2 as oauth2;
|
||||
extern crate mime;
|
||||
extern crate url;
|
||||
extern crate json_tools;
|
||||
|
||||
mod cmn;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::cell::RefCell;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::default::Default;
|
||||
use std::collections::BTreeMap;
|
||||
use serde::json;
|
||||
use std::io;
|
||||
use std::fs;
|
||||
use std::thread::sleep_ms;
|
||||
|
||||
pub use cmn::{MultiPartReader, ToParts, MethodInfo, Result, Error, CallBuilder, Hub, ReadSeek, Part, ResponseResult, RequestValue, NestedType, Delegate, DefaultDelegate, MethodsBuilder, Resource, ErrorResponse};
|
||||
|
||||
|
||||
// ##############
|
||||
// UTILITIES ###
|
||||
// ############
|
||||
|
||||
${lib.scope_enum()}
|
||||
|
||||
|
||||
// ########
|
||||
// HUB ###
|
||||
// ######
|
||||
|
||||
/// Central instance to access all ${hub_type} related resource activities
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Instantiate a new hub
|
||||
///
|
||||
<%block filter="rust_doc_comment">\
|
||||
${lib.hub_usage_example(c)}\
|
||||
</%block>
|
||||
pub struct ${hub_type}${ht_params} {
|
||||
client: RefCell<C>,
|
||||
auth: RefCell<A>,
|
||||
_user_agent: String,
|
||||
}
|
||||
|
||||
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> Hub for ${hub_type}${ht_params} {}
|
||||
|
||||
impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params}
|
||||
where ${', '.join(hub_type_bounds())} {
|
||||
|
||||
pub fn new(client: C, authenticator: A) -> ${hub_type}${ht_params} {
|
||||
${hub_type} {
|
||||
client: RefCell::new(client),
|
||||
auth: RefCell::new(authenticator),
|
||||
_user_agent: "${default_user_agent}".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
% for resource in sorted(c.rta_map.keys()):
|
||||
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}${rb_type_params_s(resource, c)} {
|
||||
${rb_type(resource)} { hub: &self }
|
||||
}
|
||||
% endfor
|
||||
|
||||
/// Set the user-agent header field to use in all requests to the server.
|
||||
/// It defaults to `${default_user_agent}`.
|
||||
///
|
||||
/// Returns the previously set user-agent.
|
||||
pub fn user_agent(&mut self, agent_name: String) -> String {
|
||||
let prev = self._user_agent.clone();
|
||||
self._user_agent = agent_name;
|
||||
prev
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
% if c.schemas:
|
||||
// ############
|
||||
// SCHEMAS ###
|
||||
// ##########
|
||||
% for s in c.schemas.values():
|
||||
% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True):
|
||||
${schema.new(s, c)}
|
||||
% endif
|
||||
% endfor
|
||||
% endif
|
||||
|
||||
// ###################
|
||||
// MethodBuilders ###
|
||||
// #################
|
||||
|
||||
% for resource in c.rta_map:
|
||||
${rbuild.new(resource, c)}
|
||||
|
||||
|
||||
% endfor
|
||||
|
||||
|
||||
// ###################
|
||||
// CallBuilders ###
|
||||
// #################
|
||||
|
||||
% for resource, methods in c.rta_map.iteritems():
|
||||
% for method in methods:
|
||||
${mbuild.new(resource, method, c)}
|
||||
|
||||
% endfor ## method in methods
|
||||
% endfor ## resource, methods
|
||||
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
|
||||
@@ -483,9 +483,6 @@ match result {
|
||||
% if URL_ENCODE in special_cases:
|
||||
use url::percent_encoding::{percent_encode, FORM_URLENCODED_ENCODE_SET};
|
||||
% endif
|
||||
% if request_value:
|
||||
use json_tools::{TokenReader, Lexer, BufferType, TokenType, FilterTypedKeyValuePairs, IteratorExt};
|
||||
% endif
|
||||
use std::io::{Read, Seek};
|
||||
use hyper::header::{ContentType, ContentLength, Authorization, UserAgent, Location};
|
||||
let mut dd = DefaultDelegate;
|
||||
@@ -665,13 +662,11 @@ else {
|
||||
let mut json_mime_type = mime::Mime(mime::TopLevel::Application, mime::SubLevel::Json, Default::default());
|
||||
let mut request_value_reader =
|
||||
{
|
||||
let json_cache = json::to_string(&self.${property(REQUEST_VALUE_PROPERTY_NAME)}).unwrap();
|
||||
let mut mem_dst = io::Cursor::new(Vec::with_capacity(json_cache.len()));
|
||||
io::copy(&mut Lexer::new(json_cache.bytes(), BufferType::Span)
|
||||
.filter_key_value_by_type(TokenType::Null)
|
||||
.reader(Some(&json_cache)), &mut mem_dst).unwrap();
|
||||
mem_dst.seek(io::SeekFrom::Start(0)).unwrap();
|
||||
mem_dst
|
||||
let mut value = json::value::to_value(&self.${property(REQUEST_VALUE_PROPERTY_NAME)});
|
||||
remove_json_null_values(&mut value);
|
||||
let mut dst = io::Cursor::new(Vec::with_capacity(128));
|
||||
json::to_writer(&mut dst, &value).unwrap();
|
||||
dst
|
||||
};
|
||||
let request_size = request_value_reader.seek(io::SeekFrom::End(0)).unwrap();
|
||||
request_value_reader.seek(io::SeekFrom::Start(0)).unwrap();
|
||||
|
||||
@@ -323,7 +323,7 @@ if dry_run {
|
||||
% if mc.response_schema:
|
||||
let mut value = json::value::to_value(&output_schema);
|
||||
remove_json_null_values(&mut value);
|
||||
serde::json::to_writer_pretty(&mut ostream, &value).unwrap();
|
||||
json::to_writer_pretty(&mut ostream, &value).unwrap();
|
||||
% endif
|
||||
% if track_download_flag:
|
||||
} else {
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
<%block filter="rust_comment">\
|
||||
<%util:gen_info source="${self.uri}" />\
|
||||
</%block>
|
||||
#![feature(plugin, exit_status)]
|
||||
#![allow(unused_variables, unused_imports, dead_code, unused_mut)]
|
||||
|
||||
#[macro_use]
|
||||
@@ -34,18 +33,19 @@ mod cmn;
|
||||
${engine.new(c)}\
|
||||
|
||||
fn main() {
|
||||
let mut exit_status = 0i32;
|
||||
${argparse.new(c) | indent_all_but_first_by(1)}\
|
||||
let matches = app.get_matches();
|
||||
|
||||
let debug = matches.is_present("${DEBUG_FLAG}");
|
||||
match Engine::new(matches) {
|
||||
Err(err) => {
|
||||
env::set_exit_status(err.exit_code);
|
||||
exit_status = err.exit_code;
|
||||
writeln!(io::stderr(), "{}", err).ok();
|
||||
},
|
||||
Ok(engine) => {
|
||||
if let Err(doit_err) = engine.doit() {
|
||||
env::set_exit_status(1);
|
||||
exit_status = 1;
|
||||
match doit_err {
|
||||
DoitError::IoError(path, err) => {
|
||||
writeln!(io::stderr(), "Failed to open output file '{}': {}", path, err).ok();
|
||||
@@ -61,4 +61,6 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::process::exit(exit_status);
|
||||
}
|
||||
@@ -288,7 +288,7 @@ impl Display for Error {
|
||||
writeln!(f, "The media size {} exceeds the maximum allowed upload size of {}"
|
||||
, resource_size, max_size),
|
||||
Error::MissingAPIKey => {
|
||||
writeln!(f, "The application's API key was not found in the configuration").ok();
|
||||
(writeln!(f, "The application's API key was not found in the configuration")).ok();
|
||||
writeln!(f, "It is used as there are no Scopes defined for this method.")
|
||||
},
|
||||
Error::BadRequest(ref err) => {
|
||||
@@ -422,8 +422,8 @@ impl<'a> Read for MultiPartReader<'a> {
|
||||
(n, true, _) if n > 0 => {
|
||||
let (headers, reader) = self.raw_parts.remove(0);
|
||||
let mut c = Cursor::new(Vec::<u8>::new());
|
||||
write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
|
||||
headers, LINE_ENDING).unwrap();
|
||||
(write!(&mut c, "{}--{}{}{}{}", LINE_ENDING, BOUNDARY, LINE_ENDING,
|
||||
headers, LINE_ENDING)).unwrap();
|
||||
c.seek(SeekFrom::Start(0)).unwrap();
|
||||
self.current_part = Some((c, reader));
|
||||
}
|
||||
@@ -469,18 +469,51 @@ impl<'a> Read for MultiPartReader<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
header!{
|
||||
#[doc="The `X-Upload-Content-Type` header."]
|
||||
(XUploadContentType, "X-Upload-Content-Type") => [Mime]
|
||||
// The following macro invocation needs to be expanded, as `include!`
|
||||
// doens't support external macros
|
||||
// header!{
|
||||
// #[doc="The `X-Upload-Content-Type` header."]
|
||||
// (XUploadContentType, "X-Upload-Content-Type") => [Mime]
|
||||
|
||||
xupload_content_type {
|
||||
test_header!(
|
||||
test1,
|
||||
vec![b"text/plain"],
|
||||
Some(HeaderField(
|
||||
vec![Mime(TopLevel::Text, SubLevel::Plain, Vec::new())]
|
||||
)));
|
||||
// xupload_content_type {
|
||||
// test_header!(
|
||||
// test1,
|
||||
// vec![b"text/plain"],
|
||||
// Some(HeaderField(
|
||||
// vec![Mime(TopLevel::Text, SubLevel::Plain, Vec::new())]
|
||||
// )));
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
/// The `X-Upload-Content-Type` header.
|
||||
///
|
||||
/// Generated via rustc --pretty expanded -Z unstable-options, and manually
|
||||
/// processed to be more readable.
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub struct XUploadContentType(pub Mime);
|
||||
|
||||
impl ::std::ops::Deref for XUploadContentType {
|
||||
type Target = Mime;
|
||||
fn deref<'a>(&'a self) -> &'a Mime { &self.0 }
|
||||
}
|
||||
impl ::std::ops::DerefMut for XUploadContentType {
|
||||
fn deref_mut<'a>(&'a mut self) -> &'a mut Mime { &mut self.0 }
|
||||
}
|
||||
impl Header for XUploadContentType {
|
||||
fn header_name() -> &'static str { "X-Upload-Content-Type" }
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
|
||||
hyper::header::parsing::from_one_raw_str(raw).map(XUploadContentType)
|
||||
}
|
||||
}
|
||||
impl HeaderFormat for XUploadContentType {
|
||||
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
Display::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
impl Display for XUploadContentType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,7 +525,7 @@ pub struct Chunk {
|
||||
|
||||
impl fmt::Display for Chunk {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(fmt, "{}-{}", self.first, self.last).ok();
|
||||
(write!(fmt, "{}-{}", self.first, self.last)).ok();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -534,7 +567,7 @@ impl Header for ContentRange {
|
||||
}
|
||||
|
||||
/// We are not parsable, as parsing is done by the `Range` header
|
||||
fn parse_header(_: &[Vec<u8>]) -> Option<ContentRange> {
|
||||
fn parse_header(_: &[Vec<u8>]) -> Option<Self> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -547,7 +580,7 @@ impl HeaderFormat for ContentRange {
|
||||
Some(ref c) => try!(c.fmt(fmt)),
|
||||
None => try!(fmt.write_str("*"))
|
||||
}
|
||||
write!(fmt, "/{}", self.total_length).ok();
|
||||
(write!(fmt, "/{}", self.total_length)).ok();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -560,8 +593,9 @@ impl Header for RangeResponseHeader {
|
||||
"Range"
|
||||
}
|
||||
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<RangeResponseHeader> {
|
||||
if let [ref v] = raw {
|
||||
fn parse_header(raw: &[Vec<u8>]) -> Option<Self> {
|
||||
if raw.len() > 0 {
|
||||
let v = &raw[0];
|
||||
if let Ok(s) = std::str::from_utf8(v) {
|
||||
const PREFIX: &'static str = "bytes ";
|
||||
if s.starts_with(PREFIX) {
|
||||
@@ -700,4 +734,27 @@ impl<'a, A> ResumableUploadHelper<'a, A>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy of src/rust/cli/cmn.rs
|
||||
// TODO(ST): Allow sharing common code between program types
|
||||
pub fn remove_json_null_values(value: &mut serde::json::value::Value) {
|
||||
match *value {
|
||||
serde::json::value::Value::Object(ref mut map) => {
|
||||
let mut for_removal = Vec::new();
|
||||
|
||||
for (key, mut value) in map.iter_mut() {
|
||||
if value.is_null() {
|
||||
for_removal.push(key.clone());
|
||||
} else {
|
||||
remove_json_null_values(&mut value);
|
||||
}
|
||||
}
|
||||
|
||||
for key in &for_removal {
|
||||
map.remove(key);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user