Skip to content

Commit

Permalink
feat: python/asyncio support for _preload_content
Browse files Browse the repository at this point in the history
  • Loading branch information
tomplus committed May 18, 2018
1 parent 7f72d12 commit c0f85df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions samples/client/petstore/python-asyncio/petstore_api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import aiohttp
import certifi
import asyncio
# python 2 and python 3 compatibility library
from six.moves.urllib.parse import urlencode

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c0f85df

Please sign in to comment.