Skip to content

Commit 04457f3

Browse files
ADR-007jairhenrique
authored andcommitted
Improve performance by caching urlparse
1 parent 965f365 commit 04457f3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

vcr/request.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Request:
1616
def __init__(self, method, uri, body, headers):
1717
self.method = method
1818
self.uri = uri
19+
self.parsed_uri = urlparse(self.uri)
1920
self._was_file = hasattr(body, "read")
2021
self._was_iter = _is_nonsequence_iterator(body)
2122
if self._was_file:
@@ -61,30 +62,29 @@ def add_header(self, key, value):
6162

6263
@property
6364
def scheme(self):
64-
return urlparse(self.uri).scheme
65+
return self.parsed_uri.scheme
6566

6667
@property
6768
def host(self):
68-
return urlparse(self.uri).hostname
69+
return self.parsed_uri.hostname
6970

7071
@property
7172
def port(self):
72-
parse_uri = urlparse(self.uri)
73-
port = parse_uri.port
73+
port = self.parsed_uri.port
7474
if port is None:
7575
try:
76-
port = {"https": 443, "http": 80}[parse_uri.scheme]
76+
port = {"https": 443, "http": 80}[self.parsed_uri.scheme]
7777
except KeyError:
7878
pass
7979
return port
8080

8181
@property
8282
def path(self):
83-
return urlparse(self.uri).path
83+
return self.parsed_uri.path
8484

8585
@property
8686
def query(self):
87-
q = urlparse(self.uri).query
87+
q = self.parsed_uri.query
8888
return sorted(parse_qsl(q))
8989

9090
# alias for backwards compatibility

0 commit comments

Comments
 (0)