From a4c6a71a3a58fc03d02ce024a914877f34fc6574 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Thu, 28 Mar 2019 11:31:42 +0100 Subject: [PATCH] Can now limit the number of results returned by the /domains endpoint --- funkwhale_network/routes.py | 8 ++++++-- tests/test_routes.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/funkwhale_network/routes.py b/funkwhale_network/routes.py index 5f84b00..1a0a530 100644 --- a/funkwhale_network/routes.py +++ b/funkwhale_network/routes.py @@ -21,6 +21,7 @@ domain_filters = { "open_registrations": fields.Bool(), "anonymous_can_listen": fields.Bool(), "federation_enabled": fields.Bool(), + "limit": fields.Integer(), } @@ -48,11 +49,14 @@ async def index(request): async def domains(request): if request.method == "GET": - filters = await parser.parse(domain_filters, request) + limit = filters.pop("limit", 0) rows = await db.get_domains(request["conn"], private=False, **filters) + total = len(rows) + if limit: + rows = rows[:limit] payload = { - "count": len(rows), + "count": total, "previous": None, "next": None, "results": [ diff --git a/tests/test_routes.py b/tests/test_routes.py index 35973ad..fbe9698 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -29,6 +29,29 @@ async def test_domains_get(db_conn, client, factories): } +async def test_domains_get_page_size(db_conn, client, factories): + + checks = sorted( + [ + await factories["Check"].c(private=False), + await factories["Check"].c(private=False), + ], + key=lambda o: o["domain"], + ) + for check in checks: + domain = await db.get_domain(db_conn, check["domain"]) + check["first_seen"] = domain["first_seen"] + check["node_name"] = domain["node_name"] + resp = await client.get("/api/domains", params={"limit": 1}) + assert resp.status == 200 + assert await resp.json() == { + "count": 2, + "next": None, + "previous": None, + "results": [serializers.serialize_domain_from_check(checks[0])], + } + + @pytest.mark.parametrize( "field", ["up", "open_registrations", "anonymous_can_listen", "federation_enabled"] ) -- GitLab