From 122c8a999842f5833976ba0870abcecb7b7a9531 Mon Sep 17 00:00:00 2001 From: Phil Salant Date: Mon, 31 Oct 2022 10:39:39 -0600 Subject: [PATCH 1/2] Just default `offset` to 0 --- fideslog/api/database/registrations.py | 4 ++-- fideslog/api/routes/registrations.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fideslog/api/database/registrations.py b/fideslog/api/database/registrations.py index 8d8c6b0..cdf4ff1 100644 --- a/fideslog/api/database/registrations.py +++ b/fideslog/api/database/registrations.py @@ -14,7 +14,7 @@ def get( database: Session, count: Optional[int], - offset: Optional[int], + offset: int = 0, ) -> List[RegistrationORM]: """ Return existing registrations. @@ -22,7 +22,7 @@ def get( log.debug("Fetching registrations") if count is not None: - return database.query(RegistrationORM).limit(count).offset(offset or 0).all() + return database.query(RegistrationORM).limit(count).offset(offset).all() return database.query(RegistrationORM).all() diff --git a/fideslog/api/routes/registrations.py b/fideslog/api/routes/registrations.py index 684d898..b800e9b 100644 --- a/fideslog/api/routes/registrations.py +++ b/fideslog/api/routes/registrations.py @@ -32,8 +32,8 @@ async def list_registrations( description="The amount of registrations to return.", gt=0, ), - offset: Optional[int] = Query( - default=None, + offset: int = Query( + default=0, description="The amount of registrations to skip before returning.", gt=0, ), From 6ec63fa77205fb3b3fffa04634ea947358209a03 Mon Sep 17 00:00:00 2001 From: Phil Salant Date: Mon, 31 Oct 2022 11:27:58 -0600 Subject: [PATCH 2/2] Paginate with `fastapi-pagination` --- fideslog/api/database/registrations.py | 16 +++++----- fideslog/api/requirements.txt | 5 +-- fideslog/api/routes/registrations.py | 43 +++++++------------------- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/fideslog/api/database/registrations.py b/fideslog/api/database/registrations.py index cdf4ff1..e07cfe5 100644 --- a/fideslog/api/database/registrations.py +++ b/fideslog/api/database/registrations.py @@ -1,7 +1,8 @@ from datetime import datetime, timezone from logging import getLogger -from typing import List, Optional +from fastapi_pagination.bases import AbstractPage, AbstractParams +from fastapi_pagination.ext.sqlalchemy import paginate from sqlalchemy.exc import NoResultFound from sqlalchemy.orm import Session @@ -13,18 +14,17 @@ def get( database: Session, - count: Optional[int], - offset: int = 0, -) -> List[RegistrationORM]: + pagination_params: AbstractParams, +) -> AbstractPage[RegistrationORM]: """ Return existing registrations. """ log.debug("Fetching registrations") - if count is not None: - return database.query(RegistrationORM).limit(count).offset(offset).all() - - return database.query(RegistrationORM).all() + return paginate( + database.query(RegistrationORM).order_by(RegistrationORM.created_at.desc()), + params=pagination_params, + ) def create(database: Session, registration: Registration) -> None: diff --git a/fideslog/api/requirements.txt b/fideslog/api/requirements.txt index e4c2db0..92a5c99 100644 --- a/fideslog/api/requirements.txt +++ b/fideslog/api/requirements.txt @@ -1,5 +1,6 @@ -fastapi==0.74.0 -pydantic[email]==1.9.0 +fastapi-pagination[sqlalchemy]== 0.10.0 +fastapi==0.82.0 +pydantic[email]==1.9.1 slowapi==0.1.5 snowflake-sqlalchemy==1.3.3 SQLAlchemy-Utils==0.38.3 diff --git a/fideslog/api/routes/registrations.py b/fideslog/api/routes/registrations.py index b800e9b..f0627e4 100644 --- a/fideslog/api/routes/registrations.py +++ b/fideslog/api/routes/registrations.py @@ -1,7 +1,9 @@ from logging import getLogger -from typing import List, Optional +from typing import List -from fastapi import APIRouter, Depends, Query, Request, Response, status +from fastapi import APIRouter, Depends, Request, Response, status +from fastapi_pagination import Params +from fastapi_pagination.bases import AbstractPage from sqlalchemy.exc import DBAPIError, NoResultFound from sqlalchemy.orm import Session from sqlalchemy.orm.exc import UnmappedInstanceError @@ -9,6 +11,7 @@ from ..database import get_db from ..database.registrations import create, delete, get, update from ..errors import InternalServerError, NotFoundError, TooManyRequestsError +from ..models.models import Registration as RegistrationORM from ..schemas.registration import Registration log = getLogger(__name__) @@ -27,37 +30,19 @@ ) async def list_registrations( _: Request, - count: Optional[int] = Query( - default=None, - description="The amount of registrations to return.", - gt=0, - ), - offset: int = Query( - default=0, - description="The amount of registrations to skip before returning.", - gt=0, - ), + params: Params = Depends(), database: Session = Depends(get_db), -) -> List[Registration]: +) -> AbstractPage[RegistrationORM]: """ List existing registrations. """ try: - registrations = get(database, count, offset) + registrations = get(database, params) except DBAPIError as err: raise InternalServerError(err) from err - return [ - Registration( - client_id=registration.client_id, - email=registration.email, - organization=registration.organization, - created_at=registration.created_at, - updated_at=registration.updated_at, - ) - for registration in registrations - ] + return registrations.items # type: ignore[attr-defined] @registration_router.post( @@ -102,7 +87,7 @@ async def modify_registration( _: Request, registration: Registration, database: Session = Depends(get_db), -) -> Registration: +) -> RegistrationORM: """ Update an existing registration. """ @@ -114,13 +99,7 @@ async def modify_registration( except Exception as err: raise InternalServerError(err) from err - return Registration( - client_id=updated.client_id, - email=updated.email, - organization=updated.organization, - created_at=updated.created_at, - updated_at=updated.updated_at, - ) + return updated @registration_router.delete(