How to use the cachecontrol.filewrapper.CallbackFileWrapper function in CacheControl

To help you get started, we’ve selected a few CacheControl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ionrock / cachecontrol / tests / test_regressions.py View on Github external
def test_getattr_during_gc():
    s = CallbackFileWrapper(None, None)
    # normal behavior:
    with pytest.raises(AttributeError):
        s.x

    # this previously had caused an infinite recursion
    vars(s).clear()  # gc does this.
    with pytest.raises(AttributeError):
        s.x
github ionrock / cachecontrol / cachecontrol / adapter.py View on Github external
# We are done with the server response, read a
                # possible response body (compliant servers will
                # not return one, but we cannot be 100% sure) and
                # release the connection back to the pool.
                response.read(decode_content=False)
                response.release_conn()

                response = cached_response

            # We always cache the 301 responses
            elif response.status == 301:
                self.controller.cache_response(request, response)
            else:
                # Wrap the response file with a wrapper that will cache the
                #   response when the stream has been consumed.
                response._fp = CallbackFileWrapper(
                    response._fp,
                    functools.partial(
                        self.controller.cache_response, request, response
                    ),
                )
                if response.chunked:
                    super_update_chunk_length = response._update_chunk_length

                    def _update_chunk_length(self):
                        super_update_chunk_length()
                        if self.chunk_left == 0:
                            self._fp._close()

                    response._update_chunk_length = types.MethodType(
                        _update_chunk_length, response
                    )
github jmcarp / requests-middleware / requests_middleware / contrib / cachecontrolware.py View on Github external
response if appropriate and mark with `from_cache`.
        """
        from_cache = getattr(response, 'from_cache', False)

        if request.method == 'GET' and not from_cache:
            if response.status == 304:
                cached_response = self.controller.update_cached_response(
                    request, response
                )
                if cached_response is not response:
                    from_cache = True
                response = cached_response
            else:
                if self.heuristic:
                    response = self.heuristic.apply(response)
                response._fp = CallbackFileWrapper(
                    response._fp,
                    functools.partial(
                        self.controller.cache_response,
                        request,
                        response,
                    )
                )

        if request.method in INVALIDATING_METHODS and response.ok:
            cache_url = self.controller.cache_url(request.url)
            self.cache.delete(cache_url)

        response.from_cache = from_cache

        return request, response