10000 Revert "Revert "feat: Add support for DSN paths"" · etherscan-io/sentry-python@ab5069a · GitHub
[go: up one dir, main page]

Skip to content

Commit ab5069a

Browse files
committed
Revert "Revert "feat: Add support for DSN paths""
This reverts commit d44a02f.
1 parent d44a02f commit ab5069a

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

sentry_sdk/utils.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ def __init__(self, value):
8484
self.port = self.scheme == "https" and 443 or 80
8585
self.public_key = parts.username
8686
if not self.public_key:
87-
raise BadDsn("Missig public key")
87+
raise BadDsn("Missing public key")
8888
self.secret_key = parts.password
89-
if not parts.path:
90-
raise BadDsn("Missing project ID in DSN")
89+
90+
path = parts.path.rsplit("/", 1)
91+
9192
try:
92-
self.project_id = text_type(int(parts.path[1:]))
93+
self.project_id = text_type(int(path.pop()))
9394
except (ValueError, TypeError):
9495
raise BadDsn("Invalid project in DSN (%r)" % (parts.path or "")[1:])
9596

97+
self.path = "/".join(path) + "/"
98+
9699
@property
97100
def netloc(self):
98101
"""The netloc part of a DSN."""
@@ -106,18 +109,20 @@ def to_auth(self, client=None):
106109
return Auth(
107110
scheme=self.scheme,
108111
host=self.netloc,
112+
path=self.path,
109113
project_id=self.project_id,
110114
public_key=self.public_key,
111115
secret_key=self.secret_key,
112116
client=client,
113117
)
114118

115119
def __str__(self):
116-
return "%s://%s%s@%s/%s" % (
120+
return "%s://%s%s@%s%s%s" % (
117121
self.scheme,
118122
self.public_key,
119123
self.secret_key and "@" + self.secret_key or "",
120124
self.netloc,
125+
self.path,
121126
self.project_id,
122127
)
123128

@@ -129,6 +134,7 @@ def __init__(
129134
self,
130135
scheme,
131136
host,
137+
path,
132138
project_id,
133139
public_key,
134140
secret_key=None,
@@ -137,6 +143,7 @@ def __init__(
137143
):
138144
self.scheme = scheme
139145
self.host = host
146+
self.path = path
140147
self.project_id = project_id
141148
self.public_key = public_key
142149
self.secret_key = secret_key
@@ -146,7 +153,12 @@ def __init__(
146153
@property
147154
def store_api_url(self):
148155
"""Returns the API url for storing events."""
149-
return "%s://%s/api/%s/store/" % (self.scheme, self.host, self.project_id)
156+
return "%s://%s%sapi/%s/store/" % (
157+
self.scheme,
158+
self.host,
159+
self.path,
160+
self.project_id,
161+
)
150162

151163
def to_header(self, timestamp=None):
152164
"""Returns the auth header a string."""

tests/utils/test_general.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import hypothesis.strategies as st
99

1010
from sentry_sdk.utils import (
11+
BadDsn,
12+
Dsn,
1113
safe_repr,
1214
exceptions_from_error_tuple,
1315
format_and_strip,
@@ -111,3 +113,35 @@ def test_filename():
111113
import sentry_sdk.utils
112114

113115
assert x("sentry_sdk.utils", sentry_sdk.utils.__file__) == "sentry_sdk/utils.py"
116+
117+
118+
@pytest.mark.parametrize(
119+
"given,expected",
120+
[
121+
("https://foobar@sentry.io/123", "https://sentry.io/api/123/store/"),
122+
("https://foobar@sentry.io/bam/123", "https://sentry.io/bam/api/123/store/"),
123+
(
124+
"https://foobar@sentry.io/bam/baz/123",
125+
"https://sentry.io/bam/baz/api/123/store/",
126+
),
127+
],
128+
)
129+
def test_parse_dsn_paths(given, expected):
130+
dsn = Dsn(given)
131+
auth = dsn.to_auth()
132+
assert auth.store_api_url == expected
133+
134+
135+
@pytest.mark.parametrize(
136+
"dsn",
137+
[
138+
"https://foobar@sentry.io"
139+
"https://f 5FEE oobar@sentry.io/"
140+
"https://foobar@sentry.io/asdf"
141+
"https://foobar@sentry.io/asdf/"
142+
"https://foobar@sentry.io/asdf/123/"
143+
],
144+
)
145+
def test_parse_invalid_dsn(dsn):
146+
with pytest.raises(BadDsn):
147+
dsn = Dsn(dsn)

0 commit comments

Comments
 (0)
0