|
14 | 14 |
|
15 | 15 | from ._compat import queue |
16 | 16 | from .consts import VERSION |
| 17 | +from .utils import Dsn |
17 | 18 |
|
18 | 19 | try: |
19 | 20 | from urllib.request import getproxies |
|
24 | 25 | logger = logging.getLogger(__name__) |
25 | 26 |
|
26 | 27 |
|
27 | | -def _make_pool(dsn, http_proxy, https_proxy): |
28 | | - proxy = https_proxy if dsn == "https" else http_proxy |
| 28 | +def _make_pool(parsed_dsn, http_proxy, https_proxy): |
| 29 | + proxy = https_proxy if parsed_dsn == "https" else http_proxy |
29 | 30 | if not proxy: |
30 | | - proxy = getproxies().get(dsn.scheme) |
| 31 | + proxy = getproxies().get(parsed_dsn.scheme) |
31 | 32 |
|
32 | 33 | opts = {"num_pools": 2, "cert_reqs": "CERT_REQUIRED", "ca_certs": certifi.where()} |
33 | 34 |
|
@@ -70,7 +71,7 @@ def send_event(pool, event, auth): |
70 | 71 |
|
71 | 72 |
|
72 | 73 | def spawn_thread(transport): |
73 | | - auth = transport.dsn.to_auth("sentry-python/%s" % VERSION) |
| 74 | + auth = transport.parsed_dsn.to_auth("sentry-python/%s" % VERSION) |
74 | 75 |
|
75 | 76 | def thread(): |
76 | 77 | disabled_until = None |
@@ -104,10 +105,36 @@ def thread(): |
104 | 105 |
|
105 | 106 |
|
106 | 107 | class Transport(object): |
107 | | - def __init__(self, dsn, http_proxy=None, https_proxy=None): |
108 | | - self.dsn = dsn |
| 108 | + def __init__(self, options=None): |
| 109 | + self.options = options |
| 110 | + if options and options["dsn"]: |
| 111 | + self.parsed_dsn = Dsn(options["dsn"]) |
| 112 | + else: |
| 113 | + self.parsed_dsn = None |
| 114 | + |
| 115 | + def capture_event(self, event): |
| 116 | + raise NotImplementedError() |
| 117 | + |
| 118 | + def close(self): |
| 119 | + pass |
| 120 | + |
| 121 | + def drain_events(self, timeout): |
| 122 | + pass |
| 123 | + |
| 124 | + def __del__(self): |
| 125 | + self.close() |
| 126 | + |
| 127 | + |
| 128 | +class HttpTransport(Transport): |
| 129 | + def __init__(self, options): |
| 130 | + Transport.__init__(self, options) |
109 | 131 | self._queue = None |
110 | | - self._pool = _make_pool(dsn, http_proxy=http_proxy, https_proxy=https_proxy) |
| 132 | + self._pool = _make_pool( |
| 133 | + self.parsed_dsn, |
| 134 | + http_proxy=options["http_proxy"], |
| 135 | + https_proxy=options["https_proxy"], |
| 136 | + ) |
| 137 | + self.start() |
111 | 138 |
|
112 | 139 | def start(self): |
113 | 140 | if self._queue is None: |
@@ -139,3 +166,36 @@ def drain_events(self, timeout): |
139 | 166 |
|
140 | 167 | def __del__(self): |
141 | 168 | self.close() |
| 169 | + |
| 170 | + |
| 171 | +class _FunctionTransport(Transport): |
| 172 | + def __init__(self, func): |
| 173 | + Transport.__init__(self) |
| 174 | + self._func = func |
| 175 | + |
| 176 | + def capture_event(self, event): |
| 177 | + self._func(event) |
| 178 | + |
| 179 | + |
| 180 | +def make_transport(options): |
| 181 | + ref_transport = options["transport"] |
| 182 | + |
| 183 | + # If no transport is given, we use the http transport class |
| 184 | + if ref_transport is None: |
| 185 | + transport_cls = HttpTransport |
| 186 | + else: |
| 187 | + try: |
| 188 | + issubclass(ref_transport, type) |
| 189 | + except TypeError: |
| 190 | + # if we are not a class but we are a callable, assume a |
| 191 | + # function that acts as capture_event |
| 192 | + if callable(ref_transport): |
| 193 | + return _FunctionTransport(ref_transport) |
| 194 | + # otherwise assume an object fulfilling the transport contract |
| 195 | + return ref_transport |
| 196 | + transport_cls = ref_transport |
| 197 | + |
| 198 | + # if a transport class is given only instanciate it if the dsn is not |
| 199 | + # empty or None |
| 200 | + if options["dsn"]: |
| 201 | + return transport_cls(options) |
0 commit comments