add web API option to limit the number of results

This commit is contained in:
ducklet 2021-06-22 12:22:50 +02:00
parent f602542f43
commit 0a21cb4420
2 changed files with 22 additions and 4 deletions

View file

@ -1,7 +1,7 @@
import base64
import binascii
import logging
from typing import Literal
from typing import Literal, Optional
from starlette.applications import Starlette
from starlette.authentication import (
@ -63,6 +63,23 @@ def yearcomp(s: str):
return comp, int(s)
def as_int(x, *, max: int = None, min: Optional[int] = 1, default: int = None):
try:
if not isinstance(x, int):
x = int(x)
if min is not None and x < min:
return min
if max is not None and x > max:
return max
return x
except:
if default is None:
raise
return default
async def ratings(request):
params = request.query_params
rows = await find_ratings(
@ -71,6 +88,7 @@ async def ratings(request):
ignore_tv_episodes=truthy(params.get("ignore_tv_episodes")),
include_unrated=truthy(params.get("include_unrated")),
yearcomp=yearcomp(params["year"]) if "year" in params else None,
limit_rows=as_int(params.get("per_page"), max=10, default=5),
)
aggr = {}