def send(self, request, **kwargs): if (self._is_cache_disabled or request.method not in self._cache_allowable_methods): response = super(CachedSession, self).send(request, **kwargs) response.from_cache = False return response cache_key = self.cache.create_key(request) def send_request_and_cache_response(): response = super(CachedSession, self).send(request, **kwargs) if response.status_code in self._cache_allowable_codes: self.cache.save_response(cache_key, response) response.from_cache = False return response response, timestamp = self.cache.get_response_and_time(cache_key) if response is None: return send_request_and_cache_response() if self._cache_expire_after is not None: difference = datetime.utcnow() - timestamp if difference > timedelta(seconds=self._cache_expire_after): self.cache.delete(cache_key) return send_request_and_cache_response() # dispatch hook here, because we've removed it before pickling response = dispatch_hook('response', request.hooks, response, **kwargs) response.from_cache = True return response
def _request_send_hook(self, *args, **kwargs): if self.method not in _config['allowable_methods']: return _original_request_send(self, *args, **kwargs) if self.method == 'POST': data = self._encode_params(getattr(self, 'data', {})) if isinstance(data, tuple): # old requests versions data = data[1] cache_url = self.full_url + str(data) else: cache_url = self.full_url def send_request_and_cache_response(): result = _original_request_send(self, *args, **kwargs) if result and self.response.status_code in _config['allowable_codes']: _cache.save_response(cache_url, self.response) return result response, timestamp = _cache.get_response_and_time(cache_url) if response is None: return send_request_and_cache_response() if _config['expire_after'] is not None: difference = datetime.now() - timestamp if difference > timedelta(minutes=_config['expire_after']): _cache.del_cached_url(cache_url) return send_request_and_cache_response() response.from_cache = True self.sent = True self.response = response # TODO: is it stable api? if dispatch_hook is not None: dispatch_hook('response', self.hooks, self.response) r = dispatch_hook('post_request', self.hooks, self) self.__dict__.update(r.__dict__) return True
def _request_send_hook(self, *args, **kwargs): if self.method not in _config['allowable_methods']: return _original_request_send(self, *args, **kwargs) if self.method == 'POST': data = self._encode_params(getattr(self, 'data', {})) if isinstance(data, tuple): # old requests versions data = data[1] cache_url = self.full_url + str(data) else: cache_url = self.full_url def send_request_and_cache_response(): result = _original_request_send(self, *args, **kwargs) if result and self.response.status_code in _config['allowable_codes']: _cache.save_response(cache_url, self.response) return result response, timestamp = _cache.get_response_and_time(cache_url) if response is None: return send_request_and_cache_response() if _config['expire_after'] is not None: difference = datetime.now() - timestamp if difference > timedelta(minutes=_config['expire_after']): _cache.del_cached_url(cache_url) return send_request_and_cache_response() response.from_cache = True self.sent = True self.response = response # TODO: is it stable api? if dispatch_hook is not None: dispatch_hook('response', self.hooks, self.response) r = dispatch_hook('post_request', self.hooks, self) self.__dict__.update(r.__dict__) return True