2018-06-10 08:55:16 +00:00
|
|
|
import requests
|
2018-04-08 16:24:07 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2018-04-08 11:35:37 +00:00
|
|
|
from funkwhale_api.common import session
|
|
|
|
|
2018-09-28 20:47:05 +00:00
|
|
|
from . import serializers, signing
|
2018-04-08 11:35:37 +00:00
|
|
|
|
|
|
|
|
2018-09-06 18:35:02 +00:00
|
|
|
def get_library_data(library_url, actor):
|
2018-04-08 11:35:37 +00:00
|
|
|
auth = signing.get_auth(actor.private_key, actor.private_key_id)
|
|
|
|
try:
|
|
|
|
response = session.get_session().get(
|
|
|
|
library_url,
|
|
|
|
auth=auth,
|
|
|
|
timeout=5,
|
2018-04-08 16:24:07 +00:00
|
|
|
verify=settings.EXTERNAL_REQUESTS_VERIFY_SSL,
|
2018-06-09 13:36:16 +00:00
|
|
|
headers={"Content-Type": "application/activity+json"},
|
2018-04-08 11:35:37 +00:00
|
|
|
)
|
|
|
|
except requests.ConnectionError:
|
2018-06-09 13:36:16 +00:00
|
|
|
return {"errors": ["This library is not reachable"]}
|
2018-04-08 11:35:37 +00:00
|
|
|
scode = response.status_code
|
|
|
|
if scode == 401:
|
2018-06-09 13:36:16 +00:00
|
|
|
return {"errors": ["This library requires authentication"]}
|
2018-04-08 11:35:37 +00:00
|
|
|
elif scode == 403:
|
2018-06-09 13:36:16 +00:00
|
|
|
return {"errors": ["Permission denied while scanning library"]}
|
2018-04-08 11:35:37 +00:00
|
|
|
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
|
|
|
|
2018-04-08 11:35:37 +00:00
|
|
|
return serializer.validated_data
|
2018-04-11 21:13:33 +00:00
|
|
|
|
|
|
|
|
2018-09-06 18:35:02 +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
|