Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(0.9) Add CachedRequest.path_url property #638

Merged
merged 1 commit into from May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -2,6 +2,7 @@

## 0.9.5 (Unreleased)
* Fix usage of memory backend with `install_cache()`
* Add `CachedRequest.path_url` property
* Add compatibility with cattrs 22.1

## 0.9.4 (2022-04-22)
Expand Down
8 changes: 8 additions & 0 deletions requests_cache/models/request.py
@@ -1,4 +1,5 @@
from logging import getLogger
from urllib.parse import urlsplit

from attr import asdict, define, field, fields_dict
from requests import PreparedRequest
Expand Down Expand Up @@ -27,6 +28,13 @@ def from_request(cls, original_request: PreparedRequest) -> 'CachedRequest':
kwargs['cookies'] = getattr(original_request, '_cookies', None)
return cls(**kwargs) # type: ignore # False positive in mypy 0.920+?

@property
def path_url(self):
p = urlsplit(self.url)
url = p.path or '/'
url += f'?{p.query}' if p.query else ''
return url

def copy(self) -> 'CachedRequest':
"""Return a copy of the CachedRequest"""
return self.__class__(**asdict(self))
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/test_request.py
Expand Up @@ -10,7 +10,7 @@ def test_from_request(mock_session):
expected_headers = {**default_headers(), 'Content-Length': '12', 'foo': 'bar'}

assert response.request.body == request.body == b'mock request'
assert response.request._cookies == request.cookies == {}
assert response.request.headers == request.headers == expected_headers
assert response.request.method == request.method == 'GET'
assert response.request.path_url == request.path_url == '/text'
assert response.request.url == request.url == MOCKED_URL