From 253309848427f2326bc7e1c1343867117867bc41 Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Thu, 14 Jan 2016 10:48:40 -0800 Subject: [PATCH] Add option to force passing alt=json for some APIs that return xml by default --- lib/google/apis/generator/annotator.rb | 5 +++++ lib/google/apis/generator/model.rb | 3 +++ lib/google/apis/generator/templates/_method.tmpl | 3 +++ 3 files changed, 11 insertions(+) diff --git a/lib/google/apis/generator/annotator.rb b/lib/google/apis/generator/annotator.rb index 9aefed905..1614cdddb 100644 --- a/lib/google/apis/generator/annotator.rb +++ b/lib/google/apis/generator/annotator.rb @@ -90,6 +90,10 @@ module Google @path.reduce('') { |a, e| a + '/' + e } end + def option(opt_name) + @names[sprintf('%s?%s', key, opt_name)] + end + private # For RPC style methods, pick a name based off the request objects. @@ -184,6 +188,7 @@ module Google value.constant = constantize_scope(key) end end + @rest_description.force_alt_json = @names.option('force_alt_json') @rest_description.parameters.reject! { |k, _v| PARAMETER_BLACKLIST.include?(k) } annotate_parameters(@rest_description.parameters) annotate_resource(@rest_description.name, @rest_description) diff --git a/lib/google/apis/generator/model.rb b/lib/google/apis/generator/model.rb index 5441166ed..b2b59080f 100644 --- a/lib/google/apis/generator/model.rb +++ b/lib/google/apis/generator/model.rb @@ -99,6 +99,9 @@ module Google end class RestDescription + attr_accessor :force_alt_json + alias_method :force_alt_json?, :force_alt_json + def version ActiveSupport::Inflector.camelize(@version.gsub(/\W/, '-')).gsub(/-/, '_') end diff --git a/lib/google/apis/generator/templates/_method.tmpl b/lib/google/apis/generator/templates/_method.tmpl index 6d8bb6568..6f1ddfafc 100644 --- a/lib/google/apis/generator/templates/_method.tmpl +++ b/lib/google/apis/generator/templates/_method.tmpl @@ -73,6 +73,9 @@ def <%= api_method.generated_name %>(<% for param in api_method.required_paramet command.request_object = <%= api.schemas[api_method.request._ref].generated_name %>_object <% end -%> <% if api_method.response -%> +<% if api.force_alt_json? -%> + command.query['alt'] = 'json' +<% end -%> command.response_representation = <%= api.schemas[api_method.response._ref].generated_type %>::Representation command.response_class = <%= api.schemas[api_method.response._ref].generated_type %> <% end -%>