Verified Commit 4705a8eb authored by Eliot Berriot's avatar Eliot Berriot
Browse files

First working PoC with API

parent 4caebeb6
......@@ -283,3 +283,4 @@ ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/")
# ------------------------------------------------------------------------------
# http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise
MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware") # noqa F405
BASE_URL = env("BASE_URL")
......@@ -11,11 +11,22 @@ async def json_response(self, status, content):
await self.send_response(
status,
json.dumps(content, indent=2, sort_keys=True).encode("utf-8"),
headers=[{"Content-Type": "application/json"}],
headers=[(b"Content-Type", b"application/json")],
)
def wrapper_500(callback):
async def inner(self, body):
try:
await callback(self, body)
except Exception as e:
await json_response(self, 400, {"detail": str(e)})
return callback
class SearchSingleConsumer(AsyncHttpConsumer):
@wrapper_500
async def handle(self, body):
lookup_type = self.scope["url_route"]["kwargs"]["lookup_type"]
lookup = self.scope["url_route"]["kwargs"]["lookup"]
......@@ -24,17 +35,10 @@ class SearchSingleConsumer(AsyncHttpConsumer):
except KeyError:
await json_response(self, 400, {"detail": "Invalid lookup"})
try:
import ipdb
ipdb.set_trace()
async with aiohttp.client.ClientSession() as session:
data = await source.get(lookup, session)
profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
except exceptions.SearchError as e:
await json_response(self, 400, {"detail": e.message})
raise
try:
profile = sources.result_to_retribute_profile(lookup_type, lookup, data)
except Exception:
raise
await json_response(self, 200, profile)
......@@ -32,6 +32,10 @@ class Source(object):
@registry.register
class Activitypub(Source):
id = "activitypub"
excluded_tags = [
"#noretribute",
# '#nobot',
]
async def get(self, lookup, session):
async with session.get(
......@@ -42,7 +46,7 @@ class Activitypub(Source):
serializer = activitypub.ActorSerializer(data=actor_data)
serializer.is_valid(raise_exception=True)
for tag in serializer.validated_data["tag"]:
if tag["name"] in ["#nobot"]:
if tag["name"].lower() in self.excluded_tags:
raise exceptions.SkippedProfile()
data = {
......
......@@ -5,7 +5,7 @@ def test_profile_serializer():
payload = {
"id": "https://test.domain",
"url": "https://test.domain",
"tag": [{"type": "Hashtag", "name": "#nobot"}],
"tag": [{"type": "Hashtag", "name": "#NoRetribute"}],
"attachment": [
{
"type": "PropertyValue",
......
......@@ -20,5 +20,5 @@ async def test_search_consumer_success(loop, application, mocker, coroutine_mock
"webfinger", "test@user.domain", get.return_value
)
assert response["status"] == 200
assert response["headers"] == [{"Content-Type": "application/json"}]
assert response["headers"] == [(b"Content-Type", b"application/json")]
assert response["body"] == json.dumps(expected, indent=2, sort_keys=True).encode()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment