funkwhale/api/funkwhale_api/federation/library.py

50 wiersze
1.7 KiB
Python
Czysty Zwykły widok Historia

2018-06-10 08:55:16 +00:00
import requests
from django.conf import settings
from funkwhale_api.common import session
2018-09-28 20:47:05 +00:00
from . import serializers, signing
def get_library_data(library_url, actor):
auth = signing.get_auth(actor.private_key, actor.private_key_id)
try:
response = session.get_session().get(
library_url,
auth=auth,
timeout=5,
verify=settings.EXTERNAL_REQUESTS_VERIFY_SSL,
2018-06-09 13:36:16 +00:00
headers={"Content-Type": "application/activity+json"},
)
except requests.ConnectionError:
2018-06-09 13:36:16 +00:00
return {"errors": ["This library is not reachable"]}
scode = response.status_code
if scode == 401:
2018-06-09 13:36:16 +00:00
return {"errors": ["This library requires authentication"]}
elif scode == 403:
2018-06-09 13:36:16 +00:00
return {"errors": ["Permission denied while scanning library"]}
elif scode >= 400:
2018-06-09 13:36:16 +00:00
return {"errors": ["Error {} while fetching the library".format(scode)]}
2018-09-24 18:44:22 +00:00
serializer = serializers.LibrarySerializer(data=response.json())
2018-04-08 18:27:10 +00:00
if not serializer.is_valid():
2018-06-09 13:36:16 +00:00
return {"errors": ["Invalid ActivityPub response from remote library"]}
2018-04-08 18:27:10 +00:00
return serializer.validated_data
2018-04-11 21:13:33 +00:00
def get_library_page(library, page_url, actor):
2018-04-11 21:13:33 +00:00
auth = signing.get_auth(actor.private_key, actor.private_key_id)
response = session.get_session().get(
page_url,
auth=auth,
timeout=5,
verify=settings.EXTERNAL_REQUESTS_VERIFY_SSL,
2018-06-09 13:36:16 +00:00
headers={"Content-Type": "application/activity+json"},
2018-04-11 21:13:33 +00:00
)
serializer = serializers.CollectionPageSerializer(
data=response.json(),
2018-09-22 12:29:30 +00:00
context={"library": library, "item_serializer": serializers.UploadSerializer},
2018-06-09 13:36:16 +00:00
)
2018-04-11 21:13:33 +00:00
serializer.is_valid(raise_exception=True)
return serializer.validated_data