Publish latest versions of all APIs

This commit is contained in:
Sebastian Thiel
2017-09-27 14:54:55 +02:00
parent 3a6ef3db41
commit 059d6700dd
1024 changed files with 515705 additions and 77998 deletions

View File

@@ -46,6 +46,91 @@ struct Engine<'n> {
impl<'n> Engine<'n> {
fn _detections_detect(&self, opt: &ArgMatches<'n>, dry_run: bool, err: &mut InvalidOptionsError)
-> Result<(), DoitError> {
let mut field_cursor = FieldCursor::default();
let mut object = json::value::Value::Object(Default::default());
for kvarg in opt.values_of("kv").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
let last_errc = err.issues.len();
let (key, value) = parse_kv_arg(&*kvarg, err, false);
let mut temp_cursor = field_cursor.clone();
if let Err(field_err) = temp_cursor.set(&*key) {
err.issues.push(field_err);
}
if value.is_none() {
field_cursor = temp_cursor.clone();
if err.issues.len() > last_errc {
err.issues.remove(last_errc);
}
continue;
}
let type_info: Option<(&'static str, JsonTypeInfo)> =
match &temp_cursor.to_string()[..] {
"q" => Some(("q", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Vec })),
_ => {
let suggestion = FieldCursor::did_you_mean(key, &vec!["q"]);
err.issues.push(CLIError::Field(FieldError::Unknown(temp_cursor.to_string(), suggestion, value.map(|v| v.to_string()))));
None
}
};
if let Some((field_cursor_str, type_info)) = type_info {
FieldCursor::from(field_cursor_str).set_json_value(&mut object, value.unwrap(), type_info, err, &temp_cursor);
}
}
let mut request: api::DetectLanguageRequest = json::value::from_value(object).unwrap();
let mut call = self.hub.detections().detect(request);
for parg in opt.values_of("v").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
let (key, value) = parse_kv_arg(&*parg, err, false);
match key {
_ => {
let mut found = false;
for param in &self.gp {
if key == *param {
found = true;
call = call.param(self.gpm.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, value.unwrap_or("unset"));
break;
}
}
if !found {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v } ));
}
}
}
}
let protocol = CallType::Standard;
if dry_run {
Ok(())
} else {
assert!(err.issues.len() == 0);
for scope in self.opt.values_of("url").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
call = call.add_scope(scope);
}
let mut ostream = match writer_from_opts(opt.value_of("out")) {
Ok(mut f) => f,
Err(io_err) => return Err(DoitError::IoError(opt.value_of("out").unwrap_or("-").to_string(), io_err)),
};
match match protocol {
CallType::Standard => call.doit(),
_ => unreachable!()
} {
Err(api_err) => Err(DoitError::ApiError(api_err)),
Ok((mut response, output_schema)) => {
let mut value = json::value::to_value(&output_schema).expect("serde to work");
remove_json_null_values(&mut value);
json::to_writer_pretty(&mut ostream, &value).unwrap();
ostream.flush().unwrap();
Ok(())
}
}
}
}
fn _detections_list(&self, opt: &ArgMatches<'n>, dry_run: bool, err: &mut InvalidOptionsError)
-> Result<(), DoitError> {
let mut call = self.hub.detections().list(&opt.values_of("q").map(|i|i.collect()).unwrap_or(Vec::new()).iter().map(|&v| v.to_string()).collect::<Vec<String>>());
@@ -75,6 +160,9 @@ impl<'n> Engine<'n> {
Ok(())
} else {
assert!(err.issues.len() == 0);
for scope in self.opt.values_of("url").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
call = call.add_scope(scope);
}
let mut ostream = match writer_from_opts(opt.value_of("out")) {
Ok(mut f) => f,
Err(io_err) => return Err(DoitError::IoError(opt.value_of("out").unwrap_or("-").to_string(), io_err)),
@@ -104,6 +192,9 @@ impl<'n> Engine<'n> {
"target" => {
call = call.target(value.unwrap_or(""));
},
"model" => {
call = call.model(value.unwrap_or(""));
},
_ => {
let mut found = false;
for param in &self.gp {
@@ -117,7 +208,7 @@ impl<'n> Engine<'n> {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v.extend(["target"].iter().map(|v|*v));
v.extend(["model", "target"].iter().map(|v|*v));
v } ));
}
}
@@ -128,6 +219,9 @@ impl<'n> Engine<'n> {
Ok(())
} else {
assert!(err.issues.len() == 0);
for scope in self.opt.values_of("url").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
call = call.add_scope(scope);
}
let mut ostream = match writer_from_opts(opt.value_of("out")) {
Ok(mut f) => f,
Err(io_err) => return Err(DoitError::IoError(opt.value_of("out").unwrap_or("-").to_string(), io_err)),
@@ -157,6 +251,9 @@ impl<'n> Engine<'n> {
"source" => {
call = call.source(value.unwrap_or(""));
},
"model" => {
call = call.model(value.unwrap_or(""));
},
"format" => {
call = call.format(value.unwrap_or(""));
},
@@ -176,7 +273,7 @@ impl<'n> Engine<'n> {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v.extend(["source", "cid", "format"].iter().map(|v|*v));
v.extend(["source", "model", "cid", "format"].iter().map(|v|*v));
v } ));
}
}
@@ -187,6 +284,98 @@ impl<'n> Engine<'n> {
Ok(())
} else {
assert!(err.issues.len() == 0);
for scope in self.opt.values_of("url").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
call = call.add_scope(scope);
}
let mut ostream = match writer_from_opts(opt.value_of("out")) {
Ok(mut f) => f,
Err(io_err) => return Err(DoitError::IoError(opt.value_of("out").unwrap_or("-").to_string(), io_err)),
};
match match protocol {
CallType::Standard => call.doit(),
_ => unreachable!()
} {
Err(api_err) => Err(DoitError::ApiError(api_err)),
Ok((mut response, output_schema)) => {
let mut value = json::value::to_value(&output_schema).expect("serde to work");
remove_json_null_values(&mut value);
json::to_writer_pretty(&mut ostream, &value).unwrap();
ostream.flush().unwrap();
Ok(())
}
}
}
}
fn _translations_translate(&self, opt: &ArgMatches<'n>, dry_run: bool, err: &mut InvalidOptionsError)
-> Result<(), DoitError> {
let mut field_cursor = FieldCursor::default();
let mut object = json::value::Value::Object(Default::default());
for kvarg in opt.values_of("kv").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
let last_errc = err.issues.len();
let (key, value) = parse_kv_arg(&*kvarg, err, false);
let mut temp_cursor = field_cursor.clone();
if let Err(field_err) = temp_cursor.set(&*key) {
err.issues.push(field_err);
}
if value.is_none() {
field_cursor = temp_cursor.clone();
if err.issues.len() > last_errc {
err.issues.remove(last_errc);
}
continue;
}
let type_info: Option<(&'static str, JsonTypeInfo)> =
match &temp_cursor.to_string()[..] {
"q" => Some(("q", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Vec })),
"source" => Some(("source", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Pod })),
"model" => Some(("model", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Pod })),
"target" => Some(("target", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Pod })),
"format" => Some(("format", JsonTypeInfo { jtype: JsonType::String, ctype: ComplexType::Pod })),
_ => {
let suggestion = FieldCursor::did_you_mean(key, &vec!["format", "model", "q", "source", "target"]);
err.issues.push(CLIError::Field(FieldError::Unknown(temp_cursor.to_string(), suggestion, value.map(|v| v.to_string()))));
None
}
};
if let Some((field_cursor_str, type_info)) = type_info {
FieldCursor::from(field_cursor_str).set_json_value(&mut object, value.unwrap(), type_info, err, &temp_cursor);
}
}
let mut request: api::TranslateTextRequest = json::value::from_value(object).unwrap();
let mut call = self.hub.translations().translate(request);
for parg in opt.values_of("v").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
let (key, value) = parse_kv_arg(&*parg, err, false);
match key {
_ => {
let mut found = false;
for param in &self.gp {
if key == *param {
found = true;
call = call.param(self.gpm.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, value.unwrap_or("unset"));
break;
}
}
if !found {
err.issues.push(CLIError::UnknownParameter(key.to_string(),
{let mut v = Vec::new();
v.extend(self.gp.iter().map(|v|*v));
v } ));
}
}
}
}
let protocol = CallType::Standard;
if dry_run {
Ok(())
} else {
assert!(err.issues.len() == 0);
for scope in self.opt.values_of("url").map(|i|i.collect()).unwrap_or(Vec::new()).iter() {
call = call.add_scope(scope);
}
let mut ostream = match writer_from_opts(opt.value_of("out")) {
Ok(mut f) => f,
Err(io_err) => return Err(DoitError::IoError(opt.value_of("out").unwrap_or("-").to_string(), io_err)),
@@ -214,6 +403,9 @@ impl<'n> Engine<'n> {
match self.opt.subcommand() {
("detections", Some(opt)) => {
match opt.subcommand() {
("detect", Some(opt)) => {
call_result = self._detections_detect(opt, dry_run, &mut err);
},
("list", Some(opt)) => {
call_result = self._detections_list(opt, dry_run, &mut err);
},
@@ -239,6 +431,9 @@ impl<'n> Engine<'n> {
("list", Some(opt)) => {
call_result = self._translations_list(opt, dry_run, &mut err);
},
("translate", Some(opt)) => {
call_result = self._translations_translate(opt, dry_run, &mut err);
},
_ => {
err.issues.push(CLIError::MissingMethodError("translations".to_string()));
writeln!(io::stderr(), "{}\n", opt.usage()).ok();
@@ -300,12 +495,16 @@ impl<'n> Engine<'n> {
let engine = Engine {
opt: opt,
hub: api::Translate::new(client, auth),
gp: vec!["alt", "fields", "key", "oauth-token", "pretty-print", "quota-user", "user-ip"],
gp: vec!["$-xgafv", "access-token", "alt", "bearer-token", "callback", "fields", "key", "oauth-token", "pp", "pretty-print", "quota-user", "upload-type", "upload-protocol"],
gpm: vec![
("$-xgafv", "$.xgafv"),
("access-token", "access_token"),
("bearer-token", "bearer_token"),
("oauth-token", "oauth_token"),
("pretty-print", "prettyPrint"),
("quota-user", "quotaUser"),
("user-ip", "userIp"),
("upload-type", "uploadType"),
("upload-protocol", "upload_protocol"),
]
};
@@ -327,14 +526,37 @@ impl<'n> Engine<'n> {
fn main() {
let mut exit_status = 0i32;
let arg_data = [
("detections", "methods: 'list'", vec![
("detections", "methods: 'detect' and 'list'", vec![
("detect",
Some(r##"Detects the language of text within a request."##),
"Details at http://byron.github.io/google-apis-rs/google_translate2_cli/detections_detect",
vec![
(Some(r##"kv"##),
Some(r##"r"##),
Some(r##"Set various fields of the request structure, matching the key=value form"##),
Some(true),
Some(true)),
(Some(r##"v"##),
Some(r##"p"##),
Some(r##"Set various optional parameters, matching the key=value form"##),
Some(false),
Some(true)),
(Some(r##"out"##),
Some(r##"o"##),
Some(r##"Specify the file into which to write the program's output"##),
Some(false),
Some(false)),
]),
("list",
Some(r##"Detect the language of text."##),
Some(r##"Detects the language of text within a request."##),
"Details at http://byron.github.io/google-apis-rs/google_translate2_cli/detections_list",
vec![
(Some(r##"q"##),
None,
Some(r##"The text to detect"##),
Some(r##"The input text upon which to perform language detection. Repeat this
parameter to perform language detection on multiple text inputs."##),
Some(true),
Some(false)),
@@ -354,7 +576,7 @@ fn main() {
("languages", "methods: 'list'", vec![
("list",
Some(r##"List the source/target languages supported by the API"##),
Some(r##"Returns a list of supported languages for translation."##),
"Details at http://byron.github.io/google-apis-rs/google_translate2_cli/languages_list",
vec![
(Some(r##"v"##),
@@ -371,20 +593,22 @@ fn main() {
]),
]),
("translations", "methods: 'list'", vec![
("translations", "methods: 'list' and 'translate'", vec![
("list",
Some(r##"Returns text translations from one language to another."##),
Some(r##"Translates input text, returning translated text."##),
"Details at http://byron.github.io/google-apis-rs/google_translate2_cli/translations_list",
vec![
(Some(r##"q"##),
None,
Some(r##"The text to translate"##),
Some(r##"The input text to translate. Repeat this parameter to perform translation
operations on multiple text inputs."##),
Some(true),
Some(false)),
(Some(r##"target"##),
None,
Some(r##"The target language into which the text should be translated"##),
Some(r##"The language to use for translation of the input text, set to one of the
language codes listed in Language Support."##),
Some(true),
Some(false)),
@@ -394,6 +618,28 @@ fn main() {
Some(false),
Some(true)),
(Some(r##"out"##),
Some(r##"o"##),
Some(r##"Specify the file into which to write the program's output"##),
Some(false),
Some(false)),
]),
("translate",
Some(r##"Translates input text, returning translated text."##),
"Details at http://byron.github.io/google-apis-rs/google_translate2_cli/translations_translate",
vec![
(Some(r##"kv"##),
Some(r##"r"##),
Some(r##"Set various fields of the request structure, matching the key=value form"##),
Some(true),
Some(true)),
(Some(r##"v"##),
Some(r##"p"##),
Some(r##"Set various optional parameters, matching the key=value form"##),
Some(false),
Some(true)),
(Some(r##"out"##),
Some(r##"o"##),
Some(r##"Specify the file into which to write the program's output"##),
@@ -406,9 +652,15 @@ fn main() {
let mut app = App::new("translate2")
.author("Sebastian Thiel <byronimo@gmail.com>")
.version("1.0.6+20160627")
.about("Translates text from one language to another.")
.version("1.0.6+20170525")
.about("The Google Cloud Translation API lets websites and programs integrate with
Google Translate programmatically.")
.after_help("All documentation details can be found at http://byron.github.io/google-apis-rs/google_translate2_cli")
.arg(Arg::with_name("url")
.long("scope")
.help("Specify the authentication a method should be executed in. Each scope requires the user to grant this application permission to use it.If unset, it defaults to the shortest scope url for a particular method.")
.multiple(true)
.takes_value(true))
.arg(Arg::with_name("folder")
.long("config-dir")
.help("A directory into which we will store our persistent data. Defaults to a user-writable directory that we will create during the first invocation.[default: ~/.google-service-cli")