Skip to content

Commit 41304a2

Browse files
Sorted didcomm connections list in descending order by created_at field
Signed-off-by: George J Padayatti <[email protected]>
1 parent e37cd30 commit 41304a2

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

mydata_did/v1_0/manager.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4478,7 +4478,7 @@ async def delete_da_personal_data_in_wallet(self, *, personal_data_id: str) -> N
44784478
existing_version=data_agreement_dict.data_agreement_template_version,
44794479
existing_data_agreement_id=data_agreement_dict.data_agreement_template_id
44804480
)
4481-
4481+
44824482
# Mark the old data agreement record as deleted
44834483
data_agreement_record._delete_flag = True
44844484
data_agreement_record._publish_flag = False
@@ -4488,3 +4488,38 @@ async def delete_da_personal_data_in_wallet(self, *, personal_data_id: str) -> N
44884488
raise ADAManagerError(
44894489
f"Failed to delete data agreement; Reason: {e.roll_up}"
44904490
)
4491+
4492+
@classmethod
4493+
def serialize_connection_record(cls, connection_records: typing.List[ConnectionRecord], is_list: bool = True) -> dict:
4494+
"""
4495+
Serialize connection record.
4496+
4497+
Args:
4498+
connection_records: List of connection records.
4499+
is_list: If true, serialize as list.
4500+
4501+
Returns:
4502+
List of serialized connection records.
4503+
"""
4504+
4505+
connection_records_list = []
4506+
for connection_record in connection_records:
4507+
connection_records_list.append({
4508+
"state": connection_record.state,
4509+
"invitation_mode": connection_record.invitation_mode,
4510+
"connection_id": connection_record.connection_id,
4511+
"created_at": str_to_epoch(connection_record.created_at),
4512+
"updated_at": str_to_epoch(connection_record.updated_at),
4513+
"their_did": connection_record.their_did,
4514+
"accept": connection_record.accept,
4515+
"initiator": connection_record.initiator,
4516+
"invitation_key": connection_record.invitation_key,
4517+
"routing_state": connection_record.routing_state,
4518+
"their_label": connection_record.their_label,
4519+
"my_did": connection_record.my_did,
4520+
})
4521+
4522+
# Sort by created_at in descending order
4523+
connection_records_list = sorted(connection_records_list, key=lambda k: k['created_at'], reverse=True)
4524+
4525+
return connection_records_list if is_list else connection_records_list[0]

mydata_did/v1_0/routes.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,102 @@ async def get_existing_connections_handler(request: web.BaseRequest):
31903190

31913191
return web.json_response(result)
31923192

3193+
class ConnectionsListQueryStringSchemaV2(OpenAPISchema):
3194+
"""Parameters and validators for connections list request query string."""
3195+
3196+
alias = fields.Str(
3197+
description="Alias",
3198+
required=False,
3199+
example="Barry",
3200+
)
3201+
initiator = fields.Str(
3202+
description="Connection initiator",
3203+
required=False,
3204+
validate=validate.OneOf(["self", "external"]),
3205+
)
3206+
invitation_key = fields.Str(
3207+
description="invitation key", required=False, **INDY_RAW_PUBLIC_KEY
3208+
)
3209+
my_did = fields.Str(description="My DID", required=False, **INDY_DID)
3210+
state = fields.Str(
3211+
description="Connection state",
3212+
required=False,
3213+
validate=validate.OneOf(
3214+
[
3215+
getattr(ConnectionRecord, m)
3216+
for m in vars(ConnectionRecord)
3217+
if m.startswith("STATE_")
3218+
]
3219+
),
3220+
)
3221+
their_did = fields.Str(description="Their DID", required=False, **INDY_DID)
3222+
their_role = fields.Str(
3223+
description="Their assigned connection role",
3224+
required=False,
3225+
example="Point of contact",
3226+
)
3227+
3228+
3229+
class ConnectionListSchema(OpenAPISchema):
3230+
"""Result schema for connection list."""
3231+
3232+
results = fields.List(
3233+
fields.Nested(ConnectionRecordSchema()),
3234+
description="List of connection records",
3235+
)
3236+
3237+
def connection_sort_key(conn):
3238+
"""Get the sorting key for a particular connection."""
3239+
if conn["state"] == ConnectionRecord.STATE_INACTIVE:
3240+
pfx = "2"
3241+
elif conn["state"] == ConnectionRecord.STATE_INVITATION:
3242+
pfx = "1"
3243+
else:
3244+
pfx = "0"
3245+
return pfx + conn["created_at"]
3246+
3247+
@docs(
3248+
tags=["connection"],
3249+
summary="Query agent-to-agent connections (v2)",
3250+
)
3251+
@querystring_schema(ConnectionsListQueryStringSchemaV2())
3252+
@response_schema(ConnectionListSchema(), 200)
3253+
async def connections_list_v2(request: web.BaseRequest):
3254+
"""
3255+
Request handler for searching connection records.
3256+
3257+
Args:
3258+
request: aiohttp request object
3259+
3260+
Returns:
3261+
The connection list response
3262+
3263+
"""
3264+
context = request.app["request_context"]
3265+
tag_filter = {}
3266+
for param_name in (
3267+
"invitation_id",
3268+
"my_did",
3269+
"their_did",
3270+
"request_id",
3271+
):
3272+
if param_name in request.query and request.query[param_name] != "":
3273+
tag_filter[param_name] = request.query[param_name]
3274+
post_filter = {}
3275+
for param_name in (
3276+
"alias",
3277+
"initiator",
3278+
"state",
3279+
"their_role",
3280+
):
3281+
if param_name in request.query and request.query[param_name] != "":
3282+
post_filter[param_name] = request.query[param_name]
3283+
try:
3284+
records = await ConnectionRecord.query(context, tag_filter, post_filter)
3285+
results = ADAManager.serialize_connection_record(records)
3286+
except (StorageError, BaseModelError) as err:
3287+
raise web.HTTPBadRequest(reason=err.roll_up) from err
3288+
return web.json_response({"results": results})
31933289

31943290
async def register(app: web.Application):
31953291

@@ -3399,6 +3495,11 @@ async def register(app: web.Application):
33993495
"/v1/connections/{conn_id}/existing",
34003496
get_existing_connections_handler,
34013497
allow_head=False
3498+
),
3499+
web.get(
3500+
"/v2/connections",
3501+
connections_list_v2,
3502+
allow_head=False
34023503
)
34033504
]
34043505
)

0 commit comments

Comments
 (0)