diff --git a/modules/swagger-codegen/src/main/resources/python/api.mustache b/modules/swagger-codegen/src/main/resources/python/api.mustache index b853cbc9c95..fc779a66653 100644 --- a/modules/swagger-codegen/src/main/resources/python/api.mustache +++ b/modules/swagger-codegen/src/main/resources/python/api.mustache @@ -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, diff --git a/modules/swagger-codegen/src/main/resources/python/swagger.mustache b/modules/swagger-codegen/src/main/resources/python/swagger.mustache index 549106014e3..ab45eace646 100644 --- a/modules/swagger-codegen/src/main/resources/python/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/python/swagger.mustache @@ -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] diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py index bf8a51d7ca2..a599aeaabc1 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/pet_api.py @@ -29,6 +29,8 @@ from six import iteritems from ..util import remove_none +from ..swagger import ApiClient + class PetApi(object): def __init__(self, api_client): @@ -63,11 +65,13 @@ class PetApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = ['application/json', 'application/xml'] - 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(['application/json', 'application/xml']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -102,11 +106,13 @@ class PetApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = ['application/json', 'application/xml'] - 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(['application/json', 'application/xml']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -141,11 +147,13 @@ class PetApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -182,11 +190,13 @@ class PetApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -227,11 +237,13 @@ class PetApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -274,11 +286,13 @@ class PetApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = ['application/x-www-form-urlencoded'] - 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(['application/x-www-form-urlencoded']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -318,11 +332,13 @@ class PetApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -363,11 +379,13 @@ class PetApi(object): files = remove_none(dict(file=params.get('file'))) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = ['multipart/form-data'] - 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(['multipart/form-data']) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py index 0df9a84fe75..7f9b852f7ca 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/store_api.py @@ -29,6 +29,8 @@ from six import iteritems from ..util import remove_none +from ..swagger import ApiClient + class StoreApi(object): def __init__(self, api_client): @@ -62,11 +64,13 @@ class StoreApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -103,11 +107,13 @@ class StoreApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -148,11 +154,13 @@ class StoreApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -193,11 +201,13 @@ class StoreApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py index 720843bd508..2471970c436 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/apis/user_api.py @@ -29,6 +29,8 @@ from six import iteritems from ..util import remove_none +from ..swagger import ApiClient + class UserApi(object): def __init__(self, api_client): @@ -63,11 +65,13 @@ class UserApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -102,11 +106,13 @@ class UserApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -141,11 +147,13 @@ class UserApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -181,11 +189,13 @@ class UserApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -221,11 +231,13 @@ class UserApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -264,11 +276,13 @@ class UserApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -310,11 +324,13 @@ class UserApi(object): files = remove_none(dict()) body_params = params.get('body') - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, @@ -353,11 +369,13 @@ class UserApi(object): files = remove_none(dict()) body_params = None - accepts = ['application/json', 'application/xml'] - header_params['Accept'] = ', '.join(accepts) + # HTTP header `Accept` + header_params['Accept'] = ApiClient.select_header_accept(['application/json', 'application/xml']) + if not header_params['Accept']: + del header_params['Accept'] - content_types = [] - 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([]) response = self.api_client.call_api(resource_path, method, path_params, query_params, header_params, body=body_params, post_params=form_params, files=files, diff --git a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py index 549106014e3..ab45eace646 100644 --- a/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py +++ b/samples/client/petstore/python/SwaggerPetstore-python/SwaggerPetstore/swagger.py @@ -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] diff --git a/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py new file mode 100644 index 00000000000..9adf7cdb99d --- /dev/null +++ b/samples/client/petstore/python/SwaggerPetstore-python/tests/test_api_client.py @@ -0,0 +1,66 @@ +# coding: utf-8 + +""" +Run the tests. +$ pip install nose (optional) +$ cd SwaggerPetstore-python +$ nosetests -v +""" + +import os +import time +import unittest + +import SwaggerPetstore + +HOST = 'http://petstore.swagger.io/v2' + + +class ApiClientTests(unittest.TestCase): + + def setUp(self): + self.api_client = SwaggerPetstore.ApiClient(HOST) + + def test_select_header_accept(self): + accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'application/json') + + accepts = ['application/json', 'application/xml'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'application/json') + + accepts = ['application/xml', 'application/json'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'application/json') + + accepts = ['text/plain', 'application/xml'] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, 'text/plain, application/xml') + + accepts = [] + accept = SwaggerPetstore.ApiClient.select_header_accept(accepts) + self.assertEqual(accept, None) + + def test_select_header_content_type(self): + content_types = ['APPLICATION/JSON', 'APPLICATION/XML'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + + content_types = ['application/json', 'application/xml'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + + content_types = ['application/xml', 'application/json'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + + content_types = ['text/plain', 'application/xml'] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'text/plain') + + content_types = [] + content_type = SwaggerPetstore.ApiClient.select_header_content_type(content_types) + self.assertEqual(content_type, 'application/json') + +