Skip to content

Commit

Permalink
Metatx requesters fetch from resources for list contracts handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kompotkot committed Aug 14, 2024
1 parent 97c283a commit 68251d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
28 changes: 23 additions & 5 deletions engineapi/engineapi/contracts_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class CallRequestIdDuplicates(Exception):
"""


class MetatxRequestersNotFound(Exception):
"""
Raised when metatx requesters is not found in Brood resources.
"""


def parse_registered_contract_response(
obj: Tuple[RegisteredContract, Blockchain]
) -> data.RegisteredContractResponse:
Expand Down Expand Up @@ -274,7 +280,7 @@ def get_registered_contract(

def lookup_registered_contracts(
db_session: Session,
metatx_requester_id: uuid.UUID,
metatx_requester_ids: List[uuid.UUID],
blockchain: Optional[str] = None,
address: Optional[str] = None,
limit: int = 10,
Expand All @@ -286,7 +292,7 @@ def lookup_registered_contracts(
query = (
db_session.query(RegisteredContract, Blockchain)
.join(Blockchain, Blockchain.id == RegisteredContract.blockchain_id)
.filter(RegisteredContract.metatx_requester_id == metatx_requester_id)
.filter(RegisteredContract.metatx_requester_id.in_(metatx_requester_ids))
)

if blockchain is not None:
Expand Down Expand Up @@ -661,11 +667,23 @@ def complete_call_request(
return (call_request, registered_contract)


def fetch_metatx_requester_ids(token):
def fetch_metatx_requester_ids(token: uuid.UUID) -> List[uuid.UUID]:
params = {"type": "metatx_requester"}
resources = bugout_client.list_resources(token=token, params=params)

return resources
try:
resources = bugout_client.list_resources(token=token, params=params)
except Exception as err:
logger.error("Failed to list resources")
raise Exception(err)

if len(resources.resources) == 0:
raise MetatxRequestersNotFound(
"No metatx requesters for specific user available"
)

metatx_requester_ids = [r.id for r in resources.resources]

return metatx_requester_ids


def handle_register(args: argparse.Namespace) -> None:
Expand Down
13 changes: 9 additions & 4 deletions engineapi/engineapi/routes/metatx.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,26 @@ async def list_registered_contracts_route(
"""
Users can use this endpoint to look up the contracts they have registered against this API.
"""
user, token = user_authorization

contracts_actions.fetch_metatx_requester_ids(token=token)
_, token = user_authorization

try:
metatx_requester_ids = contracts_actions.fetch_metatx_requester_ids(token=token)

registered_contracts_with_blockchain = (
contracts_actions.lookup_registered_contracts(
db_session=db_session,
metatx_requester_id=user.id,
metatx_requester_ids=metatx_requester_ids,
blockchain=blockchain,
address=address,
limit=limit,
offset=offset,
)
)
except contracts_actions.MetatxRequestersNotFound:
raise EngineHTTPException(
status_code=404,
detail="Metatx requester IDs not found",
)
except Exception as err:
logger.error(repr(err))
raise EngineHTTPException(status_code=500)
Expand Down

0 comments on commit 68251d8

Please sign in to comment.