Merge pull request #774 from geekerzp/develop_2.0_python_contenttype

Minor improvement to Python API Client 'accept' and 'content-type' header
This commit is contained in:
Tony Tam
2015-05-23 08:53:34 -07:00
7 changed files with 252 additions and 86 deletions

View File

@@ -29,6 +29,8 @@ from six import iteritems
from ..util import remove_none
from ..swagger import ApiClient
{{#operations}}
class {{classname}}(object):
@@ -68,11 +70,13 @@ class {{classname}}(object):
files = remove_none(dict({{#formParams}}{{#isFile}}{{baseName}}=params.get('{{paramName}}'){{#hasMore}}, {{/hasMore}}{{/isFile}}{{/formParams}}))
body_params = {{#bodyParam}}params.get('{{paramName}}'){{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}
accepts = [{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]
header_params['Accept'] = ', '.join(accepts)
# HTTP header `Accept`
header_params['Accept'] = ApiClient.select_header_accept([{{#produces}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
if not header_params['Accept']:
del header_params['Accept']
content_types = [{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]
header_params['Content-Type'] = content_types[0] if len(content_types) > 0 else 'application/json'
# HTTP header `Content-Type`
header_params['Content-Type'] = ApiClient.select_header_content_type([{{#consumes}}'{{mediaType}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}])
response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params,
body=body_params, post_params=form_params, files=files,

View File

@@ -105,7 +105,7 @@ class ApiClient(object):
def to_path_value(self, obj):
"""
Convert a string or object to a path-friendly value
:param obj: object or string value
:return string: quoted value
@@ -253,7 +253,32 @@ class ApiClient(object):
return params
@staticmethod
def select_header_accept(accepts):
"""
Return `Accept` based on an array of accepts provided
"""
if not accepts:
return
accepts = list(map(lambda x: x.lower(), accepts))
if 'application/json' in accepts:
return 'application/json'
else:
return ', '.join(accepts)
@staticmethod
def select_header_content_type(content_types):
"""
Return `Content-Type` baseed on an array of content_types provided
"""
if not content_types:
return 'application/json'
content_types = list(map(lambda x: x.lower(), content_types))
if 'application/json' in content_types:
return 'application/json'
else:
return content_types[0]