Algorithm for caching requests. This assumes a requests Response object.
(
self,
request: PreparedRequest,
response_or_ref: HTTPResponse | weakref.ReferenceType[HTTPResponse],
body: bytes | None = None,
status_codes: Collection[int] | None = None,
)
| 322 | ) |
| 323 | |
| 324 | def cache_response( |
| 325 | self, |
| 326 | request: PreparedRequest, |
| 327 | response_or_ref: HTTPResponse | weakref.ReferenceType[HTTPResponse], |
| 328 | body: bytes | None = None, |
| 329 | status_codes: Collection[int] | None = None, |
| 330 | ) -> None: |
| 331 | """ |
| 332 | Algorithm for caching requests. |
| 333 | |
| 334 | This assumes a requests Response object. |
| 335 | """ |
| 336 | if isinstance(response_or_ref, weakref.ReferenceType): |
| 337 | response = response_or_ref() |
| 338 | if response is None: |
| 339 | # The weakref can be None only in case the user used streamed request |
| 340 | # and did not consume or close it, and holds no reference to requests.Response. |
| 341 | # In such case, we don't want to cache the response. |
| 342 | return |
| 343 | else: |
| 344 | response = response_or_ref |
| 345 | |
| 346 | # From httplib2: Don't cache 206's since we aren't going to |
| 347 | # handle byte range requests |
| 348 | cacheable_status_codes = status_codes or self.cacheable_status_codes |
| 349 | if response.status not in cacheable_status_codes: |
| 350 | logger.debug( |
| 351 | "Status code %s not in %s", response.status, cacheable_status_codes |
| 352 | ) |
| 353 | return |
| 354 | |
| 355 | response_headers: CaseInsensitiveDict[str] = CaseInsensitiveDict( |
| 356 | response.headers |
| 357 | ) |
| 358 | |
| 359 | if "date" in response_headers: |
| 360 | time_tuple = parsedate_tz(response_headers["date"]) |
| 361 | assert time_tuple is not None |
| 362 | date = calendar.timegm(time_tuple[:6]) |
| 363 | else: |
| 364 | date = 0 |
| 365 | |
| 366 | # If we've been given a body, our response has a Content-Length, that |
| 367 | # Content-Length is valid then we can check to see if the body we've |
| 368 | # been given matches the expected size, and if it doesn't we'll just |
| 369 | # skip trying to cache it. |
| 370 | if ( |
| 371 | body is not None |
| 372 | and "content-length" in response_headers |
| 373 | and response_headers["content-length"].isdigit() |
| 374 | and int(response_headers["content-length"]) != len(body) |
| 375 | ): |
| 376 | return |
| 377 | |
| 378 | cc_req = self.parse_cache_control(request.headers) |
| 379 | cc = self.parse_cache_control(response_headers) |
| 380 | |
| 381 | assert request.url is not None |