diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-15 18:10:28 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-15 18:20:47 -0500 |
commit | f72454497ce73f99aef2bbdeeb5b1711452b12fe (patch) | |
tree | 99ef12d49273be688beb0f0c6acebb80f83f0995 /requests_cache/models | |
parent | 3f6d48707e26e103dfb0029ecb33c520ed21bf1b (diff) | |
download | requests-cache-f72454497ce73f99aef2bbdeeb5b1711452b12fe.tar.gz |
Fix structuring/unstructuring CachedResponse.history
Diffstat (limited to 'requests_cache/models')
-rwxr-xr-x | requests_cache/models/response.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/requests_cache/models/response.py b/requests_cache/models/response.py index b6ba3ae..e85af1a 100755 --- a/requests_cache/models/response.py +++ b/requests_cache/models/response.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from datetime import datetime, timedelta, timezone from logging import getLogger from typing import TYPE_CHECKING, List, Optional, Tuple @@ -86,7 +88,10 @@ class CachedResponse(BaseResponse): def from_response(cls, response: Response, **kwargs): """Create a CachedResponse based on an original Response or another CachedResponse object""" if isinstance(response, CachedResponse): - return attr.evolve(response, **kwargs) + obj = attr.evolve(response, **kwargs) + obj._convert_redirects() + return obj + obj = cls(**kwargs) # Copy basic attributes @@ -101,14 +106,16 @@ class CachedResponse(BaseResponse): # Store response body, which will have been read & decoded by requests.Response by now obj._content = response.content - # Copy redirect history, if any; avoid recursion by not copying redirects of redirects - obj.history = [] - if not obj.is_redirect: - for redirect in response.history: - obj.history.append(cls.from_response(redirect)) - + obj._convert_redirects() return obj + def _convert_redirects(self): + """Convert redirect history, if any; avoid recursion by not copying redirects of redirects""" + if self.is_redirect: + self.history = [] + return + self.history = [self.from_response(redirect) for redirect in self.history] + @property def _content_consumed(self) -> bool: """For compatibility with requests.Response; will always be True for a cached response""" |