mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 20:06:22 -05:00
Creating api_param_limiter which can be used to restrict limit or offset.
This commit is contained in:
parent
ad1b8b805e
commit
9da3b3d8b2
5 changed files with 57 additions and 15 deletions
|
|
@ -13,7 +13,7 @@ from sqlalchemy.orm import Session
|
|||
|
||||
from wrolpi import before_startup
|
||||
from wrolpi.common import logger, get_config, iterify, chunks, get_media_directory, \
|
||||
minimize_dict
|
||||
minimize_dict, api_param_limiter
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.errors import UnknownFile, UnknownDirectory, ChannelNameConflict, ChannelURLConflict, \
|
||||
ChannelLinkConflict, ChannelDirectoryConflict, ChannelSourceIdConflict
|
||||
|
|
@ -47,14 +47,7 @@ def load_channels_config() -> dict:
|
|||
return channels
|
||||
|
||||
|
||||
def get_allowed_limit(limit: int) -> int:
|
||||
"""
|
||||
Return the video limit int if it was passed, unless it is over the maximum limit. If no limit is provided, return
|
||||
the default limit.
|
||||
"""
|
||||
if limit:
|
||||
return min(int(limit), VIDEO_QUERY_MAX_LIMIT)
|
||||
return VIDEO_QUERY_LIMIT
|
||||
video_limiter = api_param_limiter(100)
|
||||
|
||||
|
||||
class ConfigError(Exception):
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ from wrolpi.errors import ValidationError, InvalidOrderBy
|
|||
from wrolpi.root_api import json_response
|
||||
from wrolpi.schema import validate_doc, JSONErrorResponse
|
||||
from .lib import get_video, VIDEO_ORDERS, DEFAULT_VIDEO_ORDER, video_search, get_video_for_app
|
||||
from ..common import get_matching_directories, get_allowed_limit
|
||||
from ..common import get_matching_directories, video_limiter
|
||||
from ..lib import save_channels_config
|
||||
from ..schema import VideoResponse, VideoSearchRequest, VideoSearchResponse, \
|
||||
DirectoriesResponse, DirectoriesRequest
|
||||
|
|
@ -45,7 +45,7 @@ async def search(_: Request, data: dict):
|
|||
channel_link = data.get('channel_link')
|
||||
order_by = data.get('order_by', DEFAULT_VIDEO_ORDER)
|
||||
offset = int(data.get('offset', 0))
|
||||
limit = get_allowed_limit(data.get('limit'))
|
||||
limit = video_limiter(data.get('limit'))
|
||||
filters = data.get('filters', None)
|
||||
except Exception as e:
|
||||
raise ValidationError('Unable to validate search queries') from e
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue