FEAT: Filters for GET requests, add it to GET /payments (#1557)
* feat filters, add them to GET payments * add limit and offset to filters (#1563) * add limit and offset to filters * move filters example to parse_filters doc string * black * add openapi docs * remove example commentC * improve typing and make nested filter possible in openapi * typo in fn name * readd Type --------- Co-authored-by: jackstar12 <62219658+jackstar12@users.noreply.github.com> Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
co-authored by
jackstar12
calle
parent
fe9e821af5
commit
8ce84ce592
@@ -1,15 +1,18 @@
|
||||
from http import HTTPStatus
|
||||
from typing import Optional, Type
|
||||
|
||||
from fastapi import Security, status
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi.openapi.models import APIKey, APIKeyIn
|
||||
from fastapi.security.api_key import APIKeyHeader, APIKeyQuery
|
||||
from fastapi.security.base import SecurityBase
|
||||
from pydantic import BaseModel
|
||||
from pydantic.types import UUID4
|
||||
from starlette.requests import Request
|
||||
|
||||
from lnbits.core.crud import get_user, get_wallet_for_key
|
||||
from lnbits.core.models import User, Wallet
|
||||
from lnbits.db import Filter, Filters
|
||||
from lnbits.requestvars import g
|
||||
from lnbits.settings import settings
|
||||
|
||||
@@ -266,3 +269,29 @@ async def check_super_user(usr: UUID4) -> User:
|
||||
detail="User not authorized. No super user privileges.",
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
def parse_filters(model: Type[BaseModel]):
|
||||
"""
|
||||
Parses the query params as filters.
|
||||
:param model: model used for validation of filter values
|
||||
"""
|
||||
|
||||
def dependency(
|
||||
request: Request, limit: Optional[int] = None, offset: Optional[int] = None
|
||||
):
|
||||
params = request.query_params
|
||||
filters = []
|
||||
for key in params.keys():
|
||||
try:
|
||||
filters.append(Filter.parse_query(key, params.getlist(key), model))
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return Filters(
|
||||
filters=filters,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
return dependency
|
||||
|
||||
Reference in New Issue
Block a user