Skip to content

Commit

Permalink
Improve performance by caching urlparse
Browse files Browse the repository at this point in the history
  • Loading branch information
ADR-007 authored and jairhenrique committed Jan 4, 2025
1 parent 965f365 commit 04457f3
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions vcr/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Request:
def __init__(self, method, uri, body, headers):
self.method = method
self.uri = uri
self.parsed_uri = urlparse(self.uri)
self._was_file = hasattr(body, "read")
self._was_iter = _is_nonsequence_iterator(body)
if self._was_file:
Expand Down Expand Up @@ -61,30 +62,29 @@ def add_header(self, key, value):

@property
def scheme(self):
return urlparse(self.uri).scheme
return self.parsed_uri.scheme

@property
def host(self):
return urlparse(self.uri).hostname
return self.parsed_uri.hostname

@property
def port(self):
parse_uri = urlparse(self.uri)
port = parse_uri.port
port = self.parsed_uri.port
if port is None:
try:
port = {"https": 443, "http": 80}[parse_uri.scheme]
port = {"https": 443, "http": 80}[self.parsed_uri.scheme]
except KeyError:
pass
return port

@property
def path(self):
return urlparse(self.uri).path
return self.parsed_uri.path

@property
def query(self):
q = urlparse(self.uri).query
q = self.parsed_uri.query
return sorted(parse_qsl(q))

# alias for backwards compatibility
Expand Down

0 comments on commit 04457f3

Please sign in to comment.