1"""
2raven.transport.eventlet
3~~~~~~~~~~~~~~~~~~~~~~~~
4
5:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
6:license: BSD, see LICENSE for more details.
7"""
8from __future__ import absolute_import
9
10import sys
11
12from raven.transport.http import HTTPTransport
13
14try:
15    import eventlet
16    try:
17        from eventlet.green import urllib2 as eventlet_urllib2
18    except ImportError:
19        from eventlet.green.urllib import request as eventlet_urllib2
20    has_eventlet = True
21except ImportError:
22    has_eventlet = False
23
24
25class EventletHTTPTransport(HTTPTransport):
26
27    scheme = ['eventlet+http', 'eventlet+https']
28
29    def __init__(self, pool_size=100, **kwargs):
30        if not has_eventlet:
31            raise ImportError('EventletHTTPTransport requires eventlet.')
32        super(EventletHTTPTransport, self).__init__(**kwargs)
33
34    def _send_payload(self, payload):
35        url, data, headers = payload
36        req = eventlet_urllib2.Request(url, headers=headers)
37        try:
38            if sys.version_info < (2, 6):
39                response = eventlet_urllib2.urlopen(req, data).read()
40            else:
41                response = eventlet_urllib2.urlopen(req, data,
42                                                    self.timeout).read()
43            return response
44        except Exception as err:
45            return err
46
47    def send(self, url, data, headers):
48        """
49        Spawn an async request to a remote webserver.
50        """
51        eventlet.spawn(self._send_payload, (url, data, headers))
52