Build a response by making a request or using the cache. This will end up calling send and returning a potentially cached response
( # type: ignore[override]
self,
request: PreparedRequest,
response: HTTPResponse,
from_cache: bool = False,
cacheable_methods: Collection[str] | None = None,
)
| 78 | return resp |
| 79 | |
| 80 | def build_response( # type: ignore[override] |
| 81 | self, |
| 82 | request: PreparedRequest, |
| 83 | response: HTTPResponse, |
| 84 | from_cache: bool = False, |
| 85 | cacheable_methods: Collection[str] | None = None, |
| 86 | ) -> Response: |
| 87 | """ |
| 88 | Build a response by making a request or using the cache. |
| 89 | |
| 90 | This will end up calling send and returning a potentially |
| 91 | cached response |
| 92 | """ |
| 93 | cacheable = cacheable_methods or self.cacheable_methods |
| 94 | if not from_cache and request.method in cacheable: |
| 95 | # Check for any heuristics that might update headers |
| 96 | # before trying to cache. |
| 97 | if self.heuristic: |
| 98 | response = self.heuristic.apply(response) |
| 99 | |
| 100 | # apply any expiration heuristics |
| 101 | if response.status == 304: |
| 102 | # We must have sent an ETag request. This could mean |
| 103 | # that we've been expired already or that we simply |
| 104 | # have an etag. In either case, we want to try and |
| 105 | # update the cache if that is the case. |
| 106 | cached_response = self.controller.update_cached_response( |
| 107 | request, response |
| 108 | ) |
| 109 | |
| 110 | if cached_response is not response: |
| 111 | from_cache = True |
| 112 | |
| 113 | # We are done with the server response, read a |
| 114 | # possible response body (compliant servers will |
| 115 | # not return one, but we cannot be 100% sure) and |
| 116 | # release the connection back to the pool. |
| 117 | response.read(decode_content=False) |
| 118 | response.release_conn() |
| 119 | |
| 120 | response = cached_response |
| 121 | |
| 122 | # We always cache the 301 responses |
| 123 | elif int(response.status) in PERMANENT_REDIRECT_STATUSES: |
| 124 | self.controller.cache_response(request, response) |
| 125 | else: |
| 126 | # Wrap the response file with a wrapper that will cache the |
| 127 | # response when the stream has been consumed. |
| 128 | response._fp = CallbackFileWrapper( # type: ignore[assignment] |
| 129 | response._fp, # type: ignore[arg-type] |
| 130 | functools.partial( |
| 131 | self.controller.cache_response, request, weakref.ref(response) |
| 132 | ), |
| 133 | ) |
| 134 | if response.chunked: |
| 135 | super_update_chunk_length = response.__class__._update_chunk_length |
| 136 | |
| 137 | def _update_chunk_length( |
no test coverage detected