This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 657
/
Copy pathremote.py
123 lines (92 loc) · 4.02 KB
/
remote.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import absolute_import
import logging
import os
import warnings
from raven.utils.compat import PY2, text_type
from raven.exceptions import InvalidDsn
from raven.utils.encoding import to_string
from raven.utils.urlparse import parse_qsl, urlparse
ERR_UNKNOWN_SCHEME = 'Unsupported Sentry DSN scheme: {0} ({1})'
logger = logging.getLogger('raven')
def discover_default_transport():
from raven.transport.threaded import ThreadedHTTPTransport
from raven.transport.http import HTTPTransport
# Google App Engine
# https://cloud.google.com/appengine/docs/python/how-requests-are-handled#Python_The_environment
if 'CURRENT_VERSION_ID' in os.environ and 'INSTANCE_ID' in os.environ:
logger.info('Detected environment to be Google App Engine. Using synchronous HTTP transport.')
return HTTPTransport
# AWS Lambda
# https://alestic.com/2014/11/aws-lambda-environment/
if 'LAMBDA_TASK_ROOT' in os.environ:
logger.info('Detected environment to be AWS Lambda. Using synchronous HTTP transport.')
return HTTPTransport
return ThreadedHTTPTransport
DEFAULT_TRANSPORT = discover_default_transport()
class RemoteConfig(object):
def __init__(self, base_url=None, project=None, public_key=None,
secret_key=None, transport=None, options=None):
if base_url:
base_url = base_url.rstrip('/')
store_endpoint = '%s/api/%s/store/' % (base_url, project)
else:
store_endpoint = None
self.base_url = base_url
self.project = project
self.public_key = public_key
self.secret_key = secret_key
self.options = options or {}
self.store_endpoint = store_endpoint
self._transport_cls = transport or DEFAULT_TRANSPORT
def __unicode__(self):
return text_type(self.base_url)
def is_active(self):
return all([self.base_url, self.project, self.public_key, self.secret_key])
def get_transport(self):
if not self.store_endpoint:
return
if not hasattr(self, '_transport'):
self._transport = self._transport_cls(**self.options)
return self._transport
def get_public_dsn(self):
url = urlparse(self.base_url)
netloc = url.hostname
if url.port:
netloc += ':%s' % url.port
return '//%s@%s%s/%s' % (self.public_key, netloc, url.path, self.project)
@classmethod
def from_string(cls, value, transport=None, transport_registry=None):
# in Python 2.x sending the DSN as a unicode value will eventually
# cause issues in httplib
if PY2:
value = to_string(value)
url = urlparse(value.strip())
if url.scheme not in ('http', 'https'):
warnings.warn('Transport selection via DSN is deprecated. You should explicitly pass the transport class to Client() instead.')
if transport is None:
if not transport_registry:
from raven.transport import TransportRegistry, default_transports
transport_registry = TransportRegistry(default_transports)
if not transport_registry.supported_scheme(url.scheme):
raise InvalidDsn(ERR_UNKNOWN_SCHEME.format(url.scheme, value))
transport = transport_registry.get_transport_cls(url.scheme)
netloc = url.hostname
if url.port:
netloc += ':%s' % url.port
path_bits = url.path.rsplit('/', 1)
if len(path_bits) > 1:
path = path_bits[0]
else:
path = ''
project = path_bits[-1]
if not all([netloc, project, url.username, url.password]):
raise InvalidDsn('Invalid Sentry DSN: %r' % url.geturl())
base_url = '%s://%s%s' % (url.scheme.rsplit('+', 1)[-1], netloc, path)
return cls(
base_url=base_url,
project=project,
public_key=url.username,
secret_key=url.password,
options=dict(parse_qsl(url.query)),
transport=transport,
)