Skip to content

[Python] Add option to select/detect content-type. #10978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ class {{classname}}(object):
_check_return_type (bool): specifies if type checking
should be done one the data received from the server.
Default is True.
_content_type (str/None): force body content-type.
Default is None and content-type will be predicted by allowed
content-types and body.
_host_index (int/None): specifies the index of the server
that we want to use.
Default is read from the configuration.
Expand Down Expand Up @@ -298,6 +301,8 @@ class {{classname}}(object):
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_content_type'] = kwargs.get(
'_content_type')
kwargs['_host_index'] = kwargs.get('_host_index')
{{#requiredParams}}
kwargs['{{paramName}}'] = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class ApiClient(object):
_preload_content: bool = True,
_request_timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
_host: typing.Optional[str] = None,
_check_type: typing.Optional[bool] = None
_check_type: typing.Optional[bool] = None,
_content_type: typing.Optional[str] = None
):

config = self.configuration
Expand Down Expand Up @@ -584,17 +585,24 @@ class ApiClient(object):
else:
return ', '.join(accepts)

def select_header_content_type(self, content_types):
def select_header_content_type(self, content_types, method=None, body=None):
"""Returns `Content-Type` based on an array of content_types provided.

:param content_types: List of content-types.
:param method: http method (e.g. POST, PATCH).
:param body: http body to send.
:return: Content-Type (e.g. application/json).
"""
if not content_types:
return 'application/json'

content_types = [x.lower() for x in content_types]

if (method == 'PATCH' and
'application/json-patch+json' in content_types and
isinstance(body, list)):
return 'application/json-patch+json'

if 'application/json' in content_types or '*/*' in content_types:
return 'application/json'
else:
Expand Down Expand Up @@ -685,7 +693,8 @@ class Endpoint(object):
'_request_timeout',
'_return_http_data_only',
'_check_input_type',
'_check_return_type'
'_check_return_type',
'_content_type'
])
self.params_map['nullable'].extend(['_request_timeout'])
self.validations = root_map['validations']
Expand All @@ -698,7 +707,8 @@ class Endpoint(object):
'_request_timeout': (none_type, float, (float,), [float], int, (int,), [int]),
'_return_http_data_only': (bool,),
'_check_input_type': (bool,),
'_check_return_type': (bool,)
'_check_return_type': (bool,),
'_content_type': (none_type, str)
}
self.openapi_types.update(extra_types)
self.attribute_map = root_map['attribute_map']
Expand Down Expand Up @@ -845,12 +855,16 @@ class Endpoint(object):
params['header']['Accept'] = self.api_client.select_header_accept(
accept_headers_list)

content_type_headers_list = self.headers_map['content_type']
if content_type_headers_list:
if params['body'] != "":
header_list = self.api_client.select_header_content_type(
content_type_headers_list)
params['header']['Content-Type'] = header_list
if kwargs.get('_content_type'):
params['header']['Content-Type'] = kwargs['_content_type']
else:
content_type_headers_list = self.headers_map['content_type']
if content_type_headers_list:
if params['body'] != "":
header_list = self.api_client.select_header_content_type(
content_type_headers_list, self.settings['http_method'],
params['body'])
params['header']['Content-Type'] = header_list

return self.api_client.call_api(
self.settings['endpoint_path'], self.settings['http_method'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def call_123_test_special_tags(
_check_return_type (bool): specifies if type checking
should be done one the data received from the server.
Default is True.
_content_type (str/None): force body content-type.
Default is None and content-type will be predicted by allowed
content-types and body.
_host_index (int/None): specifies the index of the server
that we want to use.
Default is read from the configuration.
Expand Down Expand Up @@ -147,6 +150,8 @@ def call_123_test_special_tags(
kwargs['_check_return_type'] = kwargs.get(
'_check_return_type', True
)
kwargs['_content_type'] = kwargs.get(
'_content_type')
kwargs['_host_index'] = kwargs.get('_host_index')
kwargs['body'] = \
body
Expand Down
Loading