2018-06-06 22:00:35 +00:00
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from .urlutils import check_url
|
|
|
|
from .errors import ActivityNotFoundError
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultRemoteObjectFetcher(object):
|
|
|
|
"""Not meant to be used on production, a caching layer, and DB shortcut fox inbox/outbox should be hooked."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._user_agent = 'Little Boxes (+https://github.com/tsileo/little_boxes)'
|
|
|
|
|
|
|
|
def fetch(self, iri):
|
|
|
|
check_url(iri)
|
|
|
|
|
2018-06-08 19:33:46 +00:00
|
|
|
resp = requests.get(iri, headers={
|
2018-06-06 22:00:35 +00:00
|
|
|
'Accept': 'application/activity+json',
|
2018-06-08 19:33:46 +00:00
|
|
|
'User-Agent': self._user_agent,
|
2018-06-06 22:00:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if resp.status_code == 404:
|
2018-06-08 19:33:46 +00:00
|
|
|
raise ActivityNotFoundError(f'{iri} cannot be fetched, 404 not found error')
|
2018-06-06 22:00:35 +00:00
|
|
|
|
|
|
|
resp.raise_for_status()
|
2018-06-08 19:33:46 +00:00
|
|
|
|
2018-06-06 22:00:35 +00:00
|
|
|
return resp.json()
|
|
|
|
|
2018-06-08 19:33:46 +00:00
|
|
|
|
2018-06-06 22:00:35 +00:00
|
|
|
OBJECT_FETCHER = DefaultRemoteObjectFetcher()
|
|
|
|
|
2018-06-08 19:33:46 +00:00
|
|
|
|
2018-06-06 22:00:35 +00:00
|
|
|
def set_object_fetcher(object_fetcher: Any):
|
2018-06-08 19:33:46 +00:00
|
|
|
global OBJECT_FETCHER
|
2018-06-06 22:00:35 +00:00
|
|
|
OBJECT_FETCHER = object_fetcher
|