def processResponse(self, service, url, response): query = CacheEntryTimed.objects.filter(service=service, url=url) cache_entry = CacheEntryTimed() if len(query): cache_entry = query[0] if response.status == 304: if cache_entry is None: raise Exception("304, but no content??") response = MockHTTP() response.status = cache_entry.status response.data = cache_entry.content response.headers = cache_entry.headers return {"response": response} else: now = make_aware(datetime.now(), get_current_timezone()) cache_entry.service = service cache_entry.url = url cache_entry.status = response.status cache_entry.content = response.data cache_entry.headers = response.headers cache_entry.time_saved = now store_cache_entry(cache_entry) return
def _process_response(self, service, url, response, overwrite_success_with_error_at=60 * 60 * 8): now = make_aware(datetime.now(), get_current_timezone()) query = CacheEntryTimed.objects.filter(service=service, url=url) cache_entry = None if len(query): cache_entry = query[0] else: cache_entry = CacheEntryTimed() if response.status != 200: # Only override a successful cache entry with an error if the # Successful entry is older than 8 hours - MUWM-509 if cache_entry.id is not None and cache_entry.status == 200: save_delta = now - cache_entry.time_saved extended_cache_delta = timedelta( seconds=overwrite_success_with_error_at) if save_delta < extended_cache_delta: response = MockHTTP() response.status = cache_entry.status response.data = cache_entry.content return {"response": response} cache_entry.service = service cache_entry.url = url cache_entry.status = response.status cache_entry.content = response.data # This extra step is needed w/ Live resources because # HTTPHeaderDict isn't serializable. header_data = {} for header in response.headers: header_data[header] = response.getheader(header) cache_entry.headers = header_data cache_entry.time_saved = now try: store_cache_entry(cache_entry) except Exception as ex: # If someone beat us in to saving a cache entry, that's ok. # We just need a very recent entry. return return