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/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