28 lines
950 B
Python
28 lines
950 B
Python
from typing import Optional
|
|
|
|
from fastapi import Header, Query
|
|
|
|
from .errors import APIError
|
|
from .store import store
|
|
|
|
|
|
def page_limit(limit: str = Query(default="50")) -> int:
|
|
if not limit.isascii() or not limit.isdecimal() or len(limit) > 3:
|
|
raise APIError(400, "invalid_request", "Неверный limit")
|
|
number = int(limit)
|
|
if not 1 <= number <= 100:
|
|
raise APIError(400, "invalid_request", "Неверный limit")
|
|
return number
|
|
|
|
|
|
|
|
def current_user(x_user_id: Optional[str] = Header(default=None, alias="X-User-Id")) -> dict:
|
|
if not x_user_id:
|
|
raise APIError(401, "unauthorized", "Нет действительной идентификации")
|
|
try:
|
|
return store.require_user(x_user_id)
|
|
except APIError as exc:
|
|
if exc.status_code == 400:
|
|
raise APIError(401, "unauthorized", "Нет действительной идентификации") from exc
|
|
raise
|