Verified Commit 124aca7b authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Added /sources endpoint

parent b5a9a5fb
......@@ -18,6 +18,7 @@ application = ProtocolTypeRouter(
{
"http": URLRouter(
[
url(r"^v1/sources$", consumers.SourcesConsumer),
url(r"^v1/providers$", consumers.ProvidersConsumer),
url(r"^v1/search/$", consumers.SearchMultipleConsumer),
url(
......
......@@ -195,3 +195,13 @@ class ProvidersConsumer(AsyncHttpConsumer):
for _, p in sorted(providers.registry, key=lambda v: v[0])
]
await json_response(self, 200, data)
class SourcesConsumer(AsyncHttpConsumer):
@wrapper_500
async def handle(self, body):
data = [
{"id": p.id, "label": p.label, "example": p.example}
for p in sorted(sources.registry._data.values(), key=lambda v: v.id)
]
await json_response(self, 200, data)
......@@ -26,6 +26,8 @@ registry = Registry()
class Source(object):
id = None
label = None
example = None
async def get(self, lookup):
raise NotImplementedError()
......@@ -34,6 +36,8 @@ class Source(object):
@registry.register
class Activitypub(Source):
id = "activitypub"
label = "ActivityPub URL"
example = "https://mastodon.domain/@username"
excluded_tags = [
"#noretribute",
# '#nobot',
......@@ -73,6 +77,8 @@ class Activitypub(Source):
@registry.register
class Webfinger(Source):
id = "webfinger"
label = "Fediverse/Webfinger username"
example = "username@mastodon.domain"
async def get(self, lookup, session, cache):
webfinger_data = await webfinger.lookup(lookup, session, cache=cache)
......@@ -86,6 +92,8 @@ class Webfinger(Source):
@registry.register
class MusicBrainz(Source):
id = "musicbrainz"
label = "MusicBrainz Artist ID"
example = "a2e55cf5-ca3a-4c26-ba62-fc4a4f2bc603"
async def get(self, lookup, session, cache):
......
......@@ -142,3 +142,18 @@ async def test_providers(loop, application, mocker, coroutine_mock):
(b"Access-Control-Allow-Origin", b"*"),
]
assert response["body"] == json.dumps(expected, indent=2, sort_keys=True).encode()
async def test_sources(loop, application, mocker, coroutine_mock):
expected = [
{"id": p.id, "label": p.label, "example": p.example}
for p in sorted(sources.registry._data.values(), key=lambda v: v.id)
]
communicator = HttpCommunicator(application, "GET", "/v1/sources")
response = await communicator.get_response()
assert response["status"] == 200
assert response["headers"] == [
(b"Content-Type", b"application/json"),
(b"Access-Control-Allow-Origin", b"*"),
]
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