Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
retribute.me
api
Commits
4705a8eb
Verified
Commit
4705a8eb
authored
May 29, 2019
by
Eliot Berriot
Browse files
First working PoC with API
parent
4caebeb6
Changes
5
Hide whitespace changes
Inline
Side-by-side
config/settings/base.py
View file @
4705a8eb
...
...
@@ -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"
)
retribute_api/search/consumers.py
View file @
4705a8eb
...
...
@@ -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
)
retribute_api/search/sources.py
View file @
4705a8eb
...
...
@@ -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
=
{
...
...
tests/search/test_activitypub.py
View file @
4705a8eb
...
...
@@ -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"
,
...
...
tests/search/test_consumers.py
View file @
4705a8eb
...
...
@@ -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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment