Skip to content

Commit

Permalink
🎨 deduplicate reading of limit and offset query parameters
Browse files Browse the repository at this point in the history
Signed-off-by: ff137 <ff137@proton.me>
  • Loading branch information
ff137 committed Jun 25, 2024
1 parent ac1b2f0 commit f3e33ab
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
18 changes: 18 additions & 0 deletions aries_cloudagent/messaging/models/paginated_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Class for paginated query parameters."""

from typing import Tuple

from aiohttp.web import BaseRequest
from marshmallow import fields

from ...messaging.models.openapi import OpenAPISchema
Expand Down Expand Up @@ -28,3 +31,18 @@ class PaginatedQuerySchema(OpenAPISchema):
metadata={"description": "Offset for pagination", "example": 0},
error_messages={"validator_failed": "Value must be 0 or greater"},
)


def get_limit_offset(request: BaseRequest) -> Tuple[int, int]:
"""Read the limit and offset query parameters from a request as ints, with defaults.
Args:
request: aiohttp request object
Returns:
A tuple of the limit and offset values
"""

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
return limit, offset
6 changes: 2 additions & 4 deletions aries_cloudagent/multitenant/admin/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
from ...core.profile import ProfileManagerProvider
from ...messaging.models.base import BaseModelError
from ...messaging.models.openapi import OpenAPISchema
from ...messaging.models.paginated_query import PaginatedQuerySchema
from ...messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ...messaging.valid import UUID4_EXAMPLE, JSONWebToken
from ...multitenant.base import BaseMultitenantManager
from ...storage.base import DEFAULT_PAGE_SIZE
from ...storage.error import StorageError, StorageNotFoundError
from ...utils.endorsement_setup import attempt_auto_author_with_endorser_setup
from ...utils.profiles import subwallet_type_not_same_as_base_wallet_raise_web_exception
Expand Down Expand Up @@ -382,8 +381,7 @@ async def wallets_list(request: web.BaseRequest):
if wallet_name:
query["wallet_name"] = wallet_name

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

try:
async with profile.session() as session:
Expand Down
6 changes: 2 additions & 4 deletions aries_cloudagent/protocols/connections/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ....connections.models.conn_record import ConnRecord, ConnRecordSchema
from ....messaging.models.base import BaseModelError
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.models.paginated_query import PaginatedQuerySchema
from ....messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ....messaging.valid import (
ENDPOINT_EXAMPLE,
ENDPOINT_VALIDATE,
Expand All @@ -31,7 +31,6 @@
UUID4_EXAMPLE,
UUID4_VALIDATE,
)
from ....storage.base import DEFAULT_PAGE_SIZE
from ....storage.error import StorageError, StorageNotFoundError
from ....wallet.error import WalletError
from .manager import ConnectionManager, ConnectionManagerError
Expand Down Expand Up @@ -470,8 +469,7 @@ async def connections_list(request: web.BaseRequest):
if request.query.get("connection_protocol"):
post_filter["connection_protocol"] = request.query["connection_protocol"]

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

profile = context.profile
try:
Expand Down
6 changes: 2 additions & 4 deletions aries_cloudagent/protocols/issue_credential/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ....messaging.credential_definitions.util import CRED_DEF_TAGS
from ....messaging.models.base import BaseModelError
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.models.paginated_query import PaginatedQuerySchema
from ....messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ....messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_CRED_DEF_ID_VALIDATE,
Expand All @@ -35,7 +35,6 @@
UUID4_EXAMPLE,
UUID4_VALIDATE,
)
from ....storage.base import DEFAULT_PAGE_SIZE
from ....storage.error import StorageError, StorageNotFoundError
from ....utils.tracing import AdminAPIMessageTracingSchema, get_timer, trace_event
from ....wallet.util import default_did_from_verkey
Expand Down Expand Up @@ -405,8 +404,7 @@ async def credential_exchange_list(request: web.BaseRequest):
if request.query.get(k, "") != ""
}

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

try:
async with context.profile.session() as session:
Expand Down
6 changes: 2 additions & 4 deletions aries_cloudagent/protocols/issue_credential/v2_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ....messaging.decorators.attach_decorator import AttachDecorator
from ....messaging.models.base import BaseModelError
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.models.paginated_query import PaginatedQuerySchema
from ....messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ....messaging.valid import (
INDY_CRED_DEF_ID_EXAMPLE,
INDY_CRED_DEF_ID_VALIDATE,
Expand All @@ -39,7 +39,6 @@
UUID4_EXAMPLE,
UUID4_VALIDATE,
)
from ....storage.base import DEFAULT_PAGE_SIZE
from ....storage.error import StorageError, StorageNotFoundError
from ....utils.tracing import AdminAPIMessageTracingSchema, get_timer, trace_event
from ....vc.ld_proofs.error import LinkedDataProofException
Expand Down Expand Up @@ -568,8 +567,7 @@ async def credential_exchange_list(request: web.BaseRequest):
if request.query.get(k, "") != ""
}

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

try:
async with profile.session() as session:
Expand Down
6 changes: 2 additions & 4 deletions aries_cloudagent/protocols/present_proof/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ....messaging.decorators.attach_decorator import AttachDecorator
from ....messaging.models.base import BaseModelError
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.models.paginated_query import PaginatedQuerySchema
from ....messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ....messaging.valid import (
INDY_EXTRA_WQL_EXAMPLE,
INDY_EXTRA_WQL_VALIDATE,
Expand All @@ -36,7 +36,6 @@
UUID4_EXAMPLE,
UUID4_VALIDATE,
)
from ....storage.base import DEFAULT_PAGE_SIZE
from ....storage.error import StorageError, StorageNotFoundError
from ....utils.tracing import AdminAPIMessageTracingSchema, get_timer, trace_event
from ....wallet.error import WalletNotFoundError
Expand Down Expand Up @@ -312,8 +311,7 @@ async def presentation_exchange_list(request: web.BaseRequest):
if request.query.get(k, "") != ""
}

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

try:
async with context.profile.session() as session:
Expand Down
7 changes: 3 additions & 4 deletions aries_cloudagent/protocols/present_proof/v2_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ....messaging.decorators.attach_decorator import AttachDecorator
from ....messaging.models.base import BaseModelError
from ....messaging.models.openapi import OpenAPISchema
from ....messaging.models.paginated_query import PaginatedQuerySchema
from ....messaging.models.paginated_query import PaginatedQuerySchema, get_limit_offset
from ....messaging.valid import (
INDY_EXTRA_WQL_EXAMPLE,
INDY_EXTRA_WQL_VALIDATE,
Expand All @@ -37,7 +37,7 @@
UUID4_EXAMPLE,
UUID4_VALIDATE,
)
from ....storage.base import DEFAULT_PAGE_SIZE, BaseStorage
from ....storage.base import BaseStorage
from ....storage.error import StorageError, StorageNotFoundError
from ....storage.vc_holder.base import VCHolder
from ....storage.vc_holder.vc_record import VCRecord
Expand Down Expand Up @@ -449,8 +449,7 @@ async def present_proof_list(request: web.BaseRequest):
if request.query.get(k, "") != ""
}

limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
limit, offset = get_limit_offset(request)

try:
async with profile.session() as session:
Expand Down

0 comments on commit f3e33ab

Please sign in to comment.