Revert to request_cache monkey patching, take #2. Catch KeyError in model_to_objects.

Run extract_receivers for unsupported objects.
more-content
Alain St-Denis 2022-05-19 12:48:57 +00:00
rodzic f95f370d0e
commit f46f30e61a
2 zmienionych plików z 10 dodań i 11 usunięć

Wyświetl plik

@ -900,7 +900,6 @@ def element_to_objects(element: Union[Dict, Object]) -> List:
"""
Transform an Element to a list of entities.
"""
entities = []
# json-ld handling with calamus
# Skips unimplemented payloads
@ -914,14 +913,14 @@ def element_to_objects(element: Union[Dict, Object]) -> List:
except ValueError:
logger.error("Failed to validate entity %s: %s", entity, ex)
return None
entities.append(entity)
#if not found_parent and getattr(entity, 'target_id', None):
# entities = retrieve_and_parse_document(entity.target_id) + entities
#if getattr(entity, 'replies', None):
# entities += process_reply_collection(getattr(entity.replies,'first', None))
return entities
return [entity]
elif entity:
logger.info('Entity type "%s" was handled through the json-ld processor but is not a base entity', entity.__class__.__name__)
entity._receivers = extract_receivers(entity)
return [entity]
else:
logger.warning("Payload not implemented by the json-ld processor, skipping")
@ -933,8 +932,8 @@ def model_to_objects(payload):
if model and issubclass(model, Object):
try:
entity = model.schema().load(payload)
except (jsonld.JsonLdError, exceptions.ValidationError) as exc : # Just give up for now. This must be made robust
logger.warning(f"Invalid jsonld payload, skipping ({exc})")
except (KeyError, jsonld.JsonLdError, exceptions.ValidationError) as exc : # Just give up for now. This must be made robust
logger.warning(f"Error parsing jsonld payload ({exc})")
return None
if isinstance(getattr(entity, 'object_', None), Object):

Wyświetl plik

@ -11,7 +11,7 @@ import requests
from requests.exceptions import RequestException, HTTPError, SSLError
from requests.exceptions import ConnectionError
from requests.structures import CaseInsensitiveDict
from requests_cache import install_cache, RedisCache, SQLiteCache
import requests_cache as rc
from federation import __version__
@ -22,13 +22,13 @@ try:
from federation.utils.django import get_configuration
cfg = get_configuration()
if cfg.get('redis'):
backend = RedisCache(namespace='fed_cache', **cfg['redis'])
backend = rc.RedisCache(namespace='fed_cache', **cfg['redis'])
else:
backend = SQLiteCache(db_path='fed_cache')
backend = rc.SQLiteCache(db_path='fed_cache')
except ImportError:
backend = SQLiteCache(db_path='fed_cache')
backend = rc.SQLiteCache(db_path='fed_cache')
install_cache(backend=backend, expire_after=7200)
rc.install_cache(backend=backend, expire_after=7200)
USER_AGENT = "python/federation/%s" % __version__
@ -77,7 +77,7 @@ def fetch_document(url=None, host=None, path="/", timeout=10, raise_ssl_errors=T
if cache:
response = requests.get(url, timeout=timeout, headers=headers, **kwargs)
else:
with requests_cache.disabled():
with rc.disabled():
response = requests.get(url, timeout=timeout, headers=headers, **kwargs)
logger.debug("fetch_document: found document, code %s", response.status_code)
response.raise_for_status()