From 7bd1391528aa962dd2e62168c24a378cd0fdb154 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Tue, 22 Feb 2022 13:11:03 -0500 Subject: [PATCH] refactor: use constants for mediation metadata strings Signed-off-by: Daniel Bluhm --- .../v1_0/handlers/connection_request_handler.py | 16 ++++++---------- .../protocols/connections/v1_0/manager.py | 4 +++- .../coordinate_mediation/v1_0/manager.py | 2 ++ .../didexchange/v1_0/handlers/request_handler.py | 10 ++++------ .../protocols/out_of_band/v1_0/manager.py | 4 +++- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/aries_cloudagent/protocols/connections/v1_0/handlers/connection_request_handler.py b/aries_cloudagent/protocols/connections/v1_0/handlers/connection_request_handler.py index b1f26a85ae..38a7e385f5 100644 --- a/aries_cloudagent/protocols/connections/v1_0/handlers/connection_request_handler.py +++ b/aries_cloudagent/protocols/connections/v1_0/handlers/connection_request_handler.py @@ -1,12 +1,8 @@ """Connection request handler.""" -from .....messaging.base_handler import ( - BaseHandler, - BaseResponder, - RequestContext, -) from .....connections.models.conn_record import ConnRecord - +from .....messaging.base_handler import BaseHandler, BaseResponder, RequestContext +from ....coordinate_mediation.v1_0.manager import MediationManager from ..manager import ConnectionManager, ConnectionManagerError from ..messages.connection_request import ConnectionRequest from ..messages.problem_report import ConnectionProblemReport @@ -30,19 +26,19 @@ async def handle(self, context: RequestContext, responder: BaseResponder): profile = context.profile mgr = ConnectionManager(profile) + mediation_id = None if context.connection_record: async with profile.session() as session: mediation_metadata = await context.connection_record.metadata_get( - session, "mediation", {} + session, MediationManager.METADATA_KEY, {} ) - else: - mediation_metadata = {} + mediation_id = mediation_metadata.get(MediationManager.METADATA_ID) try: connection = await mgr.receive_request( context.message, context.message_receipt, - mediation_id=mediation_metadata.get("id"), + mediation_id=mediation_id, ) if connection.accept == ConnRecord.ACCEPT_AUTO: diff --git a/aries_cloudagent/protocols/connections/v1_0/manager.py b/aries_cloudagent/protocols/connections/v1_0/manager.py index 96e72b8b37..a1fb3af093 100644 --- a/aries_cloudagent/protocols/connections/v1_0/manager.py +++ b/aries_cloudagent/protocols/connections/v1_0/manager.py @@ -248,7 +248,9 @@ async def create_invitation( # Save that this invitation was created with mediation async with self.profile.session() as session: await connection.metadata_set( - session, "mediation", {"id": mediation_record.mediation_id} + session, + MediationManager.METADATA_KEY, + {MediationManager.METADATA_ID: mediation_record.mediation_id}, ) if keylist_updates: diff --git a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py index bd86337700..8fb2d2a449 100644 --- a/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py +++ b/aries_cloudagent/protocols/coordinate_mediation/v1_0/manager.py @@ -58,6 +58,8 @@ class MediationManager: DEFAULT_MEDIATOR_RECORD_TYPE = "default_mediator" SEND_REQ_AFTER_CONNECTION = "send_mediation_request_on_connection" SET_TO_DEFAULT_ON_GRANTED = "set_to_default_on_granted" + METADATA_KEY = "mediation" + METADATA_ID = "id" def __init__(self, profile: Profile): """Initialize Mediation Manager. diff --git a/aries_cloudagent/protocols/didexchange/v1_0/handlers/request_handler.py b/aries_cloudagent/protocols/didexchange/v1_0/handlers/request_handler.py index b7f036e11f..efcc02c64d 100644 --- a/aries_cloudagent/protocols/didexchange/v1_0/handlers/request_handler.py +++ b/aries_cloudagent/protocols/didexchange/v1_0/handlers/request_handler.py @@ -2,9 +2,8 @@ from .....connections.models.conn_record import ConnRecord from .....messaging.base_handler import BaseHandler, BaseResponder, RequestContext - +from ....coordinate_mediation.v1_0.manager import MediationManager from ....problem_report.v1_0.message import ProblemReport - from ..manager import DIDXManager, DIDXManagerError from ..messages.request import DIDXRequest @@ -27,15 +26,14 @@ async def handle(self, context: RequestContext, responder: BaseResponder): profile = context.profile mgr = DIDXManager(profile) + mediation_id = None if context.connection_record: async with profile.session() as session: mediation_metadata = await context.connection_record.metadata_get( - session, "mediation", {} + session, MediationManager.METADATA_KEY, {} ) - else: - mediation_metadata = {} + mediation_id = mediation_metadata.get(MediationManager.METADATA_ID) - mediation_id = mediation_metadata.get("id") try: conn_rec = await mgr.receive_request( request=context.message, diff --git a/aries_cloudagent/protocols/out_of_band/v1_0/manager.py b/aries_cloudagent/protocols/out_of_band/v1_0/manager.py index 7b8478e258..9fd4542c02 100644 --- a/aries_cloudagent/protocols/out_of_band/v1_0/manager.py +++ b/aries_cloudagent/protocols/out_of_band/v1_0/manager.py @@ -325,7 +325,9 @@ async def create_invitation( async with self.profile.session() as session: await conn_rec.metadata_set( - session, "mediation", {"id": mediation_record.mediation_id} + session, + MediationManager.METADATA_KEY, + {MediationManager.METADATA_ID: mediation_record.mediation_id}, ) if keylist_updates: