From 7be96f4340e674770c93c1f2747035f51beeaa73 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Sun, 22 Oct 2017 21:17:54 -0700 Subject: [PATCH] Allow to pass an instance of ClientSession as an argument. Refs #22 --- CHANGES | 5 +++++ raven_aiohttp.py | 18 +++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGES b/CHANGES index 06ca84b..9e51301 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +X.X.X +----- + +- Allow to pass an instance of ClientSession as an argument + 0.6.0 ----- diff --git a/raven_aiohttp.py b/raven_aiohttp.py index 06ad36c..5d4e066 100644 --- a/raven_aiohttp.py +++ b/raven_aiohttp.py @@ -36,7 +36,7 @@ class AioHttpTransportBase( def __init__(self, parsed_url=None, *, verify_ssl=True, resolve=True, timeout=defaults.TIMEOUT, - keepalive=True, family=socket.AF_INET, loop=None): + keepalive=True, family=socket.AF_INET, client_session=None, loop=None): self._resolve = resolve self._keepalive = keepalive self._family = family @@ -53,7 +53,9 @@ def __init__(self, parsed_url=None, *, verify_ssl=True, resolve=True, else: super().__init__(parsed_url, timeout, verify_ssl) - if self.keepalive: + if client_session: + self._client_session = client_session + else: self._client_session = self._client_session_factory() self._closing = False @@ -80,15 +82,10 @@ def _client_session_factory(self): @asyncio.coroutine def _do_send(self, url, data, headers, success_cb, failure_cb): - if self.keepalive: - session = self._client_session - else: - session = self._client_session_factory() - resp = None try: - resp = yield from session.post( + resp = yield from self._client_session.post( url, data=data, compress=False, @@ -118,8 +115,6 @@ def _do_send(self, url, data, headers, success_cb, failure_cb): finally: if resp is not None: resp.release() - if not self.keepalive: - yield from session.close() @abc.abstractmethod def _async_send(self, url, data, headers, success_cb, failure_cb): # pragma: no cover @@ -146,8 +141,9 @@ def _close_coro(self, *, timeout=None): except asyncio.TimeoutError: pass finally: - if self.keepalive: + if self._client_session: yield from self._client_session.close() + self._client_session = None def close(self, *, timeout=None): if self._closing: