Skip to content
Snippets Groups Projects
Verified Commit 4705a8eb authored by Eliot Berriot's avatar Eliot Berriot
Browse files

First working PoC with API

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