6
6
import uuid
7
7
import logging
8
8
import jwt
9
+ import math
9
10
10
11
import validators
11
12
49
50
from .models .diddoc_model import MyDataDIDDocSchema
50
51
from .models .data_agreement_instance_model import DataAgreementInstanceSchema , DataAgreementInstance
51
52
52
- from .utils .util import str_to_bool , bool_to_str , comma_separated_str_to_list
53
+ from .utils .util import str_to_bool , bool_to_str , comma_separated_str_to_list , get_slices
53
54
from .utils .regex import MYDATA_DID
54
55
from .utils .jsonld .data_agreement import sign_data_agreement , verify_data_agreement , verify_data_agreement_with_proof_chain
55
56
56
57
57
58
LOGGER = logging .getLogger (__name__ )
58
59
60
+ PAGINATION_PAGE_SIZE = 10
61
+
59
62
60
63
class SendCreateDIDMessageMatchInfoSchema (OpenAPISchema ):
61
64
"""
@@ -421,6 +424,18 @@ class DataAgreementQueryStringSchema(OpenAPISchema):
421
424
example = "connection_id,state,presentation_exchange_id" ,
422
425
)
423
426
427
+ page = fields .Int (
428
+ required = False ,
429
+ description = "Page number" ,
430
+ example = 1 ,
431
+ )
432
+
433
+ page_size = fields .Int (
434
+ required = False ,
435
+ description = "Page size" ,
436
+ example = 10 ,
437
+ )
438
+
424
439
425
440
class UpdateDataAgreementMatchInfoSchema (OpenAPISchema ):
426
441
"""
@@ -1658,15 +1673,53 @@ async def query_data_agreements_in_wallet(request: web.BaseRequest):
1658
1673
context = context
1659
1674
)
1660
1675
1661
- # Fields to be included in the response.
1662
- include_fields = request .query .get ("include_fields" )
1663
- include_fields = comma_separated_str_to_list (
1664
- include_fields ) if include_fields else None
1676
+ # Pagination parameters
1677
+ pagination = {
1678
+ "totalCount" : 0 ,
1679
+ "page" : 0 ,
1680
+ "pageSize" : PAGINATION_PAGE_SIZE ,
1681
+ "totalPages" : 0 ,
1682
+ }
1683
+
1684
+ try :
1685
+
1686
+ # Fields to be included in the response.
1687
+ include_fields = request .query .get ("include_fields" )
1688
+ include_fields = comma_separated_str_to_list (
1689
+ include_fields ) if include_fields else None
1690
+
1691
+ # Query data agreements in the wallet
1692
+ (data_agreement_records , resp_da_list ) = await mydata_did_manager .query_data_agreements_in_wallet (tag_filter = tag_filter , include_fields = include_fields )
1693
+
1694
+ # Page size from request.
1695
+ page_size = int (request .query .get ("page_size" , PAGINATION_PAGE_SIZE ))
1696
+ pagination ["pageSize" ] = page_size
1697
+
1698
+ # Total number of records
1699
+ pagination ["totalCount" ] = len (resp_da_list )
1700
+
1701
+ # Total number of pages.
1702
+ pagination ["totalPages" ] = math .ceil (
1703
+ pagination ["totalCount" ] / pagination ["pageSize" ])
1704
+
1705
+ # Pagination parameters
1706
+ page = request .query .get ("page" )
1707
+ if page :
1708
+ page = int (page )
1709
+ pagination ["page" ] = page
1710
+
1711
+ lower , upper = get_slices (page , pagination ["pageSize" ])
1712
+
1713
+ resp_da_list = resp_da_list [lower :upper ]
1714
+
1715
+ except (StorageError , BaseModelError , ValueError ) as err :
1665
1716
1666
- # Query data agreements in the wallet
1667
- (data_agreement_records , resp_da_list ) = await mydata_did_manager .query_data_agreements_in_wallet (tag_filter = tag_filter , include_fields = include_fields )
1717
+ raise web .HTTPBadRequest (reason = err .roll_up ) from err
1668
1718
1669
- return web .json_response (resp_da_list )
1719
+ return web .json_response ({
1720
+ "results" : resp_da_list ,
1721
+ "pagination" : pagination if page else {},
1722
+ })
1670
1723
1671
1724
1672
1725
@docs (
@@ -3265,6 +3318,18 @@ class ConnectionsListQueryStringSchemaV2(OpenAPISchema):
3265
3318
example = "connection_id,state,presentation_exchange_id" ,
3266
3319
)
3267
3320
3321
+ page = fields .Int (
3322
+ required = False ,
3323
+ description = "Page number" ,
3324
+ example = 1 ,
3325
+ )
3326
+
3327
+ page_size = fields .Int (
3328
+ required = False ,
3329
+ description = "Page size" ,
3330
+ example = 10 ,
3331
+ )
3332
+
3268
3333
3269
3334
class ConnectionListSchema (OpenAPISchema ):
3270
3335
"""Result schema for connection list."""
@@ -3322,10 +3387,30 @@ async def connections_list_v2(request: web.BaseRequest):
3322
3387
):
3323
3388
if param_name in request .query and request .query [param_name ] != "" :
3324
3389
post_filter [param_name ] = request .query [param_name ]
3390
+
3391
+ # Pagination parameters
3392
+ pagination = {
3393
+ "totalCount" : 0 ,
3394
+ "page" : 0 ,
3395
+ "pageSize" : PAGINATION_PAGE_SIZE ,
3396
+ "totalPages" : 0 ,
3397
+ }
3398
+
3325
3399
try :
3326
3400
3327
3401
records = await ConnectionRecord .query (context , tag_filter , post_filter )
3328
3402
3403
+ # Page size from request.
3404
+ page_size = int (request .query .get ("page_size" , PAGINATION_PAGE_SIZE ))
3405
+ pagination ["pageSize" ] = page_size
3406
+
3407
+ # Total number of records
3408
+ pagination ["totalCount" ] = len (records )
3409
+
3410
+ # Total number of pages.
3411
+ pagination ["totalPages" ] = math .ceil (
3412
+ pagination ["totalCount" ] / pagination ["pageSize" ])
3413
+
3329
3414
# Fields to be included in the response.
3330
3415
include_fields = request .query .get ("include_fields" )
3331
3416
include_fields = comma_separated_str_to_list (
@@ -3334,9 +3419,24 @@ async def connections_list_v2(request: web.BaseRequest):
3334
3419
results = ADAManager .serialize_connection_record (
3335
3420
records , True , include_fields )
3336
3421
3337
- except (StorageError , BaseModelError ) as err :
3422
+ # Pagination parameters
3423
+ page = request .query .get ("page" )
3424
+ if page :
3425
+ page = int (page )
3426
+ pagination ["page" ] = page
3427
+
3428
+ lower , upper = get_slices (page , pagination ["pageSize" ])
3429
+
3430
+ results = results [lower :upper ]
3431
+
3432
+ except (StorageError , BaseModelError , ValueError ) as err :
3338
3433
raise web .HTTPBadRequest (reason = err .roll_up ) from err
3339
- return web .json_response ({"results" : results })
3434
+ return web .json_response (
3435
+ {
3436
+ "results" : results ,
3437
+ "pagination" : pagination if page else {},
3438
+ }
3439
+ )
3340
3440
3341
3441
3342
3442
async def register (app : web .Application ):
0 commit comments