Skip to content

Commit

Permalink
fix(CLI): alt-media handling in CLI+API-docs
Browse files Browse the repository at this point in the history
* API-docs now adjust depending on where 'alt' is set (either as global
  parameter, or as method-parameter)
* CLI: download tracking now works for 'alt' as method-parameter
* CLI: global parameter remapping allows them to be named consistently,
  but map to the name required by the google API.

Fixes #61
  • Loading branch information
Byron committed Apr 15, 2015
1 parent 36a7cb2 commit 306852d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/mako/api/lib/mbuild.mako
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
ThisType = mb_type(resource, method) + mb_tparams
params, request_value = build_all_params(c, m)
alt_param = None
for p in params:
if p.name == 'alt':
alt_param = p
break
# end
# end
part_prop, parts = parts_from_params(params)
part_desc = make_parts_desc(part_prop)
Expand All @@ -56,7 +63,11 @@ ${m.description | rust_doc_comment}
% endif
% if m.get('supportsMediaDownload', False):
/// This method supports **media download**. To enable it, adjust the builder like this:
% if alt_param:
/// `.${mangle_ident(setter_fn_name(alt_param))}("media")`.
% else:
/// `${ADD_PARAM_MEDIA_EXAMPLE}`.
% endif
% if response_schema:
/// Please note that due to missing multi-part support on the server side, you will only receive the media,
/// but not the `${unique_type_name(response_schema.id)}` structure that you would usually get. The latter will be a default value.
Expand Down Expand Up @@ -140,7 +151,7 @@ ${self._setter_fn(resource, method, m, p, part_prop, ThisType, c)}\
///
/// # Additional Parameters
///
% for opn, op in parameters.iteritems():
% for opn, op in list((opn, op) for (opn, op) in parameters.iteritems() if opn not in [p.name for p in params]):
/// * *${opn}* (${op.location}-${op.type}) - ${op.description}
% endfor
% endif
Expand Down
35 changes: 26 additions & 9 deletions src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ self.opt.${cmd_ident(method)} {
mc = new_method_context(resource, method, c)
supports_media_download = mc.m.get('supportsMediaDownload', False)
handle_output = mc.response_schema or supports_media_download
track_download_flag = supports_media_download and parameters is not UNDEFINED and 'alt' in parameters
optional_props = [p for p in mc.optional_props if not p.get('skip_example', False)]
optional_prop_names = set(p.name for p in optional_props)
global_parameter_names = set()
track_download_flag = (supports_media_download and
(parameters is not UNDEFINED and 'alt' in parameters) or ('alt' in optional_prop_names))
if parameters is not UNDEFINED:
global_parameter_names = list(pn for pn in sorted(parameters.keys()) if pn not in optional_prop_names)
handle_props = optional_props or parameters is not UNDEFINED
%>\
## REQUIRED PARAMETERS
Expand Down Expand Up @@ -176,20 +180,28 @@ for parg in ${SOPT + arg_ident(VALUE_ARG)}.iter() {
ptype = 'int64'
value_unwrap = 'value.unwrap_or("%s")' % JSON_TYPE_RND_MAP[ptype]()
%>\
"${ident(p.name)}" => call = call.${mangle_ident(setter_fn_name(p))}(\
"${mangle_subcommand(p.name)}" => {
% if p.name == 'alt':
if ${value_unwrap} == "media" {
download_mode = true;
}
% endif
call = call.${mangle_ident(setter_fn_name(p))}(\
% if ptype != 'string':
arg_from_str(${value_unwrap}, err, "${ident(p.name)}", "${p.type}")),
arg_from_str(${value_unwrap}, err, "${mangle_subcommand(p.name)}", "${p.type}")\
% else:
${value_unwrap}),
${value_unwrap}\
% endif # handle conversion
);
},
% endfor # each property
% if parameters is not UNDEFINED:
% for pn, p in list((pn, p) for (pn, p) in parameters.iteritems() if pn not in optional_prop_names):
% for pn in global_parameter_names:
\
% if not loop.first:
|\
% endif
"${ident(pn)}"\
"${mangle_subcommand(pn)}"\
% if not loop.last:
% endif
Expand All @@ -198,12 +210,17 @@ ${value_unwrap}),
<%
value_unwrap = 'value.unwrap_or("unset")'
%>\
% if track_download_flag:
% if track_download_flag and 'alt' in global_parameter_names:
if key == "alt" && ${value_unwrap} == "media" {
download_mode = true;
}
% endif
call = call.${ADD_PARAM_FN}(key, ${value_unwrap})
let map = [
% for pn in list(pn for pn in global_parameter_names if mangle_subcommand(pn) != pn):
("${mangle_subcommand(pn)}", "${pn}"),
% endfor # each global parameter
];
call = call.${ADD_PARAM_FN}(map.iter().find(|t| t.0 == key).unwrap_or(&("", key)).1, ${value_unwrap})
},
% endif # handle global parameters
_ => err.issues.push(CLIError::UnknownParameter(key.to_string())),
Expand Down

0 comments on commit 306852d

Please sign in to comment.