Skip to content

Commit

Permalink
[Python] Add option to select content-type using body or force it for… (
Browse files Browse the repository at this point in the history
#10686)

* [Python] Add option to select content-type using body or force it for API call

* Add support for application/json-patch+json

* Add unittests.
  • Loading branch information
tomplus authored Nov 22, 2021
1 parent c13067d commit e9f2ccd
Show file tree
Hide file tree
Showing 33 changed files with 819 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class {{classname}}(object):
request; this effectively ignores the authentication
in the spec for a single request.
:type _request_auth: dict, optional
:type _content_type: string, optional: force content-type for the request
:return: Returns the result object.
If the method is called asynchronously,
returns the request thread.
Expand Down Expand Up @@ -142,7 +143,8 @@ class {{classname}}(object):
'_return_http_data_only',
'_preload_content',
'_request_timeout',
'_request_auth'
'_request_auth',
'_content_type'
]
)

Expand Down Expand Up @@ -247,8 +249,10 @@ class {{classname}}(object):
{{/hasProduces}}
{{#hasConsumes}}
# HTTP header `Content-Type`
header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501
[{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}]) # noqa: E501
header_params['Content-Type'] = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
[{{#consumes}}'{{{mediaType}}}'{{^-last}}, {{/-last}}{{/consumes}}],
'{{httpMethod}}', body_params)) # noqa: E501

{{/hasConsumes}}
# Authentication setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,17 +545,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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
request; this effectively ignores the authentication
in the spec for a single request.
:type _request_auth: dict, optional
:type _content_type: string, optional: force content-type for the request
:return: Returns the result object.
If the method is called asynchronously,
returns the request thread.
Expand All @@ -112,7 +113,8 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
'_return_http_data_only',
'_preload_content',
'_request_timeout',
'_request_auth'
'_request_auth',
'_content_type'
]
)

Expand Down Expand Up @@ -148,8 +150,10 @@ def call_123_test_special_tags_with_http_info(self, body, **kwargs): # noqa: E5
['application/json']) # noqa: E501

# HTTP header `Content-Type`
header_params['Content-Type'] = self.api_client.select_header_content_type( # noqa: E501
['application/json']) # noqa: E501
header_params['Content-Type'] = local_var_params.get('_content_type',
self.api_client.select_header_content_type(
['application/json'],
'PATCH', body_params)) # noqa: E501

# Authentication setting
auth_settings = [] # noqa: E501
Expand Down
Loading

0 comments on commit e9f2ccd

Please sign in to comment.