From 7184f1ec6800b7fcdd7d207f747a7c0128e55aaf Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 24 May 2018 17:10:07 +0200 Subject: [PATCH] [python] asyncio supports _preload_content; remove unsed imports (#107) * chore: update python samples * feat: python/asyncio support for _preload_content * feat: remove unused imports from python clients; fix discriminator --- .../main/resources/python/asyncio/rest.mustache | 16 +++++++++++----- .../src/main/resources/python/model.mustache | 6 +----- .../petstore/python-asyncio/docs/FakeApi.md | 2 +- .../python-asyncio/petstore_api/models/animal.py | 2 +- .../petstore_api/models/animal_farm.py | 2 -- .../petstore_api/models/array_test.py | 2 -- .../python-asyncio/petstore_api/models/cat.py | 2 -- .../python-asyncio/petstore_api/models/dog.py | 2 -- .../petstore_api/models/enum_test.py | 2 -- ...properties_and_additional_properties_class.py | 2 -- .../python-asyncio/petstore_api/models/pet.py | 3 --- .../petstore/python-asyncio/petstore_api/rest.py | 16 +++++++++++----- .../petstore/python-tornado/docs/FakeApi.md | 2 +- .../python-tornado/petstore_api/models/animal.py | 2 +- .../petstore_api/models/animal_farm.py | 2 -- .../petstore_api/models/array_test.py | 2 -- .../python-tornado/petstore_api/models/cat.py | 2 -- .../python-tornado/petstore_api/models/dog.py | 2 -- .../petstore_api/models/enum_test.py | 2 -- ...properties_and_additional_properties_class.py | 2 -- .../python-tornado/petstore_api/models/pet.py | 3 --- samples/client/petstore/python/docs/FakeApi.md | 2 +- .../python/petstore_api/models/animal.py | 2 +- .../python/petstore_api/models/animal_farm.py | 2 -- .../python/petstore_api/models/array_test.py | 2 -- .../petstore/python/petstore_api/models/cat.py | 2 -- .../petstore/python/petstore_api/models/dog.py | 2 -- .../python/petstore_api/models/enum_test.py | 2 -- ...properties_and_additional_properties_class.py | 2 -- .../petstore/python/petstore_api/models/pet.py | 3 --- .../python/tests/test_deserialization.py | 15 +++++++++++++++ 31 files changed, 44 insertions(+), 66 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index 39003abfbfdc..8c9d49dc14f9 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -10,6 +10,7 @@ import ssl import aiohttp import certifi +import asyncio # python 2 and python 3 compatibility library from six.moves.urllib.parse import urlencode @@ -70,6 +71,9 @@ class RESTClientObject(object): connector=connector ) + def __del__(self): + asyncio.ensure_future(self.pool_manager.close()) + async def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): @@ -152,15 +156,17 @@ class RESTClientObject(object): declared content type.""" raise ApiException(status=0, reason=msg) - async with self.pool_manager.request(**args) as r: + r = await self.pool_manager.request(**args) + if _preload_content: + data = await r.text() r = RESTResponse(r, data) - # log response body - logger.debug("response body: %s", r.data) + # log response body + logger.debug("response body: %s", r.data) - if not 200 <= r.status <= 299: - raise ApiException(http_resp=r) + if not 200 <= r.status <= 299: + raise ApiException(http_resp=r) return r diff --git a/modules/openapi-generator/src/main/resources/python/model.mustache b/modules/openapi-generator/src/main/resources/python/model.mustache index acac422fd6b6..123577adb869 100644 --- a/modules/openapi-generator/src/main/resources/python/model.mustache +++ b/modules/openapi-generator/src/main/resources/python/model.mustache @@ -6,10 +6,6 @@ import pprint import re # noqa: F401 import six -{{#imports}}{{#-first}} -{{/-first}} -{{import}} # noqa: F401,E501 -{{/imports}} {{#models}} @@ -170,7 +166,7 @@ class {{classname}}(object): {{#discriminator}} def get_real_child_model(self, data): """Returns the real base class specified by the discriminator""" - discriminator_value = data[self.discriminator].lower() + discriminator_value = data[self.discriminator] return self.discriminator_value_class_map.get(discriminator_value) {{/discriminator}} diff --git a/samples/client/petstore/python-asyncio/docs/FakeApi.md b/samples/client/petstore/python-asyncio/docs/FakeApi.md index 586616d73603..3b30ab1b4e9c 100644 --- a/samples/client/petstore/python-asyncio/docs/FakeApi.md +++ b/samples/client/petstore/python-asyncio/docs/FakeApi.md @@ -419,7 +419,7 @@ Name | Type | Description | Notes **enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to '-efg'] **enum_query_integer** | **int**| Query parameter enum test (double) | [optional] **enum_query_double** | **float**| Query parameter enum test (double) | [optional] - **enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to '$'] + **enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to '$'] **enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to '-efg'] ### Return type diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/animal.py b/samples/client/petstore/python-asyncio/petstore_api/models/animal.py index 4a0768026542..5acccf50f7ab 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/animal.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/animal.py @@ -102,7 +102,7 @@ def color(self, color): def get_real_child_model(self, data): """Returns the real base class specified by the discriminator""" - discriminator_value = data[self.discriminator].lower() + discriminator_value = data[self.discriminator] return self.discriminator_value_class_map.get(discriminator_value) def to_dict(self): diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/animal_farm.py b/samples/client/petstore/python-asyncio/petstore_api/models/animal_farm.py index 238d580ba528..4894e5530a33 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/animal_farm.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/animal_farm.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class AnimalFarm(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/array_test.py b/samples/client/petstore/python-asyncio/petstore_api/models/array_test.py index 04a871e8f74d..05cc108139a5 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/array_test.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/array_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501 - class ArrayTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/cat.py b/samples/client/petstore/python-asyncio/petstore_api/models/cat.py index 808409c22669..c5c87a6b1118 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/cat.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/cat.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Cat(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/dog.py b/samples/client/petstore/python-asyncio/petstore_api/models/dog.py index f7fa73c9d4c8..c2bc8f745d15 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/dog.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/dog.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Dog(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/enum_test.py b/samples/client/petstore/python-asyncio/petstore_api/models/enum_test.py index cfe0ebcb84a5..0fd60c4a351d 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/enum_test.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/enum_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501 - class EnumTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/mixed_properties_and_additional_properties_class.py b/samples/client/petstore/python-asyncio/petstore_api/models/mixed_properties_and_additional_properties_class.py index a38141493f84..ecf67acc5e95 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/mixed_properties_and_additional_properties_class.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class MixedPropertiesAndAdditionalPropertiesClass(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/models/pet.py b/samples/client/petstore/python-asyncio/petstore_api/models/pet.py index 27891437778c..d3c412f4a82b 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/models/pet.py +++ b/samples/client/petstore/python-asyncio/petstore_api/models/pet.py @@ -15,9 +15,6 @@ import six -from petstore_api.models.category import Category # noqa: F401,E501 -from petstore_api.models.tag import Tag # noqa: F401,E501 - class Pet(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-asyncio/petstore_api/rest.py b/samples/client/petstore/python-asyncio/petstore_api/rest.py index f25b4bbe9a29..f0ac582a697f 100644 --- a/samples/client/petstore/python-asyncio/petstore_api/rest.py +++ b/samples/client/petstore/python-asyncio/petstore_api/rest.py @@ -18,6 +18,7 @@ import aiohttp import certifi +import asyncio # python 2 and python 3 compatibility library from six.moves.urllib.parse import urlencode @@ -78,6 +79,9 @@ def __init__(self, configuration, pools_size=4, maxsize=4): connector=connector ) + def __del__(self): + asyncio.ensure_future(self.pool_manager.close()) + async def request(self, method, url, query_params=None, headers=None, body=None, post_params=None, _preload_content=True, _request_timeout=None): @@ -160,15 +164,17 @@ async def request(self, method, url, query_params=None, headers=None, declared content type.""" raise ApiException(status=0, reason=msg) - async with self.pool_manager.request(**args) as r: + r = await self.pool_manager.request(**args) + if _preload_content: + data = await r.text() r = RESTResponse(r, data) - # log response body - logger.debug("response body: %s", r.data) + # log response body + logger.debug("response body: %s", r.data) - if not 200 <= r.status <= 299: - raise ApiException(http_resp=r) + if not 200 <= r.status <= 299: + raise ApiException(http_resp=r) return r diff --git a/samples/client/petstore/python-tornado/docs/FakeApi.md b/samples/client/petstore/python-tornado/docs/FakeApi.md index 586616d73603..3b30ab1b4e9c 100644 --- a/samples/client/petstore/python-tornado/docs/FakeApi.md +++ b/samples/client/petstore/python-tornado/docs/FakeApi.md @@ -419,7 +419,7 @@ Name | Type | Description | Notes **enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to '-efg'] **enum_query_integer** | **int**| Query parameter enum test (double) | [optional] **enum_query_double** | **float**| Query parameter enum test (double) | [optional] - **enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to '$'] + **enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to '$'] **enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to '-efg'] ### Return type diff --git a/samples/client/petstore/python-tornado/petstore_api/models/animal.py b/samples/client/petstore/python-tornado/petstore_api/models/animal.py index 4a0768026542..5acccf50f7ab 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/animal.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/animal.py @@ -102,7 +102,7 @@ def color(self, color): def get_real_child_model(self, data): """Returns the real base class specified by the discriminator""" - discriminator_value = data[self.discriminator].lower() + discriminator_value = data[self.discriminator] return self.discriminator_value_class_map.get(discriminator_value) def to_dict(self): diff --git a/samples/client/petstore/python-tornado/petstore_api/models/animal_farm.py b/samples/client/petstore/python-tornado/petstore_api/models/animal_farm.py index 238d580ba528..4894e5530a33 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/animal_farm.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/animal_farm.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class AnimalFarm(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/array_test.py b/samples/client/petstore/python-tornado/petstore_api/models/array_test.py index 04a871e8f74d..05cc108139a5 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/array_test.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/array_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501 - class ArrayTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/cat.py b/samples/client/petstore/python-tornado/petstore_api/models/cat.py index 808409c22669..c5c87a6b1118 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/cat.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/cat.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Cat(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/dog.py b/samples/client/petstore/python-tornado/petstore_api/models/dog.py index f7fa73c9d4c8..c2bc8f745d15 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/dog.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/dog.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Dog(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/enum_test.py b/samples/client/petstore/python-tornado/petstore_api/models/enum_test.py index cfe0ebcb84a5..0fd60c4a351d 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/enum_test.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/enum_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501 - class EnumTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/mixed_properties_and_additional_properties_class.py b/samples/client/petstore/python-tornado/petstore_api/models/mixed_properties_and_additional_properties_class.py index a38141493f84..ecf67acc5e95 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/mixed_properties_and_additional_properties_class.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class MixedPropertiesAndAdditionalPropertiesClass(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python-tornado/petstore_api/models/pet.py b/samples/client/petstore/python-tornado/petstore_api/models/pet.py index 27891437778c..d3c412f4a82b 100644 --- a/samples/client/petstore/python-tornado/petstore_api/models/pet.py +++ b/samples/client/petstore/python-tornado/petstore_api/models/pet.py @@ -15,9 +15,6 @@ import six -from petstore_api.models.category import Category # noqa: F401,E501 -from petstore_api.models.tag import Tag # noqa: F401,E501 - class Pet(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/docs/FakeApi.md b/samples/client/petstore/python/docs/FakeApi.md index 586616d73603..3b30ab1b4e9c 100644 --- a/samples/client/petstore/python/docs/FakeApi.md +++ b/samples/client/petstore/python/docs/FakeApi.md @@ -419,7 +419,7 @@ Name | Type | Description | Notes **enum_query_string** | **str**| Query parameter enum test (string) | [optional] [default to '-efg'] **enum_query_integer** | **int**| Query parameter enum test (double) | [optional] **enum_query_double** | **float**| Query parameter enum test (double) | [optional] - **enum_form_string_array** | **list[str]**| Form parameter enum test (string array) | [optional] [default to '$'] + **enum_form_string_array** | [**list[str]**](str.md)| Form parameter enum test (string array) | [optional] [default to '$'] **enum_form_string** | **str**| Form parameter enum test (string) | [optional] [default to '-efg'] ### Return type diff --git a/samples/client/petstore/python/petstore_api/models/animal.py b/samples/client/petstore/python/petstore_api/models/animal.py index 4a0768026542..5acccf50f7ab 100644 --- a/samples/client/petstore/python/petstore_api/models/animal.py +++ b/samples/client/petstore/python/petstore_api/models/animal.py @@ -102,7 +102,7 @@ def color(self, color): def get_real_child_model(self, data): """Returns the real base class specified by the discriminator""" - discriminator_value = data[self.discriminator].lower() + discriminator_value = data[self.discriminator] return self.discriminator_value_class_map.get(discriminator_value) def to_dict(self): diff --git a/samples/client/petstore/python/petstore_api/models/animal_farm.py b/samples/client/petstore/python/petstore_api/models/animal_farm.py index 238d580ba528..4894e5530a33 100644 --- a/samples/client/petstore/python/petstore_api/models/animal_farm.py +++ b/samples/client/petstore/python/petstore_api/models/animal_farm.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class AnimalFarm(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/array_test.py b/samples/client/petstore/python/petstore_api/models/array_test.py index 04a871e8f74d..05cc108139a5 100644 --- a/samples/client/petstore/python/petstore_api/models/array_test.py +++ b/samples/client/petstore/python/petstore_api/models/array_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501 - class ArrayTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/cat.py b/samples/client/petstore/python/petstore_api/models/cat.py index 808409c22669..c5c87a6b1118 100644 --- a/samples/client/petstore/python/petstore_api/models/cat.py +++ b/samples/client/petstore/python/petstore_api/models/cat.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Cat(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/dog.py b/samples/client/petstore/python/petstore_api/models/dog.py index f7fa73c9d4c8..c2bc8f745d15 100644 --- a/samples/client/petstore/python/petstore_api/models/dog.py +++ b/samples/client/petstore/python/petstore_api/models/dog.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class Dog(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/enum_test.py b/samples/client/petstore/python/petstore_api/models/enum_test.py index cfe0ebcb84a5..0fd60c4a351d 100644 --- a/samples/client/petstore/python/petstore_api/models/enum_test.py +++ b/samples/client/petstore/python/petstore_api/models/enum_test.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501 - class EnumTest(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/mixed_properties_and_additional_properties_class.py b/samples/client/petstore/python/petstore_api/models/mixed_properties_and_additional_properties_class.py index a38141493f84..ecf67acc5e95 100644 --- a/samples/client/petstore/python/petstore_api/models/mixed_properties_and_additional_properties_class.py +++ b/samples/client/petstore/python/petstore_api/models/mixed_properties_and_additional_properties_class.py @@ -15,8 +15,6 @@ import six -from petstore_api.models.animal import Animal # noqa: F401,E501 - class MixedPropertiesAndAdditionalPropertiesClass(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/petstore_api/models/pet.py b/samples/client/petstore/python/petstore_api/models/pet.py index 27891437778c..d3c412f4a82b 100644 --- a/samples/client/petstore/python/petstore_api/models/pet.py +++ b/samples/client/petstore/python/petstore_api/models/pet.py @@ -15,9 +15,6 @@ import six -from petstore_api.models.category import Category # noqa: F401,E501 -from petstore_api.models.tag import Tag # noqa: F401,E501 - class Pet(object): """NOTE: This class is auto generated by OpenAPI Generator. diff --git a/samples/client/petstore/python/tests/test_deserialization.py b/samples/client/petstore/python/tests/test_deserialization.py index 5c1540d99450..329e4fc5508e 100644 --- a/samples/client/petstore/python/tests/test_deserialization.py +++ b/samples/client/petstore/python/tests/test_deserialization.py @@ -71,6 +71,21 @@ def test_deserialize_dict_str_pet(self): self.assertTrue(isinstance(deserialized, dict)) self.assertTrue(isinstance(deserialized['pet'], petstore_api.Pet)) + def test_deserialize_dict_str_dog(self): + """ deserialize dict(str, Dog), use discriminator""" + data = { + 'dog': { + "id": 0, + "className": "Dog", + "color": "white", + "bread": "Jack Russel Terrier" + } + } + + deserialized = self.deserialize(data, 'dict(str, Animal)') + self.assertTrue(isinstance(deserialized, dict)) + self.assertTrue(isinstance(deserialized['dog'], petstore_api.Dog)) + def test_deserialize_dict_str_int(self): """ deserialize dict(str, int) """ data = {