This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
raven_aiohttp.py
65 lines (57 loc) · 2.31 KB
/
raven_aiohttp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
raven_aiohttp
~~~~~~~~~~~~~
:copyright: (c) 2010-2015 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from raven.exceptions import APIError, RateLimited
from raven.transport.base import AsyncTransport
from raven.transport.http import HTTPTransport
from raven.conf import defaults
import aiohttp
import asyncio
import socket
class AioHttpTransport(AsyncTransport, HTTPTransport):
def __init__(self, parsed_url, *, verify_ssl=True, resolve=True,
timeout=defaults.TIMEOUT,
keepalive=True, family=socket.AF_INET, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
super().__init__(parsed_url, timeout, verify_ssl)
if keepalive:
self._connector = aiohttp.TCPConnector(verify_ssl=verify_ssl,
resolve=resolve,
family=family,
loop=loop)
else:
self._connector = None
def async_send(self, data, headers, success_cb, failure_cb):
@asyncio.coroutine
def f():
try:
resp = yield from asyncio.wait_for(
aiohttp.request('POST',
self._url, data=data,
headers=headers,
connector=self._connector,
loop=self._loop),
self.timeout,
loop=self._loop)
yield from resp.release()
code = resp.status
if code != 200:
msg = resp.headers.get('x-sentry-error')
if code == 429:
try:
retry_after = int(resp.headers.get('retry-after'))
except (ValueError, TypeError):
retry_after = 0
failure_cb(RateLimited(msg, retry_after))
else:
failure_cb(APIError(msg, code))
else:
success_cb()
except Exception as exc:
failure_cb(exc)
asyncio.async(f(), loop=self._loop)